Published using Google Docs
Strings
Updated automatically every 5 minutes

http://www.ntu.edu.sg/home/ehchua/programming/java/j3d_string.html

OOP_StringLliteralVsObject.png

String s1 = "Hello";              // String literal
String s2 = "Hello";              // String literal
String s3 = s1;                   // same reference
String s4 = new String("Hello");  // String object. It is uncommon and not recommended to use the new operator to construct a String object in the heap.
String s5 = new String("Hello");  // String object

s1 == s1;         // true, same pointer
s1 == s2;         // true, s1 and s1 share storage in common pool
s1 == s3;         // true, s3 is assigned same pointer as s1
s1.equals(s3);    // true, same contents
s1 == s4;         // false, different pointers
s1.equals(s4);    // true, same contents
s4 == s5;         // false, different pointers in heap
s4.equals(s5);    // true, same contents