0

If i write :

String s = new String("abc");

It creates an object of type String with the value "abc". And if i write :

String s = "abc";

This also creates an object with value "abc". How does without encountering new keyword, an object is created.

Also if i write:

s.append("def");

It creates two more string objects that is:

a. "abcdef"  
b. "def"

So on encountering anything within double inverted commas make a new String object. How does that happen?

Maarty
  • 1,140
  • 1
  • 12
  • 27

4 Answers4

8

It is called a String literal, and is specified in the JLS 3.10.5:

A string literal consists of zero or more characters enclosed in double quotes. Characters may be represented by escape sequences (§3.10.6) - one escape sequence for characters in the range U+0000 to U+FFFF, two escape sequences for the UTF-16 surrogate code units of characters in the range U+010000 to U+10FFFF.

Keppil
  • 45,603
  • 8
  • 97
  • 119
6

"abc" is a String literal , defined by the specifications of the language.

Refer JLS 3.10.5:

A string literal consists of zero or more characters enclosed in double quotes. Characters may be represented by escape sequences (§3.10.6) - one escape sequence for characters in the range U+0000 to U+FFFF, two escape sequences for the UTF-16 surrogate code units of characters in the range U+010000 to U+10FFFF.

Suggested Reading:

  1. Difference between string object and string literal.
  2. Java String Pool

Also , read String is Really Special:

String literals are stored in a common pool. This facilitates sharing of storage for strings with the same contents to conserve storage. String objects allocated via new operator are stored in the heap, and there is no sharing of storage for the same contents.

String s1 = "Hello";              // String literal
String s2 = "Hello";              // String literal
String s3 = s1;                   // same reference
String s4 = new String("Hello");  // String object
String s5 = new String("Hello");  // String object

enter image description here

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  
Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • 1
    Exaclty :http://stackoverflow.com/questions/17489250/how-can-a-string-be-initialized-using/17489410#17489410 – Suresh Atta Jul 23 '13 at 11:15
  • @sureshatta plus the most important JLS reference and secondly that answer itself is exactly [this](http://www.ntu.edu.sg/home/ehchua/programming/java/J3d_String.html) although it omits the most important part where you should have kept the comparison using `==` and `equals()`.. – AllTooSir Jul 23 '13 at 11:16
0

In Java, when you do "hello", the java compiler creates a String Object similar to new String("hello"). This object is maintained in the String Pool (String#intern()).

SO doing String i = "hello". It checks if i exists in the pool by checking from String#intern(). If yes, it reuses it else it creates a new one and puts in the pool. See this for more info.

Community
  • 1
  • 1
Jatin
  • 31,116
  • 15
  • 98
  • 163
0

As all of them have answered "abc" is a String literal. And for your second part of question, i.e, why after executing statement s.append("def") two objects are created because in Java String is immutable.

Xavier DSouza
  • 2,861
  • 7
  • 29
  • 40