There are two ways to create a String object in Java:
Each string literal is a reference to an instance of class String.
To cut down the number of String objects created in the JVM, the String class keeps a pool of strings.
Each time your code create a string literal, the JVM checks the string literal pool first.
If the string already exists in the pool, a reference to the pooled instance returns.
If the string does not exist in the pool, a new String object instantiates, then is placed in the pool.
But, when you use
A String object is created out of the String literal pool, even if an equal string already exists in the pool.
Momen Travolta wrote:1)What are other "things" considered to be an object in java?
Everything that is not a primitive (boolean, byte, short, char, int, long, float, double) or null. That includes strings. These will also all pass the "instanceof Object" test.
2) Why is a string literal considered an object?
If String literals did not exist the only way to create a new String would be using a char:
You see how 1) nasty that looks, and 2) tedious it is to write. Imagine having a String of 50 characters!
Because of this, and because String is used so very frequently, the String class has been given special treatment - you can declare it as a string literal. But in the end that's still an instance of the String class, and therefore an object.
Vedha Vishali wrote:
1.Creates a new String object "Java" in the String literal pool.
2.Creates a another new String Object out of the String literal pool and the reference variable str points to it.
Step 1 is correct, but step 2 is not. Using the new operator will always create a new object. In this case, you create a new String object and the contents of the literal string in the pool is copied to that new object. The variable str will not point to the string in the pool, but to a separate String object that is not in the pool (the one that's created by calling new).
Yogesh Gnanapraksam wrote:In the above code, according to what Campbell has said there are two identical objects ?
According to Vedha, Only one object with two references ?
Jesper Young wrote: you create a new String object and the contents of the literal string in the pool is copied to that new object.
If I remembered correct, *String Literal Pool is collection of reference*. so in this case the Object ("pool") always creates on Heap . but when you say new String("Java")
1. create a object ("Java") on heap and which is referenced by StringLiteralPool
2. and new Operator create object which is the Identical copy of the StringLiteralPool reference Objcet agian which is lies on heap
<edit>so, there are 2 objects in heap. "Java"--->referenced by StringLiteral Pool , "Java"----> which is directly referenced by *str* variable[it may be in stack or heap]</edit>
Correct me , if I am wrong.
Joined: Mar 24, 2010
Even I explained the same.
2.Creates a another new String Object out of the String literal pool and the reference variable str points to it (Points to the new String Object created outside the literal pool).
Joined: Dec 17, 2009
1.In this step no String object creates.
2.The reference varaible str1 is made to point to the already available String Object("Java") in the literal pool.
I think you gave this explanation for a String initialization where 'new' is not used.
That is why I found your's and Campbell's to be contradictory .
Joined: Dec 17, 2009
I am quoting from HeadFirst Java
Whenever you make a new String,he JVM puts it into a special part of memory called 'String Pool'. If there is already a String
in the String Pool with the same value ,the JVM doesn't create a duplicate ,it simply refers your reference variable to the existing entry.
The JVM can get away with this because Strings are immutable;one reference variable can't change a String's value out from under another reference
variable referring to the same String.
The other issue with String pool is that Garbage Collector doesn't go there.
In my understanding of the above lines,the manner is which the String is initialized (using new or without new) does not make any difference.
If there is an identical object in the String Pool then a new String object is not created.
This is justified by the fact that the Garbage Collector doesn't go there. So if there are String objects without any references they still remain in memory and will be referenced when an identical String object reference is required.