On the basis of data types and use == equals the difference in a subject

Use == and equals on the basis of the data types are compared for the same object

What saved variable is?

For example String s1 = "hello" String s2 = "hello", s1 and s2 are stored string "hello" in the constant pool of the address, the value of s1 and s2 is the same address.

For example String s1 = new String ( "hello") and String s2 = "hello", holds the stack variables s1, s2, s1 stored in address stack "hello" address, s2 is stored in address "hello" in the method address the constant pool area.

String s1 = new String ( "hello") This code creates two objects, the first object: is an object in the heap is created (new String ()), the second object: creating a string constant pool a "hello" constant, then the relationship in memory is preserved s1 stack heap new String (); address heap new String (), point to the constant pool area method "hello" address.

String s2 = "hello" relatively simple method only in a string constant pool area creates a constant string "hello" this one object.

The variables are stored objects: points to the heap address, it saved variables are constant: points to the address of the method the constant pool.

== with objects and constants, variables compare two address values ​​are stored flase.

Objects and constant value may want to compare the contents of String functions like replication as the equals method to compare the value of content

Guess you like

Origin www.cnblogs.com/zhongmeilin/p/11615996.html