Fundamentals of Java (01): basic data types, core point sorting

1、 Basic type

1. Basic type

Instead of using new, declare a variable that is not passed by reference, and the value of the variable is directly placed on the stack. The size does not change with the running environment, which is more efficient. Reference objects created using new are stored in the heap.

2. Basic information

Basic types include the following: byte, short, int, long, float, double, Boolean and char. You can view the range size through related methods.

public class IntType01 {
    public static void main(String[] args) {
        System.out.println("进制位数:"+Integer.SIZE);
        System.out.println("最小值:"+Integer.MIN_VALUE);
        System.out.println("最大值:"+Integer.MAX_VALUE);
        System.out.println("进制位数:"+Double.SIZE);
        System.out.println("最小值:"+Double.MIN_VALUE);
        System.out.println("最大值:"+Double.MAX_VALUE);
    }
}

2、 Case usage

1. Type conversion

Automatic conversion: data types with a small range can be automatically converted to data types with a large range.

Cast: converts one data type to another.

Type promotion: there are different data types in expression operation, and the type will be automatically promoted to a wide range.

public class IntType02 {
    public static void main(String[] args) {
        // 自动转换
        int i = 112 ;
        long j = i ;
        System.out.println(j);
        // 强制转换
        double d = 13.14 ;
        int f = (int)d;
        System.out.println(f);
        // 类型提升
        long r = i * j ;
        System.out.println(r);
    }
}

Note: the most important problem in type conversion is the range size.

2. Wrapper type

The basic data type does not conform to the object-oriented idea, so the wrapper type appears, and the wrapper adds more attributes and methods. The automatic wrapper function can convert the basic type to the wrapper type. Java provides an encapsulated class for each primitive type, integer, double, long, Boolean, byte, and so on.

public class IntType03 {
    public static void main(String[] args) {
        Integer int1 = null ;
        Double dou1 = 13.14 ;
        Long lon1 = 123L ;
    }
}

The default value of integer variable is null, indicating that integer can distinguish the difference between unassigned and 0, such as the difference between getting 0 in the exam and not taking the exam.

3. Character type

Char type variables are used to store Unicode encoded characters. The Unicode character set contains Chinese characters.

public class IntType04 {
    public static void main(String[] args) {
        char cha1 = '知';
        System.out.println(cha1);
    }
}

Note: there may be special rare words that are not included in the Unicode encoded character set.

4. Assignment and operation

+=Distinction between and = short S1 = 1; S1 = S1 + 1 and short S1 = 1; s1+=1; Question.

public class IntType05 {
    public static void main(String[] args) {
        short s1 = 1 ;
        // s1 = s1 + 1 ; // 变异错误:s1自动向int类型转换
        s1 += 1 ;
        System.out.println(s1);
    }
}

+=The operator is specified in the Java language, and the compiler will recognize it, so it can be compiled correctly.

5. Boolean type

Two logical values, true and false, are usually used to represent the results of relational operations.

public class IntType06 {
    public static void main(String[] args) {
        // 存在精度损失问题:0.30000000000000004
        System.out.println(3*0.1);
        // true
        System.out.println(0.3 == 0.3);
        // false
        System.out.println(3*0.1 == 0.3);
    }
}

3、 Float and dubble

1. Basic concepts

These two types may not understand the relationship and distinction in most cases. First, we should understand several basic concepts.

Floating point number: used in a computer to approximate any real number. Specifically, this real number is obtained by multiplying an integer or fixed-point number by an integer power of a cardinality (usually 2 in computers)

Single precision floating-point number: single precision floating-point number is used to represent real number with decimal part, which is generally used for scientific calculation. Occupies 4 bytes (32 bits) of storage space

Double precision floating-point number: a double precision floating-point number is a data type used by a computer. It uses 64 bits (8 bytes) to store a floating-point number.

2. Comparative analysis

位数:32
最小值:1.4E-45
最大值:3.4028235E38
位数:64
最小值:4.9E-324
最大值:1.7976931348623157E308

Demonstration cases related to float and double declarations and transformations.

public class IntType07 {
    public static void main(String[] args) {
        // float 声明
        float f1 = 12.3f ;
        // double 声明
        double d1 = 13.4 ;
        // 向下转型,需要强制转换
        float f2 = (float) d1 ;
        System.out.println("f1="+f1+";d1="+d1+";f2="+f2);
    }
}

4、 High precision type

1、BigInteger

It supports integer operations of any size, and there will be no loss in the operation process. Without the corresponding basic type, the operation will become relatively complex, and the operation speed will naturally decrease.

2、BigDecimal

Support fixed-point numbers with arbitrary precision, which are usually used for accurate currency calculation. In the daily development of the company, it is usually a hard requirement.

public class IntType08 {
    public static void main(String[] args) {
        BigDecimal dec1 = new BigDecimal(3.0) ;
        BigDecimal dec2 = new BigDecimal(2.11) ;
        // 精确加法运算
        BigDecimal res1 = dec1.add(dec2) ;
        System.out.println(res1);
        // 精确减法运算,并截取结果
        // HALF_UP:四舍五入
        BigDecimal res2 = dec1.subtract(dec2);
        System.out.println(res2.setScale(1,RoundingMode.HALF_UP));
        // 精确乘法运算
        BigDecimal res3 = dec1.multiply(dec2) ;
        System.out.println(res3.doubleValue());
        // 精确除法运算,并截取结果
        // ROUND_DOWN:直接按保留位数截取
        BigDecimal res4 = dec1.divide(dec2,2,BigDecimal.ROUND_DOWN);
        System.out.println(res4);
    }
}

5、 Source code address

GitHub·地址
https://github.com/cicadasmile/java-base-parent
GitEE·地址
https://gitee.com/cicadasmile/java-base-parent
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>