Java's eight basic data types and BigDecimal

Basic data type

8 basic data types

They are:

  1. 6 types of numbers: byte, short, int, long, float, double
  2. 1 character type: char
  3. 1 Boolean type: boolean

 

Bytes occupied by eight basic data types

basic type Bit Byte Defaults
boolean 1 false
byte 8 1 0
char 16 2 ‘u0000’
short 16 2 0
int 32 4 0
long 64 8 0L
float 32 4 0f
double 64 8 0d

 
3. Basic and string conversion method

Take plastic surgery as an example

3.1. Integer conversion to string

String str = String.valueOf(int i);

String str = Integer.toString(int i);

String str = “ ” + i ;

3.2. Convert a string to an integer

int i = Integer.parseIn(String str)

int i = Integer.valueOf().intValue()
//说明:
//Integer.parseIn和Integer.valueOf 不同,
//前者生成的是整型,而后者是一个对象,所以要通过intValue()来获得对象的值

 
 

type of packaging

Packaging types corresponding to eight basic data types

boolean -> Boolean
byte -> Byte
char -> Character
short -> Short
int -> Integer
long -> Long
float -> Float
double -> Double

 

Basic data type and packaging type conversion

 

1. Conversion of basic data types to packaging types (boxing)
 

Integer a = new Integer (1);
Integer b = 1;//jdk1.5 之后可以通过这种方式自动装箱
Integer c = Integer.valueOf(1);

 
2. Conversion of packaging type to basic data type (unboxing)
 

int a1 = a.intValue();
int b1 = b;//自动拆箱

Double c1;
double c2= double(c1);//通过强制转换

 

8 basic types of packaging classes and constant pools

Most of the wrapper classes of Java basic types implement constant pool technology, namely Byte, Short, Integer, Long, Character, Boolean; the first four wrapper classes create the corresponding type of cache data of the value [-128, 127] by default , Character creates cache data with a value in the range of [0,127], and Boolean returns True Or False directly. If it exceeds the corresponding range, it will still create a new object.

The two floating-point number type packaging classes Float and Double do not implement constant pool technology.

Integer i1 = 33;
Integer i2 = 33;
System.out.println(i1 == i2);// 输出 true

Integer i11 = 333;
Integer i22 = 333;
System.out.println(i11 == i22);// 输出 false

Double i3 = 1.2;
Double i4 = 1.2;
System.out.println(i3 == i4);// 输出 false

 
Application scenarios:

  1. Integer i1=40;Java will directly encapsulate the code when compiling Integer i1=Integer.valueOf(40);, thereby using the objects in the constant pool.
  2. Integer i1 = new Integer(40);In this case, a new object is created.
Integer i1 = 40;
Integer i2 = new Integer(40);
System.out.println(i1==i2);//输出 false

 
Let me give a more difficult example:

Integer i1 = 40;
Integer i2 = 40;
Integer i3 = 0;
Integer i4 = new Integer(40);
Integer i5 = new Integer(40);
Integer i6 = new Integer(0);
  
System.out.println("i1=i2   " + (i1 == i2));
System.out.println("i1=i2+i3   " + (i1 == i2 + i3));
System.out.println("i1=i4   " + (i1 == i4));
System.out.println("i4=i5   " + (i4 == i5));
System.out.println("i4=i5+i6   " + (i4 == i5 + i6));   
System.out.println("40=i5+i6   " + (40 == i5 + i6));     

Output:

i1=i2   true
i1=i2+i3   true
i1=i4   false
i4=i5   false
i4=i5+i6   true
40=i5+i6   true

Explanation:

Statement i4 == i5 + i6, because +this operator is not applicable to Integer objects, first, i5 and i6 are automatically unboxed and the values ​​are added, ie i4 == 40. Then Integer object can not be directly compared with the value, so i4 automatic unpacking converted int value of 40, this statement into the final 40 == 40numerical comparison.

 
 

BigDecimal

The "Alibaba Java Development Manual" mentions: The equivalent judgment between floating-point numbers, basic data types cannot be compared with ==, and packaging data types cannot be judged with equals. Consider the following example:

float a = 1.0f - 0.9f;
float b = 0.9f - 0.8f;
System.out.println(a);// 0.100000024
System.out.println(b);// 0.099999964
System.out.println(a == b);// false

With basic mathematical knowledge, we clearly know that the output is not what we want (loss of precision). How can we solve this problem?

A very common method is to use BigDecimal to define the value of a floating point number, and then perform arithmetic operations on the floating point number.

BigDecimal a = new BigDecimal("1.0");
BigDecimal b = new BigDecimal("0.9");
BigDecimal c = new BigDecimal("0.8");

BigDecimal x = a.subtract(b); //两数相减
BigDecimal y = b.subtract(c); 

System.out.println(x); /* 0.1 */
System.out.println(y); /* 0.1 */
System.out.println(Objects.equals(x, y)); /* true */

 

BigDecimal size comparison

a.compareTo(b) : Return -1 means a is less than b, 0 means a is equal to b, 1 means a is greater than b

BigDecimal a = new BigDecimal("1.0");
BigDecimal b = new BigDecimal("0.9");
System.out.println(a.compareTo(b));// 1

 

BigDecimal keep a few decimal places

By setScaleretains several decimal places and ways to set retention rules. There are many retention rules, no need to remember, IDEA will prompt.

BigDecimal m = new BigDecimal("1.255433");
BigDecimal n = m.setScale(3,BigDecimal.ROUND_HALF_DOWN);
System.out.println(n);// 1.255

 
note

  1. When we use BigDecimal, in order to prevent loss of precision, it is recommended to use its BigDecimal(String) construction method to create objects.
  2. BigDecimal is mainly used to manipulate (large) floating-point numbers, BigInteger is mainly used to manipulate large integers (more than long type).
  3. The realization of BigDecimal uses BigInteger, the difference is that BigDecimal adds the concept of decimal places

Guess you like

Origin blog.csdn.net/weixin_43901865/article/details/112566955