Basic Java learning - basic data types

Associated blog: https://www.jianshu.com/p/f5ed637a59f9

Basic data type

1. Interpretation of Java program body

//Public means public
//Class represents a class
//Helloworld represents a class name
public class HelloWorld {
    /**  Class body
      *  public Indicates public
      *  static Indicates static
      *  void   Indicates null
      *  main   Indicates that the method name is main
      *  (String[] args) Is a formal parameter list of the main method
     */
    public static void main(String[] args) {//Represents the definition of an open static main method ps: program execution entry
        //Method body java statement
        System.out.println("Hello world! Java,I'm coming.");
    }
}

2. Basic data type

For the mutual conversion between basic data types, the conversion rules are as follows:
  • 1. The eight basic data types, except boolean type, and the remaining seven types can be converted to each other.

  • 2. The conversion from small capacity to large capacity is called automatic type conversion. The sorting of capacity is byte < short / char < int < long < float < double.

        PS: Any floating-point type, no matter how many bytes it takes, has a larger capacity than the integer type,
        char and short The number of types that can be represented is the same, but char You can take a larger positive integer.
    
  • 3. Converting large capacity into small capacity is called forced type conversion. The program can only be compiled by adding a forced type converter; However, accuracy may be lost in the operation stage, so use it with caution.

  • 4. When the face value of the whole number does not exceed the value range of byte, short and char, it can be directly assigned to variables of type byte, short and char.

  • 5. In the mixed operation of byte, short and char, convert them into int type before operation.

  • 6. The mixed operation of multiple data types shall be converted to the data type with the largest capacity before operation.

2.1 basic data type

  • Integer type: byte, short, int, long
  • Floating point type: float, double
  • boolean: boolean
  • Character type: char
keywordtypebyteValue rangeDefault value
byteByte type1-128 ~ 1270
shortShort 2-32768 ~ 327670
intinteger4-2147483648 ~ 21474836470
longLong integer8-9223372036854775808 ~ 92233720368547758080
floatSingle precision floating point4Approx. ± 3.40282347E + 38F (6-7 significant digits)0.0f
doubleDouble precision floating point8Approx. ± 1.79769313486231570E + 308 (15 significant digits)0.0d
booleanBoolean type1true/falsefalse
charcharacter20 ~ 65535'\u0000'
 1 byte = 8 bit  [One byte = 8 Bits] 1 Bit: 1 bit/0
 1 KB = 1024 Byte
 1 MB = 1024 KB
 1 GB = 1024 MB
 1 TB = 1024 GB
 
 byte Type maximum: the 7th power of 2 - 1,Namely: 127
 byte Type Min:-128[Bit operation]

2.2 member variable and local variable ~ ~ default value of basic data type

Code example:
public class HelloWorld {

    /*  Class body  */
    //Declare and define member variables
    //The default value of the eight basic data types is that everything is in line with 0.
    //Member variables are not assigned manually, and the system will assign values by default [local variables will not]
    //Variables still follow this syntax: they must be declared before assignment.
    //The static here must be added. Why not
    static int k = 1000;
    static int f;

    public static void main(String[] args) {//Represents the definition of an open static main method ps: program execution entry
         /*  Method body  */
        //Declare and define local variables
        int i=10;

        //java statement
        System.out.println("Hello world! Java,I'm coming.");
        System.out.println(i);
        System.out.println(f);

    }

}

2.3 basic data char type

Code example:
public class HelloWorld {

    /*  Class body  */

    public static void main(String[] args) {//Represents the definition of an open static main method ps: program execution entry
         /*  Method body  */
        //Declare and define local variables

        /* //The slash has the function of escape
         * \n Line feed
         * \t Tab
         * \' Normal single quotation mark
         * \\ Normal backslash
         * \" Normal double quotation mark
         * The backslash u is combined, and the next number is the unicode code of a text
         */
        char b= '\n';//Line feed
        System.out.println(b);

        char c= '\n';//Tabulation
        System.out.println(c);

        char k= '\\';
        System.out.println(k);

        char a = '\'';
        System.out.println(a);

        // Native 2A SCII. Native in jak Exe command, which can convert text into unicode encoded form
        // How do I use this command
        // Enter native2ascii on the command line, enter, and then enter the text, and then enter to obtain the unicode code
        char e = 'in';
        System.out.println(e);

        char ff = '\u0000';
        System.out.println(ff);

    }

}

