Java Little White Stepping on Pit Record-Integer & String

Integer

public static void main(String[] args) {
    Integer num1=56;
    Integer num2=56;
    System.out.println(num1==num2);
    System.out.println(num1.equals(num2));
    
    Integer num3=34567;
    Integer num4=34567;
    System.out.println(num3==num4);
    System.out.println(num3.equals(num4));
    
    Integer num21=-56;
    Integer num22=-56;      
    System.out.println(num21==num22);
    System.out.println(num21.equals(num22));
    
    Integer num23=-34567;
    Integer num24=-34567;
    System.out.println(num23==num24);       
    System.out.println(num23.equals(num24));
}

// 答案
true
true
false
true
true
true
false
true

Parsing

"First, == refers to 对象the comparison, equals is the comparison; secondly, the assignment expression of the Integer type = There is a boxing process, and the value of int is judged when packing, if the value is in [-128 , 127] The created object is returned sometimes, otherwise a new object is returned, JLS5 has introduced it, and it is also implemented in the source code. "

/**
 * Returns an {@code Integer} instance representing the specified
 * {@code int} value. If a new {@code Integer} instance is not
 * required, this method should generally be used in preference to
 * the constructor {@link #Integer(int)}, as this method is likely
 * to yield significantly better space and time performance by
 * caching frequently requested values.
 *
 * This method will always cache values in the range -128 to 127,
 * inclusive, and may cache other values outside of this range.
 *
 * @param i an {@code int} value.
 * @return an {@code Integer} instance representing {@code i}.
 * @since 1.5
 */
 public static Integer valueOf(int i) {
     if (i >= IntegerCache.low && i <= IntegerCache.high)
         return IntegerCache.cache[i + (-IntegerCache.low)];
     return new Integer(i);
 }

 

String

String hello="hello world!"; 
String hello1=new String("hello world!");
System.out.println(hello==hello1); //1
String hello2="hello world!";
System.out.println(hello==hello2); //2
String append="hello"+" world!";
System.out.println(hello==append); //3
final String pig = "length: 10";
final String dog = "length: " + pig.length();
System.out.println(pig==dog); //4
final String dog1 = ("length: " + pig.length()).intern();
System.out.println(pig==dog1); //5
System.out. println("Animals are equal: "+ pig == dog);//6

// 答案
false
true
true
false
true
false

Parsing

The string obtained by the constant expression operation is calculated at compile time , and will be treated as a string constant later; the string obtained by the link operation at runtime is newly created, so it should be treated differently. Others such as the string that can be obtained through the limited operation of the display are string constants, the String.intern method can be "qualified," and the operator priority can also be found in this book through the JLS (Java language Specification) Java programming specification. "

"To sum up again, whether it is Integer, Long, Char, Short, Double, Float, String involves a process of unpacking and packing, and their respective rules are slightly different, so you should take a closer look at the definition of JLS."

Published 952 original articles · praised 1820 · 890,000 views

Guess you like

Origin blog.csdn.net/Dream_Weave/article/details/105423367