I. Related concepts


  1. Constants are represented by final modified member variables whose values cannot be changed once given! Final modifies three types of variables: static variables, instance variables, and local variables, representing three types of constants.

  2. In the Class file structure, the first four bytes are used to store the Magic Number, which determines whether a file is acceptable to the JVM. The next four bytes are used to store the version Number. The first two bytes are used to store the minor version Number, and the last two bytes are used to store the major version Number. The constant pool is then used to store constants. Since the number of constants is not fixed, a u2-type data (constant_pool_count) is placed at the entry of the constant pool to store the constant pool capacity count. Constant pools are used to store two main types of constants: Literal and Symbolic References. Literal is equivalent to the concept of constant in Java language level, such as text string, constant value declared as final, etc. Symbolic References belong to the concept of compilation principle, including the following three types of constants:

  • Fully qualified names of classes and interfaces
  • Field name and descriptor
  • Method name and descriptor
  1. The runtime constant pool in the method area is part of the method area. In addition to the CLass version, field, method, interface, and other description information, the CLass file contains the constant pool, which is used to store various literals and symbolic references generated at compile time. This part of the constant pool will be stored in the runtime constant pool when the CLass is loaded into the method area. Runtime constant pool relative to the CLass file another important feature of the constant pool is dynamic, the Java language does not require constant must only compile time to produce, is not preset constant pool into the CLass file content can enter method area runtime constant pool, runtime might also put new constants in a pool, One feature that developers use most is the Intern () method of the String class.

  2. Benefits of constant pool Constant pool is to avoid frequent creation and destruction of objects that affect system performance, it implements object sharing. The string constant pool, for example, puts all string literals into one constant pool at compile time. (1) Saving memory space: all the same string constants in the constant pool are merged and occupy only one space. (2) Save runtime: == is faster than equals() when comparing strings. For two reference variables, only == is used to determine whether the references are equal, which can also determine whether the actual values are equal.

  3. The meaning of double equals == When a double equals sign is applied between basic data types, it is their numeric values that are compared. A double equal sign is applied between compound data types (classes) to compare where they reside in memory.

Eight basic types of wrapper classes and constant pools


  1. Basic types in the Java wrapper classes are mostly true constant pool technology, namely, Byte, Short, Integer, Long, Character, and Boolean;
Integer i1 = 40; Integer i2 = 40; System.out.println(i1==i2); / / output TRUECopy the code

These five wrapper classes create the corresponding type of cached data for the value [-128,127] by default, but new objects are still created beyond this range.

Public static Integer valueOf(int I) {assert IntegerCache. High >= 127; if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }Copy the code
Integer i1 = 400; Integer i2 = 400; System.out.println(i1==i2); / / output is falseCopy the code
  1. The wrapper class Float,Double, for two floating-point types, does not implement constant pooling technology.
Double i1 = 1.2; Double i2 = 1.2; System.out.println(i1==i2); / / output is falseCopy the code
  1. Constant pool scenarios

    (1)The Integer i1 = 40;Java encapsulates the code directly at compile timeInteger i1=Integer.valueOf(40);To use 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); / / output is falseCopy the code
  1. Integer is a richer 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));     
Copy the code
i1=i2   true
i1=i2+i3   true
i1=i4   false
i4=i5   false
i4=i5+i6   true
40=i5+i6   true
Copy the code

Statement i4 == i5 + i6, since the + operator does not apply to Integer objects, i5 and i6 are automatically unboxed, adding values, i4 == 40. Then the Integer object cannot be directly compared to a numeric value, so i4 automatically unboxes itself to int 40, and finally the statement is converted to 40 == 40 for numeric comparison. Automatic boxing and unboxing in Java

String class and constant pool


  1. String object creation mode
String str1 = "abcd"; String str2 = new String("abcd"); System.out.println(str1==str2); //falseCopy the code

