If I create objects with the same value of Integera and b in Java, do I create new objects?

Asked 2 years ago, Updated 2 years ago, 81 views

Hello! I'm asking you a question because I'm curious about the example in a Java book.

public static void main(String[] args) {
    Integer o1 = 1000;
    Integer o2 = 1000;
    Integer o3 = new Integer(1000);
    Integer[] arr1 = {1,2};
    Integer[] arr2 = {1,2};
    Strings1 = "Hong Gil-dong";
    Strings2 = "Hong Gil-dong";

}

Where o1 and o2 are created as individual objects? Since s1 and s2 share the same object, the true value is returned when (s1==s2) In the case of Integer, the number 1 is false when compared to the code below.

Compare identityHashCode with
 //==
    System.out.println("Integer o1 == o2: "+(o1==o2));         //------1
    System.out.println("Integer o1 == o3: "+(o1==o3)+"\n");    //------2

    //Hash code comparison
    System.out.println ("Compare Integro1, o2, o3.hashCode()";
    System.out.println(o1.hashCode()+", "+o2.hashCode()+", "+o3.hashCode()+"\n");
    System.out.println ("Compare IntegrityHashCode()");
    System.out.println(System.identityHashCode(o1)+", "+System.identityHashCode(o2)+", "+System.identityHashCode(o3)+"\n");

java integer

2022-09-22 18:30

2 Answers

Hello, as you know, == compares the memory location or object reference of the value, and the equals method compares the value. However, integers use cache internally to store values from -128 to +127. Therefore, the == operator returns true when used to determine a value between -128 and 127. However, if the value is greater than that, returns false because a new Integer object is created.

https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.7


2022-09-22 18:30

Since you explained the difference between == and equals, I would like to further explain the case of String.

Think of it as a case of Java 1.4

Integero1 = 1000; // Error
String1 = "Hong Gil-dong"; // Normal
String2 = "Hong Gil-dong"; // Normal

The reason for limiting to Java 1 and 4 is that the questioner may be confused.

The Integero1 = 1000 code is internally executed by a feature called autoboxing, such as Integero1 = new Integer (1000).

Then, you can ask back whether the String1 = "Hong Gil-dong" is running as String1 = new String (Hong Gil-dong) as above, but it is not.

String1 = new String ("Hong Gil-dong");
String2 = new String ("Hong Gil-dong");

These two s1 and s2 are different.

To know why, you need to know the area called constant pool inside the JVM and also understand the role of String's internal method.

If you write the answer easily, the string is a constant, and in jvm, the constant is managed separately. However, if you make it with a new String, it is not managed by a constant pool, but created in heap like a normal object.

That is, the reason why the strings come out the same is because the intern method returns "Hong Gil-dong" (where "Hong Gil-dong" is one in the constant pool).


2022-09-22 18:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.