2.3 basic data types int and long (forced type conversion)

  the "integer literal" in the java language is treated as an int type by default. To treat this "integer literal" as a long type, add 1/L after the "integer literal". It is recommended to use uppercase L.
There are three representations of integer literals in the java language:
  • The first method: decimal [is a default method]
  • The second method: octal [start with 0 when writing octal integer literal value]
  • The third method: hexadecimal [start with 0x when writing hexadecimal integer literal value]
Code example:
public class HelloWorld {
    public static void main(String[] args) {//Represents the definition of an open static main method ps: program execution entry
         /*  Method body  */
        //Declare and define local variables
        int a = 10;
        int b = 010;//Integer literals start with 0, followed by a string of digits in octal form
        int c = 0x10;//Integer literals start with 0x, followed by a string of digits in hexadecimal form

        //java statement
        System.out.println("Hello world! Java,I'm coming.");

        System.out.println(a); //10
        System.out.println(b); //8
        System.out.println(c); //16

        System.out.println(a + b + c); //34

        // 123 this integer literal is of type int
        // When i variable is declared, it is also of type int
        // 123 of type int is assigned to variable i of type int, and there is no type conversion

        int i = 123;
        System.out.println(i);

        // 456 integer literals are treated as int types and occupy 4 bytes
        // When declared, the x variable is of long type, occupying 8 bytes
        // The literal 456 of int type is assigned to the variable x of long type, and there is type conversion
        // Convert int type to long type
        // int type is small capacity and long type is large capacity
        // Small capacity can be automatically converted into large capacity, which is called automatic type conversion mechanism

        long x =456;
        System.out.println(x);

        // 2147483647 literal int type, occupying 4 bytes
        // y is a long type and takes up 8 bytes. Automatic type conversion
        long y = 2147483647;
        System.out.println(y);

        // Compilation error: too large integer: 2147483648
        // 2147483648 is treated as 4 bytes of int type, but this literal is outside the range of int type
        // long z = 2147483648;
        // System.out.println(z);

        //Resolve errors
        // 2147483648 literals are treated as long types as soon as they come up, and L is added after the literals
        // 2147483648L is an 8-byte long type
        // z is a long variable, and the following program does not have type conversion
        long z = 2147483648L;
        System.out.println(z);

    }

}

2.3 basic data types byte,short,char (forced type conversion)

  when an integer literal does not exceed the value range of byte, short and char, this literal can be directly assigned to variables of type byte, short and char. This mechanism sun allows, in order to facilitate the programmer's programming.
Code example:
public class HelloWorld {
    public static void main(String[] args) {//Represents the definition of an open static main method ps: program execution entry
         /*  Method body  */

        //java statement
        System.out.println("Hello world! Java,I'm coming.");
        
        // long m =100L, no type conversion, direct assignment
        long m =100L;

        // Assignment: n = m
        // It will compile and report errors. Large capacity cannot be directly assigned to small capacity
        // int n = m;

        // Conversion from large capacity to small capacity requires forced type conversion
        // "Mandatory type changer" is required for mandatory type conversion
        // After adding the cast character, it can be compiled, but the runtime may lose precision
        // Therefore, the forced type replacement should be used with caution, because the loss of accuracy may be serious

        // Strong rotation principle: (binary)
           // Original data: 00000000 00000000 00000000 00000000 00000000 00000000 01100100
           // Data after forced conversion: 00000000 00000000 01100100
           // Cut off the binary on the left [all data forced conversion is completed in this way]
        int n = (int)m;
        System.out.println(n);

        //give an example:
        //Console e output: - 2147483648
        long k = 2147483648L;
        int e = (int)k;
        System.out.println(e);// The accuracy loss is serious, and the result is negative [- 2147483648]
        
        // Analyze whether the program can be compiled?
        // Understanding: cannot compile without cast
        // 50 is the literal value of int type, h is the variable of byte type, and it is the conversion from large int to small byte
        // Conversion from large capacity to small capacity requires forced type conversion

        // The actual editor passed
        // In the java language, when an integer literal does not exceed the byte value range, the literal can be directly assigned to a byte type variable
        // byte h = 128;// Compilation error
        byte h = 50; //Compile through
        System.out.println(h);
        
        //short s = 32767;// adopt
        //short s1 = 32768;// Compilation error
        //char cc = 65535;// adopt
        //cc = 65536;// Compilation error
        // When an integer literal does not exceed the value range of byte, short and char, this literal can be directly assigned to variables of type byte, short and char
        // This mechanism is allowed by sun in order to facilitate the programmer's programming

    }

}

2.4 basic data double,float (forced type conversion)

  in the java language, all floating-point literal values [example: 3.0] are treated as double by default. If you want to treat this literal as float, you need to add F/f after the literal value.
be careful:
  • double and float are approximate values when they are stored in binary storage inside the computer
  • In the real world, some numbers are infinite cycles, for example: 3.333333
  • The resources of the computer are limited. Using limited resources to store unlimited data can only store approximate values.
Code example:
public class HelloWorld {
    public static void main(String[] args) {//Represents the definition of an open static main method ps: program execution entry
         /*  Method body  */

        //java statement
        System.out.println("Hello world! Java,I'm coming.");

        // 3.0 is a literal of type double
        // d is a variable of double type, which is directly assigned without type conversion
        double d = 3.0;

        // The face value of double is of type 1.5
        // f is a variable of type float
        // The conversion from large capacity to small capacity needs to add a forced type converter. All the following programs have compilation errors
        // float f =5.1;

        // Solution 1: mandatory type replacement
        float f = (float)5.1;
        // Solution 2: no type conversion
              f = 5.1f;
    }
}

2.4 basic data boolean

  • boolean type has only two values in the java language: true and false, and there are no other values.
  • In the underlying storage, the boolean type occupies one byte. In the actual storage, the false underlying is 0 and the true underlying is 1.
  • Boolean type is very important in practical development and is often used in logical operation and conditional control statements.
Code example:
public class HelloWorld {
    public static void main(String[] args) {//Represents the definition of an open static main method ps: program execution entry
        /*  Method body  */

        //java statement
        System.out.println("Hello world! Java,I'm coming.");

        //Compilation error, incompatible type
        // boolean loginSuccess = 1;

        boolean loginSuccess = true;
        //if statement
        if (loginSuccess){//true
            System.out.println("how are you");
        }else {//false
            System.out.println("Hello");
        }
    }
}

Associated blog: https://www.jianshu.com/p/f5ed637a59f9

Basic data type

1. Interpretation of Java program body

//Public means public
//Class represents a class
//Helloworld represents a class name
public class HelloWorld {
    /**  Class body
      *  public Indicates public
      *  static Indicates static
      *  void   Indicates null
      *  main   Indicates that the method name is main
      *  (String[] args) Is a formal parameter list of the main method
     */
    public static void main(String[] args) {//Represents the definition of an open static main method ps: program execution entry
        //Method body java statement
        System.out.println("Hello world! Java,I'm coming.");
    }
}

2. Basic data type

For the mutual conversion between basic data types, the conversion rules are as follows:
  • 1. The eight basic data types, except boolean type, and the remaining seven types can be converted to each other.

  • 2. The conversion from small capacity to large capacity is called automatic type conversion. The sorting of capacity is byte < short / char < int < long < float < double.

        PS: Any floating-point type, no matter how many bytes it takes, has a larger capacity than the integer type,
        char and short The number of types that can be represented is the same, but char You can take a larger positive integer.
    
  • 3. Converting large capacity into small capacity is called forced type conversion. The program can only be compiled by adding a forced type converter; However, accuracy may be lost in the operation stage, so use it with caution.

  • 4. When the face value of the whole number does not exceed the value range of byte, short and char, it can be directly assigned to variables of type byte, short and char.

  • 5. In the mixed operation of byte, short and char, convert them into int type before operation.

  • 6. The mixed operation of multiple data types shall be converted to the data type with the largest capacity before operation.

2.1 basic data type

  • Integer type: byte, short, int, long
  • Floating point type: float, double
  • boolean: boolean
  • Character type: char
keywordtypebyteValue rangeDefault value
byteByte type1-128 ~ 1270
shortShort 2-32768 ~ 327670
intinteger4-2147483648 ~ 21474836470
longLong integer8-9223372036854775808 ~ 92233720368547758080
floatSingle precision floating point4Approx. ± 3.40282347E + 38F (6-7 significant digits)0.0f
doubleDouble precision floating point8Approx. ± 1.79769313486231570E + 308 (15 significant digits)0.0d
booleanBoolean type1true/falsefalse
charcharacter20 ~ 65535'\u0000'
 1 byte = 8 bit  [One byte = 8 Bits] 1 Bit: 1 bit/0
 1 KB = 1024 Byte
 1 MB = 1024 KB
 1 GB = 1024 MB
 1 TB = 1024 GB
 
 byte Type maximum: the 7th power of 2 - 1,Namely: 127
 byte Type Min:-128[Bit operation]