There is a difference between the two methods of creating objects in the constant pool and creating a new object directly in the heap memory space. Whenever you use the new method, you need to create a new object.

  1. The concatenation expression + (1) only new objects that are concatenated using a “+” concatenation between String objects created using quoted text are added to the String pool. (2) All “+” concatenation expressions that contain new objects (including NULL) are not added to the string pool.
String str1 = "str"; String str2 = "ing"; String str3 = "str" + "ing"; String str4 = str1 + str2; System.out.println(str3 == str4); //false String str5 = "string"; System.out.println(str3 == str5); //trueCopy the code

Java Basics: String concatenation

  • Special case 1
public static final String A = "ab"; Public static final String B = "CD "; Public static void main(String[] args) {String s = A + B; String t = "abcd"; // initialize s with a + concatenation of two constants. If (s == t) {system.out.println ("s = t, they are the same object "); } else {system.out.println ("s is not equal to t, they are not the same object "); }} s is equal to t, they are the same objectCopy the code

A and B are constant, and the value is fixed, so s is fixed, which is determined when the class is compiled. String s=A+B; String s=”ab”+” CD “;

  • Special case 2
public static final String A; Public static final String B; Static {A = "ab"; B = "cd"; } public static void main(String[] args) {public static void main(String[] args) { String t = "abcd"; If (s == t) {system.out.println ("s = t, they are the same object "); } else {system.out.println ("s is not equal to t, they are not the same object "); }} s is not equal to t, they are not the same objectCopy the code

A and B are defined as constants, but they are not assigned immediately. When they are assigned, and what value they are assigned, is a variable until the value of S is computed. So A and B behave like A variable until they are assigned. S cannot be determined at compile time, but can only be created at run time.

  1. String s1 = new String(“xyz”); How many objects did ** create? ** Consider the class loading phase and the actual execution time. (1) Class loading is performed only once for a class. Xyz “is already created and hosted when the class is loaded (there is no need to create instances of” XYZ “to host if the” XYZ “string was already hosted before the class was loaded). Hosted strings are placed in a globally shared string constant pool. (2) When this code is subsequently run, the String instance corresponding to the “xyz” literal is fixed and will not be created again. So this code makes a copy of the object in the constant pool and places it in the heap, and gives S1 a reference to the object in the heap. This statement creates two objects.

  2. Another important feature of a runtime constant pool over a CLass file constant pool is that it is dynamic. The Java language does not require constants to be generated only at compile time. The runtime constant pool of the method area can be accessed only if the contents of the constant pool are not preset into the CLass file. New constants may also be added to the pool during runtime. The String intern() method looks for an equal String in the constant pool, returns a reference to that String if it does, and adds its own String to the constant pool if it does not.

Public static void main(String[] args) {String s1 = new String(" computer "); String s2 = s1.intern(); String s3 = "computer "; System.out.println("s1 == s2? " + (s1 == s2)); System.out.println("s3 == s2? " + (s3 == s2)); }Copy the code
s1 == s2? false
s3 == s2? true
Copy the code
  1. An example of richer string comparisons
public class Test {
 public static void main(String[] args) {   
      String hello = "Hello", lo = "lo";
      System.out.println((hello == "Hello") + " ");
      System.out.println((Other.hello == hello) + " ");
      System.out.println((other.Other.hello == hello) + " ");
      System.out.println((hello == ("Hel"+"lo")) + " ");
      System.out.println((hello == ("Hel"+lo)) + " ");
      System.out.println(hello == ("Hel"+lo).intern());
 }   
}
class Other { static String hello = "Hello"; }
package other;
public class Other { public static String hello = "Hello"; }
Copy the code
True true true true false ' 'references the same String object in the same package. References from the same String object in different classes of the same package. Different packages in different classes still refer to the same String object. Those recognized as the same String in a.class are automatically optimized to be constants referencing the same String. Strings created at runtime have independent memory addresses, so they do not refer to the same string. ----- [2015-08-26]Copy the code