java zero foundation 1 -- basic data type

Classification of data types

First: basic data type
1. Integer type: byte, short, int, long (1 byte, 2 bytes, 4 bytes, 8 bytes)
2. Floating point type: float, double (4 bytes, 8 bytes)
3. boolean: boolean (1 byte)
4. Character type: char (2 bytes)

Second: reference data type (all except basic type)
For example, the strings, String classes, and objects we will learn later are reference data types.

Value range of basic data types to be mastered

Value of byte: [- 128-127]
Value of short: [- 32168-32767]
Value of int: [- 2147483648-2147483647]
Value of char: [0-65535]

Rules for data conversion

Here's the conclusion. Each point in the following text will be explained one by one.
1. All but Boolean cannot be converted
2. It does not exceed the value range of byte, short and char, and can be assigned directly
3. Automatic conversion from small capacity to large capacity
4. The conversion from large capacity to small capacity becomes forced conversion, and the conversion character shall be strengthened
5.byte, short and char will be automatically converted to int during mixed operation
6. When multiple data types are mixed, they will be converted to the maximum participation first, except for the three data types in Article 5.

Integer type

1. Output different hexadecimal

Start with 0: output octal
Beginning with 0X: output hex
Beginning with 0b: output binary

        int a=10;
		System.out.println(a);//Decimal 10
		int b=010;//Octal 8 starting with 0
		System.out.println(b);
		int c =0x10;//Hex 16 starting with 0x
		System.out.println(c);
		int d =0b10;//Binary 2 starting with 0b
		System.out.println(d);

2. Shaping literal quantity

*Note: in any case, integer literals are treated as "int". If you want to process them through long, add L or l after the literals

        long g = 2147483647;//This value is treated as int, which is the maximum value of int and can be passed through
		long h =2147483628//Compilation error, exceeding the maximum value of int
		//solve
		long h =2147483628L;
		System.out.println(h);
		//Small capacity to large capacity, automatic type conversion

3. Shaping data conversion

Cast type converter: (converted type) + the variable you want to convert

        int e = 100;
		System.out.println(e);//100
		//There is type conversion here. e is int
		//b is a long type, with int accounting for 4 bytes and long accounting for 8 bytes. The conversion from small capacity to large capacity is called automatic type conversion
		long f = e;
		System.out.println(f);//100
		
        long x = 100L; 
 		int y = x;//Compilation error, long conversion to int may be lost
		//Large capacity is converted to small capacity. If you want to compile, you need to use the cast type converter (type), but the accuracy may be lost at run time
		int y = (int)x;//correct
		
		byte n = (byte)300;//300 is treated as int, to be compiled through the type converter to be cast
		System.out.println(n);//Output 44, loss of some accuracy
		byte m = 1;//Large capacity cannot be directly converted to small capacity, but it has been compiled and does not exceed the value range of byte. It can be passed. short is the same as char.

float

1. Default floating point type

Note: floating point data in java is treated as double by default. If you want to use it as float, you need to add f or F later

        double pi =3.14;//No data conversion
		System.out.println(pi);
		//float x=3.14;// report errors
		float x = 3.14f;//Mandatory type conversion is also OK!

2. Floating point conversion bit shaping

    //The following program error, double large capacity cannot be directly assigned to small capacity int
	int y=10.0/5;
	//modify
	int y =(int)10.0/5;
	System.out.println(y);//2

character

1. Character representation

In java, characters are enclosed in 'single quotation marks. Note that only one character can be enclosed in single quotation marks. For example,' ab 'is wrong

        char a ='in';//A Chinese character can be stored in char
		System.out.println(a);//in
		char b ='a';
		System.out.println(b);
		char c ='0';
		System.out.println(c);

2. Assign integer to character type

        char x = 97;
		System.out.println(x);//When an integer is assigned to char, it will be automatically converted into characters and output a
		char x1 = (char)65536;//65536 exceeds the maximum value of char. Strong rotation is required

Boolean

1. Boolean value and common range

1. In java, boolean has only two values: true and false
2. In practical application, it is often used for logical judgment (which will be described later)

2. Boolean type cannot be converted

        boolean xingbie =1;//An error is reported. int cannot be converted to Boolean
        
		//correct
		boolean x = true;
		System.out.println(x);//true
		
		int y = (int)x;//Error reported, incompatible type, unable to convert!
		char z = (char)x;//Error reported, incompatible type, unable to convert!

Hybrid operation

1. When byte, char and short are mixed, they are first converted to int type

        char c1='a';
		byte b=1;
		System.out.println(c1+b);//98, the operation result is int

2. Common errors

b and c1 here are the same as the code above

        short c =c1+b;If an error is reported, the compiler does not know the result of the operation, but only the result int Type, so even if it doesn't exceed short In order to ensure that the program runs. Will also report an error.
		//Modification:
		short c =(short)(c1+b);
		System.out.println(c);//98

3. The result of int and int is still int

System.out.println(10/3);//3

Keywords: Java Back-end

Added by decypher on Mon, 01 Nov 2021 03:32:23 +0200