Java Basic Explanation of Basic Data Types

  • 2021-10-15 10:33:48
  • OfStack

1. Integer type

Mainly expand the integer types of different radicals under 1
Binary, octal, decimal, 106-ary
* Binary: 0B (Digital Zero + B) 0b (Digital Zero + b)
* Octal: 0 (beginning with zero)
* Decimal: Normal writing is decimal
* 106-ary: 0X (digital zero + X) 0x (digital zero + x) A-F for 10-15

1.1 Test code


 		/**
         *  Binary system   : 
         *    2 Binary system   :  0B (Number zero +B )   0b (Number zero +b ) 
         *    8 Binary system   : 0( Number zero beginning )
         *    10 Binary system   : Writing normally is 10 Binary system 
         *    106 Binary system   :  0X  (Number zero +X ) 0x   (Number zero +x ) 
         *                A-F  Representative  10-15
         *
         */
        int i2_1 = 0b10; // 2 Binary  2
        int i2_2 = 0B10;

        int i8 = 010; // 8 Binary  8

        int i10 = 10; // 10 Binary  10

        int i16_1 = 0X1B; // 106 Binary  16+11 = 27
        int i16_2 = 0x1B;

        System.out.println("2 Binary system: i2_1 = "+i2_1);
        System.out.println("2 Binary system: i2_2 = "+i2_2);
        System.out.println("8 Binary system: i8 = "+i8);
        System.out.println("10 Binary system: i10 = "+i10);
        System.out.println("106 Binary system: i16_1 = "+i16_1);
        System.out.println("106 Binary system: i16_2 = "+i16_2);
        

1.2 Running results

Binary: i2_1 = 2
Binary: i2_2 = 2
Octal: i8 = 8
Decimal: i10 = 10
Binary 106: i16_1 = 27
Binary 106: i16_2 = 27

2. Floating-point

"Special Note" Try not to use floating-point data for comparison in your program, or you may have very serious problems


    Weird comparison of floating-point numbers 
      1. float  And  double  The data of cannot be compared directly   : 
         float Type : Finite discrete data, rounding error, so it is not equal to double Same data 
       2. float  And  float  The data comparison of cannot be used directly  ==
     
        [Conclusion]   :   When it comes to   When comparing floating-point numbers: 
                   Train of thought 1  :   Do a bad job   Less than a certain number 
                   Train of thought 2  :   Use   Large number type  BigDecimal

2.1 Test code (critical)


		/**
         *  Weird comparison of floating-point numbers 
         *  1. float  And  double  The data of cannot be compared directly   : 
         *     float Type : Finite discrete data, rounding error, so it is not equal to double Same data 
         *  2. float  And  float  The data comparison of cannot be used directly  ==
         *
         *   [Conclusion]   :   When it comes to   When comparing floating-point numbers: 
         *              Train of thought 1  :   Do a bad job   Less than a certain number 
         *              Train of thought 2  :   Use   Large number type  BigDecimal
         */
        float f1 = 1.56f;
        double f2 = 1.56;
        System.out.println("f1 = "+f1);
        System.out.println("f2 = "+f2);
        System.out.println(" f1  Is it related to  f2  Is equal to the value of   :   "+(f1 == f2));

        float d1 = 1231238142342342342909f;
        float d2 = d1 +1;
        System.out.println("d1 = "+d1);
        System.out.println("d2 = "+d2);
        System.out.println("d1  Is it related to  d2  Is equal to the value of   :  "+(d1 == d2));
        

2.2 Operation Results

f1 = 1.56
f2 = 1.56
Whether f1 is equal to f2: false
d1 = 1.2312381E21
d2 = 1.2312381E21
Whether d1 is equal to d2: true

3. Characters are numbers in nature

Data of character type, which is essentially 1 number
The characters correspond to the numbers in the Unicode encoding table
You can write U0000 UFFFF directly (U is followed by a 106-ary number)
"Special Character": Escape Character ***

3.1 Test code


   		char c1 = 'a';
        char c2 = ' Countries ';
        char c3 = '\u0065'; //  106 Binary system  65  Correspondence  unicode  In the coding table   Lowercase letter  e
        System.out.println("c1 = " +c1+" ; (int)c1 = "+(int)c1);
        System.out.println("c2 = " +c2+" ; (int)c2 = "+(int)c2);
        System.out.println("c3 = "+c3);
        

3.2 Operational Results

c1 = a ; (int)c1 = 97
c2 = country; (int) c2 = 22269
c3 = e

4. String comparison

[Note] The string String is not a basic data type!
The comparison here is mainly to experience 1 值 的比较 And 对象地址的比较

4.1 Test code


		String s1 = new String("Hello World!");
        String s2 = new String("Hello World!");

        String s3 = "Hello World!";
        String s4 = "Hello World!";

        System.out.println("s1  Is it related to  s2  Equality   ?   :  "+(s1 == s2)); // false  Comparison of objects 
        System.out.println("s3  Is it related to  s4  Equality   ?   :  "+(s3 == s4)); // true  Comparison of values 

4.2 Running Results

Is s1 equal to s2? : false
Is s3 equal to s4? : true


Related articles: