03_Java basic data type

Keywords: Java

Like other languages, a variable is a memory space. Creating (declaring) a variable is actually applying for a memory space in memory to store data.
The computer memory management system allocates storage space for variables according to the type of variables, and the allocated space can only be used to store this type of data.
Therefore, you can store integers, decimals, and characters in memory by defining different types of variables.
Like JavaScript, java has two major data types:

  • Built in data type
  • Reference data type

Built in data type

java has 8 basic types. It includes six numeric types (four integers, two floating-point types), character types and Boolean types.

  • byte

    • byte type is an 8-bit, signed integer represented by binary complement
    • The minimum value is - 128 (- 2 ^ 7) and the maximum value is 127 (2 ^ 7-1);
    • The default value is 0;
    • Byte type is used to save space in large arrays, mainly replacing integers, because byte variables occupy only a quarter of the space of int type;
      example:
    byte a = 100; byte b = -50;
    
  • short

    • The short type is an integer represented by a 16 bit signed binary complement;
    • The minimum value is - 32768 (- 2 ^ 15);
    • The maximum value is 32767 (2 ^ 15 - 1);
    • The short data type can also save space like byte. A short variable is one-half of the space occupied by int variables;
    • The default value is 0;
      example:
    short s = 1000,short r = -20000. 
    

    int:

  • The int data type is a 32-bit, signed integer represented by binary complement;

    • The minimum value is - 2147483648 (- 2 ^ 31);
    • The maximum value is 2147483647 (2 ^ 31 - 1);
    • Generally, integer variables are of type int by default;
    • The default value is 0;
      Example: int a = 100000, int b = -200000.
  • long:

    • long data type is a 64 bit signed integer represented by binary complement;
    • The minimum value is - 9223372036854775808 (- 2 ^ 63);
    • The maximum value is 9223372036854775807 (2 ^ 63 - 1);
    • This type is mainly used in systems that require relatively large integers;
    • The default value is 0L;
      Example: long a = 100000L, Long b = -200000L.
      "L" is not case sensitive in theory, but if it is written as "L", it is easy to be confused with the number "1" and difficult to distinguish. So it's best to capitalize.
  • float:

    • The float data type is a single precision, 32-bit, IEEE 754 compliant floating-point number;
    • float can save memory space when storing large floating-point arrays;
    • The default value is 0.0f;
    • Floating point numbers cannot be used to represent precise values, such as currency;
      Example: float f1 = 234.5f.
  • double:

    • The double data type is a double precision, 64 bit, IEEE 754 compliant floating-point number;
    • The default type of floating-point number is double;
    • The double type also cannot represent an exact value, such as currency;
    • The default value is 0.0d;
      example:
    double   d1  = 7D ;
    double   d2  = 7.;
    double   d3  =  8.0;
    double   d4  =  8.D;
    double   d5  =  12.9867;
    

    7 is an int literal, while 7D, 7. And 8.0 are double literals.

  • boolean:

    • boolean data type represents one bit information;
    • There are only two values: true and false;
    • This type is only used as a flag to record true/false;
    • The default value is false;
      example:
    boolean one = true. 
    
  • char:

    • The char type is a single 16 bit Unicode character;
    • The minimum value is \ u0000 (the decimal equivalent is 0);
    • The maximum value is \ uffff (i.e. 65535);
    • char data type can store any character;
      example:
    char letter = 'A';. 
    
Note:

For the value range of basic types of digital types, there is no need to force memory. java has defined their values as static attributes of the corresponding packaging class in the form of constants. You can directly access the corresponding attributes:

