Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use variables in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use variables in Java, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

In order to store a piece of data in Java, it must be contained in a variable. The data type determines what value a variable can be assigned to and what to do with the variable. The two basic elements of defining a variable are the type and the identifier. You can usually describe the variable with the following syntax:

Type identifer [, identifer]; this statement tells the compiler to use the type of "type" and create a variable with the name "identifer", where the semicolon will tell the compiler that this is the end of a statement; the comma and identifier in the box indicate that you can put several variables of the same type in the same statement, separated by commas between the variable names.

After you have created a variable, you can assign a value to it or perform some operations on it with an operator. Types determine the different kinds of data that variables represent, and there are two kinds of variables in the Java language. The most basic are simple type variables, which are not based on any other type, such as integers, floating-point, Boolean, and character types (note that unlike other programming languages, strings appear here as an instance of a class) In addition, Java can define and construct another type of variable: class, which is based on a simple type, which includes values, variables, and methods, and is a composite structure that combines data and code.

1: description of integer variables

Integer variables can be divided into four different types according to the amount of memory they occupy. The shortest integer is byte, which is only eight bits long, followed by a short integer short, which has 16 bits, an int type of 32 bits, and a long integer long of 64 bits. Here are illustrative examples of these integer variables.

Byte bCount; (in memory: 8 Bits)

Short sCount; (16 Bits in memory)

Int nCount; (in memory: 32 Bits)

Long LCount; (in memory: 64 Bits)

Int nx,ny,nz; (in memory: 32 Bits)

2: description of floating point variables

Floating-point types can be described by the keywords float or double. Float variables are used to represent a 32-bit single-precision floating-point number, while float-type floating-point variables are used to represent a 64-bit double-precision floating-point number. The floating point number represented by the float type is more accurate than the float type.

Float areas

Double weihgt

3: character variable description

Java uses the 16-bit Unicode character set. So the Java character is a 16-bit unsigned integer, and the character variable is used to hold a single character. For example:

Char a

A = 'c 'a

4: Boolean variable description

Boolean has both true and false logical values. In addition, logical operators return Boolean values, such as:

Boolean onClick

MouseOn=true

Boolean is an independent type. Boolean types in Java do not represent 0 and 1 integers and cannot be converted to numbers.

5: the scope of use of variables

When you specify a variable, it will be introduced into a scope, that is, the name can only be used within a specific scope of the program. The use of a variable is from where it is described to the end of the block in which it is located, which is defined by two curly braces, for example:

Class Example

Public static void main (String args [])

Int i

.

Public void function ()

Char c

.

The integer variable I is stated in the method main, and because the block of main does not include the function block, any reference to I in the function block is incorrect. The same is true for the character variable c.

In a particular case, a variable can be hidden by other variables, such as describing a variable in a block and creating a new block and defining a variable with the same name in the block, so that in the second block, the program's use of the variable refers to the variable defined the second time. In this way, we say that the first variable is hidden, and the author does not recommend this method of defining variables. Examples of variable hiding are as follows:

Class Example

Public static void main (String args [])

Int I; / / *

Boolean try=true

While (try)

Int I; / / the following references to variable I refer to the I defined here.

.

/ / the following references to the variable I refer to the I defined at *

.

When you define a variable, you must first clarify its scope of activity and name it according to its actual function. In addition, you should try to use detailed comments, which enable you to clearly distinguish between variables. The problem of variables being hidden will also be greatly reduced.

6: type conversion

The system method System.in.read returns an integer value, but you often want to use it as a character. The question now is, what should you do when you have an integer and you need to turn it into a character? You need to do a type conversion to a character. To convert from one type to another, you can use the following statement:

Int a

Char b

A = (int) b

Parenthesized int tells the compiler that you want to make the character an integer and put it in a. On the other hand, if you want to do the opposite conversion, you can use:

B = (char) a

It is important to remember that the bit lengths of integer and character variables are different. Integers are 32 bits long and character types are 16 long, so you may lose information when you switch from integers to character types. Similarly, when you convert 64-bit long integers to integers, you are likely to lose information because long integers may have more information than 32-bits. Even if two quantities have the same number of digits, such as integer and floating point (both 32 digits), you will lose information when converting decimals. Java does not allow automatic type conversion. When you do type conversion, you should pay attention to making the target type can accommodate all the information of the original type. Type conversions that will not lose information include:

Original type target type

Byte-> short-> char-> int-> long-> float-> double

Short-> int-> long-> float-> double

Char-> int-> long-> float-> double

Int-> long-> float-> double

Long-> float-> double

Float-> double

It is important to note that information may not always be lost when you perform a type conversion that is not listed here, but it is dangerous to make such a theoretically unsafe conversion.

Thank you for reading this article carefully. I hope the article "how to use variables in Java" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report