Java basic variable detailed variable conversion shift operation

The shape of the variable

class Test {
    
    
    static int a; // 静态属性/类成员变量/类变量
    int b; // 属性/成员变量/实例变量

    public static void main(String[] args ) {
    
    // 局部变量
        int c; // 局部变量
    }
}

Here b is equivalent to a global variable, and c is a local variable. The usage here is similar to the C language, but the static properties are different from the C language.
Static variables modified with the static keyword and instance variables not modified with the static keyword. A static variable belongs to a class, and there is only one copy in memory. As long as the class in which the static variable is located is loaded, the static variable will be allocated space, so it can be used. There are two ways to reference static variables, namely "class.static variable" and "object.static variable".
Instance variables belong to objects. Only after the object is created, the instance variables will be allocated memory space and can be used. There are multiple copies in memory, and they can only be referenced in the form of "object. instance variable".

Integer variable (int)

In Java, an int variable occupies 4 bytes, which is not directly related to the operating system.
The data range represented by 4 bytes is -2^31 -> 2^31-1, which is about 2.1 billion to +2.1 billion.

Title long integer variable (long)

long num = 10L; // 定义一个长整型变量, L
System.out.println(num
) ;

The long type in Java occupies 8 bytes. The range of data represented is -2^63 -> 2^63-1

Floating point variable

Double precision floating point variable (double) 8 bytes
Single precision floating point variable (float) 4 bytes

float num = 1.0f;    // 写作 1.0F 也可以
System.out.println(num);

Character type variable

  1. In Java, single quotation marks + single letters are used to represent character literals.
  2. A character in a computer is essentially an integer. The C language uses ASCII to represent characters, while Java uses Unicode to represent characters. Therefore, a character occupies two bytes, and there are more types of characters, including Chinese.

Byte type variable

byte value = 0;
System.out.println(value);
  1. The byte type also represents an integer. It occupies only one byte, which means that the range is small (-128 -> +127)
  2. Byte type and character type are not related to each other.

Short integer variable

  1. short occupies 2 bytes, and the data range represented is -32768 -> +32767
  2. This indicates that the range is relatively small and is generally not recommended.

Boolean variable

boolean value = true;
System.out.println(value);

Precautions:

  1. Variables of boolean type have only two values, true means true, and false means false.
  2. Java's boolean type and int cannot be converted to each other. There is no such use as 1 for true and 0 for false.
  3. Some JVM implementations of boolean type occupies 1 byte, and some occupies 1 bit, which is not clearly specified.

String type variable

  1. Java uses double quotes + several characters to represent string literals.
  2. Unlike the above types, String is not a basic type, but a reference type (explained later).
  3. Some specific characters in the string that are not convenient for direct representation need to be escaped.
     System.out.println("\\\"hello\\\"");
     

Output result: "hello"

\n Newline
\r Back to the beginning of the line
\t Horizontal tab
'Single quote
"Double quote
\ Backslash

The + operation of string means string splicing:
string and integer can also be used for splicing:

Variable naming rules

  1. A variable name can only contain numbers, letters, and underscores
  2. The number cannot start.
  3. Variable names are case sensitive. That is, num and Num are two different variables.

Constants modified by the final keyword

Constants cannot be modified while the program is running.

final int a = 10;
a = 20;    // 编译出错. 提示 无法为最终变量a分配值

Type conversion

A small range can be directly converted to a large range, and a large range to a small range needs to be forced to convert.
The (type) method can be used to force the double type to int. But

  1. Forced type conversion may result in loss of precision. For example, after the assignment, 10.5 becomes 10, and the part after the decimal point is ignored.
  2. Coercive type conversion does not necessarily succeed, and it is impossible to force conversion between unrelated types.
byte a = 10;
byte b = 20;
byte c = a + b;
System.out.println(c);
// 编译报错
Test.java:5: 错误: 不兼容的类型:int转换到byte可能会有损失
       

Both byte and byte are of the same type, but a compilation error occurs. The reason is that although a and b are both bytes, calculating a + b will first promote both a and b to int, and then perform the calculation, the result is also int, This is assigned to c, the above error will occur.
Because the computer's CPU usually reads and writes data from the memory in units of 4 bytes. For the convenience of hardware implementation, such as byte and short are less than 4 bytes The type of will be promoted to int first, and then participate in the calculation.

Conversion between int and String

int into String

int num = 10;
// 方法1
String str1 = num + "";  
// 方法2
String str2 = String.valueOf(num);

String to int

String str = "100";
int num = Integer.parseInt(str);

Short-circuit evaluation

&& and || follow the rules of short-circuit evaluation.

System.out.println(10 > 20 && 10 / 0 == 0);             // 打印 false
System.out.println(10 < 20 || 10 / 0 == 0);             // 打印 true

Calculating 10/0 will cause the program to throw an exception. However, the above code works normally, indicating that 10/0
is not really evaluated.

Shift operation

Shift left <<: The leftmost bit is no
longer needed, and the rightmost bit is filled with 0. Shift right >>: The rightmost bit is no longer needed, and the leftmost bit is filled with sign bit (positive number complement 0, negative number complement 1)
unsigned right shift> >>: The rightmost bit is no longer needed, and 0 is added to the leftmost bit.

int a = 0x10;
System.out.printf("%x\n", a << 1);
// 运行结果(注意, 是按十六进制打印的)
20
int a = 0x10;
System.out.printf("%x\n", a >> 1);
// 运行结果(注意, 是按十六进制打印的)
8
    
int a = 0xffffffff;
System.out.printf("%x\n", a >>> 1);
// 运行结果(注意, 是按十六进制打印的)
7fffffff

The basic rules of Java operators are basically the same as those of C language.

Guess you like

Origin blog.csdn.net/weixin_45070922/article/details/110939108