Java learning summary from a rookie-basic data types

Basic data type

1. Classification of basic data types

The size of bytes occupied by various basic data types (unit: Byte, where 1 * Byte = 8 * bit)

Data type name byte short int long float double char boolean
type of data Integer Integer Integer Integer Floating point Floating point Character type Boolean
Byte size 1 2 4 8 4 8 2 1

2. Integer data type

①Byte is a byte data type. The range that can be represented by a Byte in the memory is: -128 127 (-2^7 2^7-1)

Store the analog image in the memory space: display as -128 and 127 (mainly based on binary conversion)

1 1 1 1 1 1 1 1
0 1 1 1 1 1 1 0

That is -128 = -2^7,127 = 2^7-1;

②The range that short occupies two Bytes in the memory is: -32768~32767 (-2 15~2 15-1)

③The range that the int integer occupies four bytes in the memory is: -2147483648~2147483647 (-2 31~2 31-1)

④ **The long integer occupies eight bytes in the memory. The range that can be represented is: **

-9223372036854775808 ~9223372036854775807 (-263~236-1)

In the actual operation process, when byte, short, int, and long are faced with data whose literal is integer, their literal defaults to int. It should be noted that forced conversion should be used at this time to prevent loss or failure By compiling

3. Floating point data type

float type and double type

public class Demo {
    public static void main(String[] args){
      float f = 3.1415926;//报错,因为字面量为小数时默认为double型,而double类型为8个Byte,float类型为4个Byte
        //因为数据类型在电脑中是通过二进制的形式在存储空间中存储这一机制我们就能理解这里为什么会出现编译无法通过的原因
      float f1 = 3;//在这没有报错的原因和上面报错的原因、机制是一样的
        // 因为int型是4个Byte,float类型也是4个Byte所以在存储空间上不会出现溢出、越界这种问题故能编译通过
      double d = 3.1415926;
        System.out.println(f);//无法通过编译不能输出
        System.out.println(f1);//输出为3.0
        System.out.println(d);//输出为3.1415926
    }
}

4. Character data type

char type

public class Demo {
    
    
    public static void main(String[] args){
    
    
        char c = 'a';
        System.out.println(c);//输出结果为"a"
        //其中char类型可以转换为int型,而int型却不能随意转换为char类型
    }
}
//因为int型为4个Byte char为2个Byte
public class Demo {
    
    
    public static void main(String[] args){
    
    
        char c = 97;
        System.out.println(c);//输出结果为"a"
    }
}

public class Demo {
    
    
    public static void main(String[] args){
    
    
        char c = 32767;
        System.out.println(c);//输出结果为"翿"
    }
}

public class Demo {
    
    
    public static void main(String[] args){
    
    
        int i = 'a';
        char c = i;//编译无法通过
        System.out.println(c);
    }
}

5. Boolean type

The main expressed values ​​are true (true) and false (false)

public class Demo {
    public static void main(String[] args){
        boolean flag = true;
        System.out.println(flag);//输出值为 true
    }
}

6. Binary Conversion in Computer (Abstract)

在计算机内存存储的二进制都是以补码的形式进行存储,在内存中第一位表示符号位,0表示正,1表示负
什么是补码?
对于一个正数来说,它的原码、反码、补码都一个样
    如:10的原码:00000000 00000000 00000000 00001010
对于一个负数来说,它的原码、反码、补码是什么样子的、
    原码很简单,就是一个整数的二进制表示,第一位是符号位
    如:-1的原码:10000000 00000000 00000000 00000001
    	反码:符号位不变,其他位取反也就是说,原来是0的要变成1,是1的要变成0,记住符号位不变
       -1的反码:11111111 11111111 11111111 11111110
        补码:反码+1
       -1的补码:11111111 11111111 11111111 11111111

7. Conversion between basic data types

Through the memory occupied by the basic data types in the storage space and the storage mechanism of the data in binary form in the computer, as well as the results obtained in the actual operation, the following figure can be known:
Insert picture description here

Among them, it is automatically converted according to the above rules, and the precision may be lost when the int and long types are converted to float and double;

In addition to the forced conversion part of the code is shown below:

public class Demo {
    public static void main(String[] args){
        byte b = (byte)200;
        System.out.println(b);//输出结果为 -56
        short s = (short)32768;
        System.out.println(s);//输出结果为 -32768
        //ong l = 2147483648;//编译器无法通过;
        long l1 = 2147483648L;
        System.out.println(l1);//输出结果为 2147483648
        //float f = 3.1415926;//编译无法通过
        float f1 = 3.1415926f;
        System.out.println(f1);//输出结果 3.1415925
    }
}

From the result of the above code, we can also know that there is a loss of precision in the forced conversion.
Finally: the basic data type, small to large, can be automatically converted within the value range of the smaller, also called automatic type conversion. Big to small, forced to change, also called forced type conversion

Guess you like

Origin blog.csdn.net/weixin_45698813/article/details/112972880