2.2 member variable and local variable ~ ~ default value of basic data type

Code example:
public class HelloWorld {

    /*  Class body  */
    //Declare and define member variables
    //The default value of the eight basic data types is that everything is in line with 0.
    //Member variables are not assigned manually, and the system will assign values by default [local variables will not]
    //Variables still follow this syntax: they must be declared before assignment.
    //The static here must be added. Why not
    static int k = 1000;
    static int f;

    public static void main(String[] args) {//Represents the definition of an open static main method ps: program execution entry
         /*  Method body  */
        //Declare and define local variables
        int i=10;

        //java statement
        System.out.println("Hello world! Java,I'm coming.");
        System.out.println(i);
        System.out.println(f);

    }

}

2.3 basic data char type

Code example:
public class HelloWorld {

    /*  Class body  */

    public static void main(String[] args) {//Represents the definition of an open static main method ps: program execution entry
         /*  Method body  */
        //Declare and define local variables

        /* //The slash has the function of escape
         * \n Line feed
         * \t Tab
         * \' Normal single quotation mark
         * \\ Normal backslash
         * \" Normal double quotation mark
         * The backslash u is combined, and the next number is the unicode code of a text
         */
        char b= '\n';//Line feed
        System.out.println(b);

        char c= '\n';//Tabulation
        System.out.println(c);

        char k= '\\';
        System.out.println(k);

        char a = '\'';
        System.out.println(a);

        // Native 2A SCII. Native in jak Exe command, which can convert text into unicode encoded form
        // How do I use this command
        // Enter native2ascii on the command line, enter, and then enter the text, and then enter to obtain the unicode code
        char e = 'in';
        System.out.println(e);

        char ff = '\u0000';
        System.out.println(ff);

    }

}

2.3 basic data types int and long (forced type conversion)

  the "integer literal" in the java language is treated as an int type by default. To treat this "integer literal" as a long type, add 1/L after the "integer literal". It is recommended to use uppercase L.
There are three representations of integer literals in the java language:
  • The first method: decimal [is a default method]
  • The second method: octal [start with 0 when writing octal integer literal value]
  • The third method: hexadecimal [start with 0x when writing hexadecimal integer literal value]
Code example:
public class HelloWorld {
    public static void main(String[] args) {//Represents the definition of an open static main method ps: program execution entry
         /*  Method body  */
        //Declare and define local variables
        int a = 10;
        int b = 010;//Integer literals start with 0, followed by a string of digits in octal form
        int c = 0x10;//Integer literals start with 0x, followed by a string of digits in hexadecimal form

        //java statement
        System.out.println("Hello world! Java,I'm coming.");

        System.out.println(a); //10
        System.out.println(b); //8
        System.out.println(c); //16

        System.out.println(a + b + c); //34

        // 123 this integer literal is of type int
        // When i variable is declared, it is also of type int
        // 123 of type int is assigned to variable i of type int, and there is no type conversion

        int i = 123;
        System.out.println(i);

        // 456 integer literals are treated as int types and occupy 4 bytes
        // When declared, the x variable is of long type, occupying 8 bytes
        // The literal 456 of int type is assigned to the variable x of long type, and there is type conversion
        // Convert int type to long type
        // int type is small capacity and long type is large capacity
        // Small capacity can be automatically converted into large capacity, which is called automatic type conversion mechanism

        long x =456;
        System.out.println(x);

        // 2147483647 literal int type, occupying 4 bytes
        // y is a long type and takes up 8 bytes. Automatic type conversion
        long y = 2147483647;
        System.out.println(y);

        // Compilation error: too large integer: 2147483648
        // 2147483648 is treated as 4 bytes of int type, but this literal is outside the range of int type
        // long z = 2147483648;
        // System.out.println(z);

        //Resolve errors
        // 2147483648 literals are treated as long types as soon as they come up, and L is added after the literals
        // 2147483648L is an 8-byte long type
        // z is a long variable, and the following program does not have type conversion
        long z = 2147483648L;
        System.out.println(z);

    }

}

