Briefly describe java variables and data types

1. Variables

Whether we use C language or Java or other programming languages ​​to write programs, variables are the basic units of our programs. There are three elements of variables: type + name + value

For example: int a = 2022; //The type is int integer, the name is a, and the value is assigned as 2022.

Regarding the definition and use of variables, we should develop a good coding habit:

Variables must be declared before they are used. It is also possible to declare a variable and initialize it at the same time.

The representation of variables has an area in memory, and the space occupied by different types of variables is different. For example: int occupies 4 bytes. double takes 8 bytes. The size of the occupied space is related to the data type. Variables cannot have the same name in the same scope.

2. Operators

1. Use of the + sign

(1): When both sides are numbers, the + sign is added - - -System.out,println(10+8);//18

(2): When any of the two sides is a string, the + sign is spliced ​​- - -System.out,println("hello"+5);//hello5

The operation rules are from left to right: Note that if the string is on the left, the splicing will be done on the right - - -System.out,println("hello"+20+22);//hello2022

3. Data type

Each type of data defines a clear data type and allocates different memory spaces in memory .

Integer: 

 

 Java's integer constants default to int type. If you declare long type, you must add l or L after the constant. 

int n1 = 1;//4 个字节
//int n2 = 1L;//对不对?不对
long n3 = 1L;//对

Float type:

Java's floating-point type can be expressed as a decimal, divided into single-precision float and double-precision double, single-precision occupies 4 bytes, and double-precision occupies 8 bytes.

Java's floating-point constant defaults to double type, and when declaring a float constant, add f or F after it. Normally, the double type should be used because it is more precise than the float type.

Character type:

A character constant is a single character enclosed in single quotes (' '). Java also allows the use of '\' to convert the subsequent characters into special character constants. For example: char a = '\n'; The char type can be operated, which is equivalent to an integer, because it corresponds to a Unicode code.

 Boolean type (boolean):

Boolean type data only allows the values ​​true and false, no null, occupying 1 byte. The boolean type is suitable for logical operations and is generally used for program flow control.

Automatic type conversion:

When the program performs assignment or operation, the type with low precision is automatically converted to a data type with high precision. This is automatic type conversion.

Details: When there are multiple data mixed operations at the same time, the system first automatically converts all data into the data type with the largest capacity, and then performs calculations. byte, short and char are not automatically exchanged with each other. Boolean does not participate in the conversion.

Mandatory type conversion:

Convert a large-capacity data type to a small-capacity data type. When using it, you need to add the coercion character ( ), but it may cause precision reduction or overflow.

int i = (int)1.6;
System.out.println(i);
int j = 8;
byte b1 = (byte)j;
System.out.println(b1);

Details: Mandatory conversion is big-->small, the mandatory symbol is only valid for the nearest operand, often need to use parentheses to improve the priority. The char type can save the constant value of int, but it cannot save the variable value of int, and it needs to be converted. Byte, short, and char types are treated as int types when performing operations.

char c = 7;//T
int n = 7;
char d = n;//F
char d = (char)n;//T
System.out,println(d);

Conversion of basic data types and String types

We often need to convert basic data types into String types. Or convert the String type to a basic data type.

(1): Basic type to String type Syntax: the value of the basic type + " ".

int a = 1;
float b = 2.1f;
String str1 = a+"";
String str2 = b+"";
System.out.println(str1 + " " + str2 + " ");

(2): Convert String type to basic data type.

Just call the parsexx method through the wrapper class of the basic type.

Inter.parseInt("12");
Double.parseDouble("123.45");

Note: When converting the String type into a basic data type, make sure that the String type can be converted into valid data. For example, we can convert "123" into an integer, but we cannot convert "hello" into an integer.
 

Guess you like

Origin blog.csdn.net/weixin_64131583/article/details/122928749