SCJP 5
And work the rest out for yourselves.>campbell@computer:~/java$ java StringEqualsDemo Campbell
s = Campbell, hash = fb83cb24, s1 = Campbell, . . .
Ankur Kapoor
Jesper de Jong wrote:
Jd Sierra wrote:
This is not valid Java syntax. Did you mean:
Ankur Kapoor wrote:
2. Now lets take a scenario where you would have used new String() pre String literal created in a POOL
String myString = new String("fun"); // this shall create a New Object in HEAP but not in POOL . [OBJECT CREATED] hence creating only single Object that is in HEAP
3. Now let's use intern() function of String to make the String Objects created using new operator available in the POOL
String myString = new String("fun").intern() // this shall create a New Object in HEAP and also place it in the POOL . [OBJECT CREATED]
Kathy Sierra wrote:
In this case, because we used the new keyword, Java will create a new String object in normal (nonpool) memory, and s will refer to it. In addition, literal "abc" will be placed in the pool.
Jd Sierra wrote:
3)
now I want to know is how many object will be created in this case just and only because of line no.2?
After the end of first line, one object will be created as I mentioned in case 1.
Because of 2nd line, definately one object will be created containing String literal "ghi" due to new keyword which will not be referred from Strin Literal Pool Table. But I think here unlike in case 2, one more object containg String literal "ghi" will not be created which we are passing in String constructor because it's alreday available after line 1.
So, answer is only one new object is created just and only because of line no.2
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:2) ANY and EVERY time the compiler sees a String literal for the first time, a String is created in the String pool.
Its hard to find !!!