java string comparison and the difference equals the ==

public class StringTest {
    public static void main(String[] argc){
        String a="abc";
        String b="abc";
        String c=new String("abc");
        System.out.println(a==b);
        System.out.println(a==c);
        System.out.println(a.equals(c));
    }
}
true
false
true

It equals the ratio of the value, and the address == comparison, as to why a and b of the same address, the issues related to the string constant pool, and you can fancy a blog because of a and b point to the same address in memory, so that even the == comparison, is truue, and c using new String ( "abc"), java create a new object to hold the value of abc, in its memory abc allocated memory is different from the previous one, and stored in a new memory address referenced c, i.e. it points to, the address of a and c, and different, is false

String s="abcd"

s is not an object, it's just a reference to an object is a memory area in the memory, the more the member variables, the greater this memory area occupied space. References only a 4-byte data, stored inside the object it points to the address, this address can access the object by

Guess you like

Origin blog.csdn.net/abc_123456___/article/details/90705677