Detailed explanation of java data types and operators

1. Integer variable

1. Basic format
int variable name = initial value;

code example:

public class CSDN  {
    
    
	public static void main(String[] args) {
    
    
		int a=10;
		System.out.println(a);
	}
}

2. The int in java is 4 bytes.
3.Maximum and minimum of int in java

If the number is greater than 0, the highest bit is 0, if the number is less than 0, the highest bit is 1. When judging the value range, it is not necessary to consider the negative number to be stored in the form of complement, because the complement and the original code have a one-to-one correspondence. When the highest bit is 1, it is a negative number, and there are at most 31 1s behind, and the sum of the 31 1's is 2 to the 31st power -1, and the highest bit of the negative number is also 1, and the bit is being carried again When the condition of negative number is still satisfied, the highest bit can be regarded as a numerical bit, so the minimum value is -2 to the 31st power, and the highest bit of the maximum integer must have a value of 1, and the original code is equal to the complement, which limits The maximum value of the integer 31 is added to 1, and the sum is 2^31-1.

 public class CSDN{
    
    
 	public static void main(String[] args) {
    
    
 		int a = 10;
 		System.out.println(Integer.MAX.VALUE);
 		System.out.println(Integer.MIN.VALUE);
 	}
 }

4. If a local variable is not initialized, it cannot be used.
The identifier may comprise: underscore alphanumeric characters, but can not begin with a number, and the underscore character is not recommended, but it can not begin with a number, and is not recommended by the underlineFu , but opening the head is not able to use the number of words , not built proposed by the draw line and .

2. Long integer variable

Define a long integer, and L must be added after the number

	public static void main(String[] args) {
    
    
 		Long a = 10L;
 		System.out.println(a);
 	}

Java is different from C language. There is no such thing as unsigned. Long has 8 bytes and 64 bits. The first bit is the sign bit.
If you want to print Chinese characters, use the plus sign to splice.

 	public static void main(String[] args) {
    
    
 		Long a = 10L;
 		System.out.println(a);
        System.out.println("a的值是:"+a);//打印汉字用引号括起来然后用加号拼接

Any type of data and string are concatenated, and the result is a string.

Three. Double-precision floating-point type

 	public static void main(String[] args) {
    
    
 		//双精度浮点型
 		double d = 12.5;
 		System.out.println(d);
 	}

Decimals default to double precision type.

public static void main(String[] args) {
    
    
int a = 1;
int b = 2;
System.out.println(a / b);
}
//输出结果是0

Because both a and b are integers, the printed ones are also integers, and decimals cannot be saved.

public static void main(String[] args) {
    
    double num = 1.1;
System.out.println(num * num)
// 执行结果=1.2100000000000002
}

Any decimal number has precision, there is no precise value, there is only a range. To study this problem, you need to understand the storage of floating-point numbers in memory.
In general, if you encounter decimals, double is recommended

Four. Single-precision floating-point type

Float is 4 bytes. You cannot put the double type in the float type, which will cause compilation errors. You need to add an f after the decimal, which reflects the safety of Java.

	public static void main(String[] args) {
    
    
 		float f = 12.3;
 		System.out.println(f);
 	}

Five. Character data type

Char occupies two bytes in Java, and one byte in C language.

 	public static void main(String[] args) {
    
    
 		char ch = 'a';
 		System.out.println(ch);
 		char ch2 = 97;
 		System.out.println(ch2);
 	}

Following the unicode character set in Java is similar to the asii value in the C language, but the unicode represents a wider range of characters.

6. Byte type

Precautions:

  1. The byte type also represents an integer. It occupies only one byte, which means that the range is small (-128 -> +127)
  2. The byte type and the character type are not related to each other.
	public static void main(String[] args) {
    
    
 		byte b = 12;
 		byte c = 21;
 		System.out.println(b+" "+c);
 	}

Each data type cannot exceed its range when assigning values, otherwise an error will be reported.

Seven. Short integer

short is two bytes, and the value range is -32768 to 32767.
This means that the range is small and is generally not recommended.

	public static void main(String[] args) {
    
    
 		short sh = 12;
 		System.out.println(sh);
 	}

8. Boolean type

Boolean type:
1. In java, Boolean type has no clear size.
2. In java, the Boolean type has only two values, true and false.
3. In java, there is no such thing as 0 is false and non-zero is true.

	public static void main(String[] args) {
    
    
        boolean flag = true;
        System.out.println(flg); 
 	}

Summary: The
8 basic data types in java are as follows:
1 2 2 4 8 4 8
byte char short int long float double boolean
Byte Character Short Integer Long Float Double Boolean

String type variable

Note that the string type is defined with String
:

  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.
    The + operation of the string means string splicing:
String a = "hello"; 
String b = "world"; 
String c = a + b; 
System.out.println(c);

The above code shows that when there is a string in a + expression, string splicing is performed.
Therefore, we can easily use System.out.println to print multiple strings or numbers at the same time.

Understand numerical improvement

Int and long mixed operation

int a = 10; 
long b = 20; 
int c = a + b; // 编译出错, 提示将 long 转成 int 会丢失精度
long d = a + b; // 编译通过

Conclusion: When int and long are mixed, int will be promoted to long, and the result will still be of type long. You need to use a variable of type long to
receive the result. If you must use int to receive the result, you need to use a coercion .
Byte and byte operations

byte a = 10; 
byte b = 20; 
byte c = a + b; 
System.out.println(c); 
// 编译报错
Test.java:5: 错误: 不兼容的类型:int转换到byte可能会有损失
 byte c = a + b; 

Conclusion: byte and byte are both of the same type, but a compilation error occurs. The reason is that although a and b are both bytes, the calculation of a + b will first promote both a and b to int, and then perform the calculation, the result is also int, which 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, types such as byte and short that are less than 4 bytes will be upgraded to int before participating in the calculation .
Type promotion summary:

  1. Mixed calculations of different types of data, a small range will be upgraded to a large range.
  2. For short, byte, which is smaller than 4 bytes, it will first be promoted to a 4-byte int before the operation.

Conversion between int and String

int into String

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

Convert String to int

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

Operator

1. The basic four operators +-* /%
rules are relatively simple, it is worth noting that the result of division
a) int / int is still int, you need to use double to calculate
b) 0 cannot be used as a divisor
2. The return types of relational operators are boolean Types of.
3. Shift operator
Left shift <<: The leftmost bit is unnecessary, and the rightmost bit is filled with 0.
Shift right >>: The rightmost bit is omitted, and the leftmost bit is complemented by the sign bit (positive number complements 0, negative number complements 1).
Note:

  1. Shift 1 bit to the left, which is equivalent to the original number * 2. Shift to the left by N digits, is equivalent to the Nth power of the original number * 2.
  2. Shifting to the right by 1 digit is equivalent to the original number / 2. Shifting to the right by N digits is equivalent to the original number / 2 to the power of N.
  3. Since the computer's calculation of shift efficiency is higher than that of calculation of multiplication and division, when a code is multiplied and divided by 2 to the Nth power, it can be replaced by a shift operation.
  4. It doesn't make sense to shift negative digits or shift too large digits.

Guess you like

Origin blog.csdn.net/weixin_53831496/article/details/114915275