Basic data type

Posted by chet23 on Thu, 24 Feb 2022 16:21:30 +0100

Integer type (byte, short, int, long)

Floating point type (float, double)

Character char

boolean

1. Integer type

Integer type is used to store integer (no decimal part) values, which can be positive or negative. Integer data can be divided into byte, short, int and long according to its memory size. Different data types have different value ranges, as shown in the table:

Data typeMemory space (8 bits equals 1 byte)Value range
         byte8 bits

                                  -128~127

        short16 bit                              -32768~32767
          int32 bit                    -2147483648~2147483647
         long64 bit  -9223372036854775808~9223372036854775807

(1) Define int type variables with the following codes:

public class JavaDemo {
    public static void main(String[] args) {
        //int variable name = constant 
        int x = 10;
        //The integer type is int, the variable name is x, and the constant is 10
        System.out.println(x);
        //Output int variable x
        System.out.println(x * x);
        //Int variable * int variable = int data
    }
}

Code running result:

10
100

(2) Any data type has its value range. If it exceeds its value range, there will be circular problems. These problems are called "data overflow" in Java.

Example: data overflow caused by int data type.

public class JavaDemo {
    public static void main(String[] args) {
        int max = Integer.MAX_VALUE;
        //Gets the maximum value of int
        int min = Integer.MIN_VALUE;
        //Gets the minimum value of int
        System.out.println("int The maximum value of is:" + max);
        System.out.println("int The minimum value of is:" + min);
        System.out.println("=====The following is int Data overflow=====");
        System.out.println("int Maximum value of+1 The results are:" + (max + 1));
        //MAX + 1 = min
        System.out.println("int Maximum value of+2 The results are:" + (max + 2));
        //Maximum + 2 = secondary minimum
        System.out.println("int Minimum value of-1 The results are:" + (min - 1));
        //Min - 1 = max
        System.out.println("int Minimum value of-2 The results are:" + (min - 2));
        //Minimum - 2 = secondary maximum
    }
}

Code running result:

int The maximum value of is 2147483647
int The minimum value of is:-2147483648
=====The following is int Data overflow=====
int Maximum value of+1 The results are:-2147483648
int Maximum value of+2 The results are:-2147483647
int Minimum value of-1 The result is: 2147483647
int Minimum value of-2 The result is: 2147483646

2. Floating point type

Floating point types represent numbers with fractional parts. Floating point types in Java language are divided into single precision floating point type (float) and double precision floating point type (double). They also have different value ranges, as shown in the table:

Data typeMemory spaceValue range
            float32 bit                     1.4E-45~3.4028235E38
           double64 bit            4.9E-324~1.7976931348623157E308

Note: 1.4E-45 means: 1.4 times 10 to the - 45th power.

(1) Define double and float variables with the following code:

public class JavaDemo {
    public static void main(String[] args) {
        double x = 10.2;
        float y = 10F;
        double result = x * y;
        //double type * int type = double Type
        System.out.println(result);
    }
}

Code running result:

102.0

Note: decimal is regarded as double by default. If float decimal is used, f or F should be added after the decimal.

3. Character type

The character type (char) is used to store a single character and occupies 16 bits (two bytes) of memory space. When defining a character type variable, it should be expressed in single quotation marks. For example,'s' represents a character, while "s" represents a string. Although there is only one character, it still represents a string rather than a character due to the use of double quotation marks. (in addition, the Java language uses unicode encoding, which can store 65526 characters; therefore, the characters in Java can process the languages of almost all countries)

(1) Define a character variable with the following code:

public class Test {
    public static void main(String[] args) {
        char ch = 'A';
        System.out.println(ch);
    }
}

Code running result:

A

(2) In any programming language, characters can be converted to and from int, and Java language is no exception.

public class Test {
    public static void main(String[] args) {
        char ch = 'B';
        int num = ch;
        System.out.println(num);
    }
}

Code running result:

66

(3) Note: the encoding of upper and lower case letters has a range.

Capital letter range: 'A' (65) ~ 'Z' (90); The result of A compilation is 65, and the result of Z compilation is 90

Lowercase letter range: 'a' (97) ~ 'z' (122); The result of a compilation is 97, and the result of z compilation is 122

Through the coding range, it can be found that there is a difference of 32 numbers between upper and lower case letters, so the conversion of upper and lower case letters can be realized. The program code is as follows:

public class Test {
    public static void main(String[] args) {
        char c = 'x';
        int num = c;
        num -= 32;
        // num -= 32 is equivalent to num = nun - 32
        System.out.println((char) num);
    }
}

Code running result:

X

4. Boolean type

Boolean type, also known as logical type, defines boolean type variables through the keyword Boolean. There are only two values: true and false, which represent "true" and "false" in Boolean logic respectively. Boolean values cannot be converted to integer types. Boolean types are often used in process control.

example:

public class Dream {
    public static void main(String[] args) {
        int x = 30;
        int y = 40;
        boolean max = x > y;
        //Judge the size relationship between X and Y. if x is greater than y, output true; otherwise, output false
        System.out.println(max);
    }
}

Code running result:

false

Topics: Java Back-end