Java Basics (01): the basic data types, the core point of finishing

This article Source: GitHub · Click here || GitEE · Click here

First, the basic type

1, basic type

Do not use New to create, declare a reference variable non-delivery, and the value of the variable directly into the stack, the size does not vary with changes in the operating environment more efficient. The use of new reference created objects stored on the heap.

2, the basic information

Include the following basic types: byte, short, int, long, float, double, boolean, char, the size range can be viewed by 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);
    }
}

Second, the use case

1, type conversion

自动转换: A small range of data types can be automatically converted into a wide range of data types.

强制转换: Convert one data type to another data type.

类型提升: Calculation expression data of different types, the type is automatically lift a large 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 type of conversion needed most concern is the scope of the size of the problem.

2, package type

Elementary data type does not match the object-oriented thinking, which appeared type of wrapper, the wrapper and add additional properties and methods, automatic packaging functions can be converted to the package type base type. Java primitive types for each provides a package type, Integer, Double, Long, Boolean, Byte and the like.

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

The default value is null Integer variables Integer instructions can distinguish the difference between a value of 0 and unassigned, like exam 0 points and the difference did not take the exam.

3, character type

char type variable is used to store Unicode character encoding, unicode character set contains characters.

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

注意: Special uncommon words there may not contain concentrated in unicode character encoding.

4, assignments and operations

+= 和 =Distinction: short s1=1;s1=s1+1与short s1=1;s1+=1;the issue.

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

+=Operator is predetermined java language, the compiler will be recognized that process, it is possible to compile correctly.

5, boolean

Two logical values: trueand false, usually used to represent the relationship between the results of 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);
    }
}

Three, Float and Dubble

1, the basic concept

These two types may in most cases said they do not understand the relationship and distinction, we must first understand a few basic concepts.

浮点数: In a computer to approximate the arbitrary real number a. Specifically, the real number of a fixed-point or integer multiplied by a base (usually 2 in computers) raised to the power of an integer to give

单精度浮点数: Single-precision floating-point real number is used with a fractional part, it is generally used for scientific computing. It occupies 4 bytes (32-bit) memory space

双精度浮点数: Double precision floating point (double) is a data type of computer, 64-bit (8 bytes) to store a floating point number.

2, comparative analysis

  • Float basic description
位数:32
最小值:1.4E-45
最大值:3.4028235E38
  • Double basic description
位数:64
最小值:4.9E-324
最大值:1.7976931348623157E308
  • Case Description

float and double conversion statements and related presentation case.

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);
    }
}

Fourth, high-precision type

1、BigInteger

Supports integer arithmetic of any size, and will not have any loss of computing process, no corresponding basic types, operators will become relatively complex operation speed will drop naturally.

2、BigDecimal

Supporting arbitrary-precision fixed-point numbers, usually used for accurate currency, in the company's daily development, there 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);
    }
}

Fifth, the source code address

GitHub·地址
https://github.com/cicadasmile/java-base-parent
GitEE·地址
https://gitee.com/cicadasmile/java-base-parent

Guess you like

Origin www.cnblogs.com/cicada-smile/p/12445215.html