Java Review - Data Types

type of data

Integer types: byte, short, int and long
Floating point types: float and double
Character types: char
Boolean type: boolean

insert image description here

1. Integer

insert image description here
The integer type of the Java language is int type by default. For example, 16 is expressed as an int type constant, not short or byte, and not long. The long type needs to add l (lowercase English letter) or L (uppercase English letter) after the value.

2. Floating point type

insert image description here

The floating-point type of Java language is double type by default, for example, 0.0 means double type constant, not float type. If you want to represent the float type, you need to add f or F after the value. The letter d or D can also be added after the double floating-point value to indicate that it is a double floating-point number.

3. Index representation

Numerical values ​​represented by exponents are often used in mathematical calculations. If the exponent is expressed in decimal, you need to use uppercase or lowercase e to indicate power, and e2 to indicate 102.
An example of a floating point number represented with a decimal exponent is as follows:

double myMoney = 3.36e2;
double interestRate = 1.56e-2;

Among them, 3.36e2 represents the square of 3.36×10, and 1.56e-2 represents the negative 2 power of 1.56×10.

4. Boolean

The keyword to declare a Boolean type in the Java language is boolean, which has only two values: true and false. If you try to assign them constants other than true and false, a type mismatch compile error occurs.

5. Type conversion

5.1 Automatic conversion

Automatic type conversion means that the conversion between types needs to be automatic, and no other means are required. The general principle is that a small range of data types can be automatically converted into a large range of data types, as shown in the figure, from left to right is automatic.
insert image description here
The char type is special, char is automatically converted to int, long, float and double, but byte
and short cannot be automatically converted to char, and char cannot be automatically converted to byte or short.

Automatic type conversion occurs not only during the assignment process, but also during mathematical calculations. In operations, the data type is often converted to the same type first, and then the calculation is performed. The calculation rules are shown in the table.
insert image description here

public static void main(String[] args){
    
    
        byte byteNum = 16;
        short shortNum = 16;
        int intNum = 16;
        long longNum = 16L;
        // byte类型转换为int类型
        intNum = byteNum;
        System.out.println("byte类型转换后的值:"+intNum);
        char charNum = '花';
        // char类型转换为int类型
        intNum = charNum;
        System.out.println("char类型转换后的值:"+intNum);

        // long类型转换为float类型
        float floatNum = longNum;
        // float类型转换为double类型
        double doubleNum = floatNum;
        //表达式计算后类型是double
        intNum = 16;
        double result = floatNum * intNum + doubleNum / shortNum;
        System.out.println("计算的值:"+result + "    类型为:"+getType(result));
        /*该表达式是由 4 个完全不同的数据类型组成,范围最大的是 double,所以在计算过程中
        它们先转换成 double,所以最后的结果是 double。*/
    }
    public static String getType(Object o){
    
     //获取变量类型方法
        return o.getClass().toString(); //使用对象的的getClass()方法
    }

output:

byte类型转换后的值:16
char类型转换后的值:33457
计算的值:257.0    类型:class java.lang.Double

5.2 Mandatory conversion

Mandatory type conversion is implemented by adding "(target type)" before the variable or constant.

int i = 10;
byte b = (byte) i;

double d = 10.24;
long ll = (long) d;// 10

Mandatory type conversion may result in loss of precision, data overflow.
When the three data types of byte/short/char are operated, they will be first promoted to int type and then calculated.
Data type conversion cannot occur for Boolean type

Guess you like

Origin blog.csdn.net/qq_43325582/article/details/118944951