Day7 (expansion of integer types, floating-point numbers, characters, escape characters, and Boolean values)

Integer expansion:

Binary 0b Example: int i = 0b10
Decimal Example: int i = 10
Octal 0 Example: int i = 010
Hexadecimal 0x Example: int i = 0x10 (0-9 AF 16)

        int num1 = 10;//十进制
        int num2 = 0b10;//0b 二进制
        int num3 = 010;//0 八进制
        int num4 = 0x10;//0x 十六进制
        System.out.println(num1);
        System.out.println(num2);
        System.out.println(num3);
        System.out.println(num4);

10
2
8
16

Floating point expansion:

How does the banking business represent (money)?

BigDecimal math tool class

Not used (float and double) finite, discrete, rounding error, approximate, close but not equal

It's best to avoid using floating point numbers to compare

        float a1 = 0.1f;
        double a2 = 0.1;
        float a3 = 0.1f;
        double a4 = 1.0/10;
        System.out.println(a1==a2);
        System.out.println(a1==a3);
        System.out.println(a2==a4);
        System.out.println(a3==a4);

false
true
true
false

Character expansion:

All characters are essentially numbers

Encoding Unicode can be used as task language text occupies 2 bytes 0-65536

(There will be a code table corresponding to the text, for example: 97=a 65=A)

Interval range: U0000-UFFFF

char a ='\u0061'; ('\ 'means escape)

system.out.println((int)c1); "(int) means forced conversion"

        char b1 = 'a';
        char b2 = '瓜';
        System.out.println(b1);
        System.out.println((int)b1);// 强制转换
        System.out.println(b2);
        System.out.println((int)b2);
        char b3 = '\u0055'; // Unicode编码
        char b4 = '\u0088';
        System.out.println(b3);
        System.out.println(b4);
a
9729916
U
ˆ

Escape character

\t tab

\n Newline

For example: ("hello\t"World)

        System.out.println("hello\tworld");//转义字符 制表符
        System.out.println("hello\nworld");//转义字符 换行
hello	world
hello
world
String c1 = new String("helloword" );
String c2 = new String("helloword" );
String c3 = "helloworld";
String c4 = "helloworld";
System.out.println(c1==c2);
System.out.println(c3==c4);
System.out.println(c1==c3);
//对象 从内存分析
false
true
false

Boolean expansion

boolean flag = true;

if(flag==true){} and if(flag){} are the same

The left is for novices

        boolean d1 = true;
        boolean d2 = false;
        if (d1==true){
    
    
            System.out.println("oh my god");
            if (d1){
    
    
                System.out.println("oh my god");
                if (d2==false){
    
    
                        System.out.println("apple");
oh my god
oh my god
apple

Less is More code should be concise and simple to read

Guess you like

Origin blog.csdn.net/SuperrWatermelon/article/details/112422133