2.3 basic data types byte,short,char (forced type conversion)

  when an integer literal does not exceed the value range of byte, short and char, this literal can be directly assigned to variables of type byte, short and char. This mechanism sun allows, in order to facilitate the programmer's programming.
Code example:
public class HelloWorld {
    public static void main(String[] args) {//Represents the definition of an open static main method ps: program execution entry
         /*  Method body  */

        //java statement
        System.out.println("Hello world! Java,I'm coming.");
        
        // long m =100L, no type conversion, direct assignment
        long m =100L;

        // Assignment: n = m
        // It will compile and report errors. Large capacity cannot be directly assigned to small capacity
        // int n = m;

        // Conversion from large capacity to small capacity requires forced type conversion
        // "Mandatory type changer" is required for mandatory type conversion
        // After adding the cast character, it can be compiled, but the runtime may lose precision
        // Therefore, the forced type replacement should be used with caution, because the loss of accuracy may be serious

        // Strong rotation principle: (binary)
           // Original data: 00000000 00000000 00000000 00000000 00000000 00000000 01100100
           // Data after forced conversion: 00000000 00000000 01100100
           // Cut off the binary on the left [all data forced conversion is completed in this way]
        int n = (int)m;
        System.out.println(n);

        //give an example:
        //Console e output: - 2147483648
        long k = 2147483648L;
        int e = (int)k;
        System.out.println(e);// The accuracy loss is serious, and the result is negative [- 2147483648]
        
        // Analyze whether the program can be compiled?
        // Understanding: cannot compile without cast
        // 50 is the literal value of int type, h is the variable of byte type, and it is the conversion from large int to small byte
        // Conversion from large capacity to small capacity requires forced type conversion

        // The actual editor passed
        // In the java language, when an integer literal does not exceed the byte value range, the literal can be directly assigned to a byte type variable
        // byte h = 128;// Compilation error
        byte h = 50; //Compile through
        System.out.println(h);
        
        //short s = 32767;// adopt
        //short s1 = 32768;// Compilation error
        //char cc = 65535;// adopt
        //cc = 65536;// Compilation error
        // When an integer literal does not exceed the value range of byte, short and char, this literal can be directly assigned to variables of type byte, short and char
        // This mechanism is allowed by sun in order to facilitate the programmer's programming

    }

}

2.4 basic data double,float (forced type conversion)

  in the java language, all floating-point literal values [example: 3.0] are treated as double by default. If you want to treat this literal as float, you need to add F/f after the literal value.
be careful:
  • double and float are approximate values when they are stored in binary storage inside the computer
  • In the real world, some numbers are infinite cycles, for example: 3.333333
  • The resources of the computer are limited. Using limited resources to store unlimited data can only store approximate values.
Code example:
public class HelloWorld {
    public static void main(String[] args) {//Represents the definition of an open static main method ps: program execution entry
         /*  Method body  */

        //java statement
        System.out.println("Hello world! Java,I'm coming.");

        // 3.0 is a literal of type double
        // d is a variable of double type, which is directly assigned without type conversion
        double d = 3.0;

        // The face value of double is of type 1.5
        // f is a variable of type float
        // The conversion from large capacity to small capacity needs to add a forced type converter. All the following programs have compilation errors
        // float f =5.1;

        // Solution 1: mandatory type replacement
        float f = (float)5.1;
        // Solution 2: no type conversion
              f = 5.1f;
    }
}

2.4 basic data boolean

  • boolean type has only two values in the java language: true and false, and there are no other values.
  • In the underlying storage, the boolean type occupies one byte. In the actual storage, the false underlying is 0 and the true underlying is 1.
  • Boolean type is very important in practical development and is often used in logical operation and conditional control statements.
Code example:
public class HelloWorld {
    public static void main(String[] args) {//Represents the definition of an open static main method ps: program execution entry
        /*  Method body  */

        //java statement
        System.out.println("Hello world! Java,I'm coming.");

        //Compilation error, incompatible type
        // boolean loginSuccess = 1;

        boolean loginSuccess = true;
        //if statement
        if (loginSuccess){//true
            System.out.println("how are you");
        }else {//false
            System.out.println("Hello");
        }
    }
}

Keywords: Java

Added by bickyz on Tue, 08 Feb 2022 09:28:11 +0200