public class DataType {
    public static void main(String[] args) {
        // byte
        System.out.println("Basic type: byte Binary digits:" + Byte.SIZE);  // 8
        System.out.println("Packaging: java.lang.Byte");
        System.out.println("Minimum: Byte.MIN_VALUE=" + Byte.MIN_VALUE);  // -128
        System.out.println("Maximum: Byte.MAX_VALUE=" + Byte.MAX_VALUE);  // 127
        System.out.println();

        // short
        System.out.println("Basic type: short Binary digits:" + Short.SIZE);  // 16
        System.out.println("Packaging: java.lang.Short");
        System.out.println("Minimum: Short.MIN_VALUE=" + Short.MIN_VALUE);  // -32768
        System.out.println("Maximum: Short.MAX_VALUE=" + Short.MAX_VALUE);  // 32767
        System.out.println();

        // int
        System.out.println("Basic type: int Binary digits:" + Integer.SIZE);  // 32
        System.out.println("Packaging: java.lang.Integer");
        System.out.println("Minimum: Integer.MIN_VALUE=" + Integer.MIN_VALUE);  // -2147483648
        System.out.println("Maximum: Integer.MAX_VALUE=" + Integer.MAX_VALUE);  // 2147483647
        System.out.println();

        // long
        System.out.println("Basic type: long Binary digits:" + Long.SIZE);  // 64
        System.out.println("Packaging: java.lang.Long");
        System.out.println("Minimum: Long.MIN_VALUE=" + Long.MIN_VALUE);  // -9223372036854775808
        System.out.println("Maximum: Long.MAX_VALUE=" + Long.MAX_VALUE);  // -9223372036854775807
        System.out.println();

        // float
        System.out.println("Basic type: float Binary digits:" + Float.SIZE);  // 32
        System.out.println("Packaging: java.lang.Float");
        System.out.println("Minimum: Float.MIN_VALUE=" + Float.MIN_VALUE);  // 1.4E-45
        System.out.println("Maximum: Float.MAX_VALUE=" + Float.MAX_VALUE);  // 3.4028235E38
        System.out.println();

        // double
        System.out.println("Basic type: double Binary digits:" + Double.SIZE);  // 64
        System.out.println("Packaging: java.lang.Double");
        System.out.println("Minimum: Double.MIN_VALUE=" + Double.MIN_VALUE);  // 4.9E-324
        System.out.println("Maximum: Double.MAX_VALUE=" + Double.MAX_VALUE);  // 1.7976931348623157E308
        System.out.println();

        // char
        System.out.println("Basic type: char Binary digits:" + Character.SIZE);  // 16
        System.out.println("Packaging: java.lang.Character");
        // Set character.min as a numeric value instead of a character_ Value output to console
        System.out.println("Minimum: Character.MIN_VALUE="
                + (int) Character.MIN_VALUE);  // 0
        // Convert character.max to numeric instead of character_ Value output to console
        System.out.println("Maximum: Character.MAX_VALUE="
                + (int) Character.MAX_VALUE);  // 65535
    }
}
Default value for type

Each built-in type has a corresponding default value, as shown in the following table:

data typeDefault value
byte0
short0
int0
long0L
float0.0f
double0.0d
char'u0000'
String (or other object)null
booleanfalse

Reference data type

  • Reference data types in Java and JavaScript are similar, and reference data type variables are very similar to pointers in c/c + +. Reference type variables refer to an object in memory. These variables are specified as specific types when declared. Once a variable is declared, the type cannot be changed.
  • Both objects and arrays are reference types.
  • The default value for all reference types is null.
  • A reference variable can be used to reference any compatible type.

java constant

Constants cannot be modified at run time.
In java, the final keyword is used to modify constants. The declared methods and variables are similar:

public class RefType {
    public static void main(String[] args) {
        final int constantNumber = 10;
        // constantNumber = 100; //  report errors
        System.out.println(constantNumber);
    }
}

In programming specifications, constants are generally capitalized:

final int CONSTANT_NUMBER = 10;

Automatic type conversion

Conversion rules

Integer, constant and character data can be mixed. In the operation, different types of data will be converted to the same type before operation.
The conversion priority is as follows

low ------------------> high
byte,short,char—> int —> long—> float —> double

Data type conversion must meet the following rules:

  1. Cannot type convert boolean type.
  2. You cannot convert an object type to an object of an unrelated class.
  3. Cast must be used when converting a large capacity type to a small capacity type.
  4. Overflow or loss of precision may occur during conversion.
    give an example:
public class RefType {
  public static void main(String[] args) {
      int i = 128;
      byte b = (byte) i;
      System.out.println(b); // -128
  }
}

In this example, because the byte type is 8-bit and the maximum value is 127, when the 32-bit int type 127 is converted to byte type, 128 is greater than the maximum value 127 of byte type, resulting in value overflow. 5. The conversion from floating-point number to integer is obtained by discarding decimals rather than rounding:
give an example:

public class RefType {
  public static void main(String[] args) {
      double f = 0.723;
      int b = (int) f;
      System.out.println(b); // 0
      f = 5.658;
      b = (int) f;
      System.out.println(b); // 5
      f = 6.018;
      b = (int) f;
      System.out.println(b); // 6
  }
}
Automatic conversion of types

The automatic conversion of data type must meet the requirement that the number of bits of the data type before conversion is lower than that after conversion, that is, the data type short of 16 can be automatically converted to 32 data type int, otherwise, the conversion of int to short is not allowed.

public class RefType {
  public static void main(String[] args) {
      char letter = 's';
      int i = letter;
      System.out.println(i); // 115
      short s = 10;
      i = s;
      System.out.println(i); // 10
      int j = 11;
      letter = j; // Error: Type mismatch: cannot convert from int to char
      System.out.println(letter); // 10
  }
}

Cast type

Requirement: the converted data type must be compatible.
give an example:

public class RefType {
    public static void main(String[] args) {
        int i = 123;
        // byte b = i; //  Error: Type mismatch: cannot convert from int to byte
        long l = i; // Because the long type is 64 bits and the int type is 32 bits, the int type can be automatically converted to the long type
        System.out.println(l); // 123
        byte b = (byte) i; // Cast to byte type
        System.out.println(b); // 123
    }
}

Posted by ryanschefke on Wed, 13 Oct 2021 23:00:03 -0700