Java review your notes 6 - wrapper class constant pool

Overview:

There are some basic data types in Java, these basic data type variable, you can not call the methods, properties, like any other object ....

Bring some problems in some cases, the packaging is to solve this problem occurs

image-20191121120735523

Packaging may cause these basic data types, the object has the ability to

image-20191121121001152

Packaging correspondence between the type of the base

image-20191121120904649

Features:

  • Packaging are final modification can not be inherited
  • Digital type of parent are Number
  • When the wrapper class as the class attributes, the default values ​​are Null

Packing and unpacking

Unboxing refers to the underlying data object type packaging, unpacking the contrary

  • Autoboxing

    Assigning basic data types correspond directly to the packaging of the reference variable, the system will automatically operate in boxes

    Integer a = 10;
  • Auto-unboxing

    Integer a = new Integer(10);
    int b = a;
  • Manual unboxing

    Integer a = new Integer(10);
    int b = a.intValue();
  • Manual packing / packing

    Integer as = new Integer(10);//装箱
    Integer.valueOf(1);//装箱
    Float.valueOf(1.1);//装箱
    ....
    
    as.intValue();//拆箱

    Usually we do not need for manual entry boxes

The timing of auto-unboxing

1. The packaging type variables assigned to the corresponding base type directly, the system will automatically operate unboxing

2. When you want to access the value of the real value of the data object will automatically unpack, such as an object to be output

3. When the mathematical operation to be performed on the actual value of the packaging, unpacking automatically, for example, compare the size

Note that, when using a test for equality == == symbol on either side of the other side is the basic data type are automatically unpacking (no problem), but if all the packaging types will be compared is the same on both sides of a reference (could cause problems)
Integer i1 = new Integer(10);
Integer i2 = new Integer(10);
System.out.println(i1 == i2); //结果为false

Packaging each string conversion

In the development of the basic data types often need to string conversion type

//我们可以使用包装类提供的valueof()方法来将字符串转为对象的包装类
System.out.println(Float.valueOf("1.1ff"));
System.out.println(Boolean.valueOf("true"));
//相反的String提供了静态方法valueOf() 其他任意类型转换为字符串类型  Object表示所有类均可,包括包装类
System.out.println(String.valueOf('c'));
System.out.println(String.valueOf(true));

The object literal pool

Java virtual machine by default some simple literal object into the constant pool to reduce frequent memory operation; an optimization mechanism;

Test code:

Long s1 = Long.valueOf(127l);
Long s2 = 127l;
Long s3 = new Long(127l);
System.out.println(s1 == s2); //true
System.out.println(s1 == s3); //false

String st1 = "常量池噢噢噢噢";
String st2 = "常量池噢噢噢噢";
String st3 = new String("常量池噢噢噢噢");
System.out.println(st1 == st2); //true
System.out.println(st1 == st3); //false

Boolean b1 = true;
Boolean b2 = true;
Boolean b3 = new Boolean(true);
System.out.println(b1 == b2); //true
System.out.println(b1 == b3); //false

Character c1 = 'a';
Character c2 = 'a';
Character c3 = new Character('a');
System.out.println(c1 == c2); // true
System.out.println(c1 == c3); // false

Float f1 = 1.0f;
Float f2 = 1.0f;
Float f3 = new Float(1.0f);
System.out.println(f1 == f2);//flase
System.out.println(f1 == f3);//flase

Double d1 = 1d;
Double d2 = 1d;
Double d3 = new Double(1d);
System.out.println(d1==d2);//flase
System.out.println(d1==d3);//flase
  • All literal String type will enter the constant pool
  • Byte Short Integer Long four types of literals equal to greater than -128 and less than or equal to 127 when the object enters the constant pool
  • Character type can not be negative, literal greater than or equal to 0 and less than or equal to 127 when the object into the constant pool
  • A Boolean word constant pool will enter the amount of surface
  • Float and Double types will not enter the constant pool

Emphasize:

The literal meaning is to say, during compilation directly specify the actual value, very clear data

This means that when we do not use the literal but the use of the new keyword, the object will go directly into the stack area without the constant pool, see the following example:

Integer a1 = new Integer(10);
Integer a2 = new Integer(10);
System.out.println(a1 == a2);  //false

! Once the new is bound to open up new memory space to take advantage of the constant pool of therefore recommended to make use code literal avoid the use of new;
Additional information:
These wrapper classes, and static methods valueOf String class provides (); will constant access to the pool, rather than new instantly, so the use of literal or valueOf methods can take advantage of the constant pool;

note:

Basic data type does not need to cache data base because the constant pool types a reference does not exist so that the stack is in a region with a method of storing the actual value;

Guess you like

Origin www.cnblogs.com/yangyuanhu/p/11909467.html