Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how many objects are created

 
josephine chen
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String name;
String newName = new String("Nick");//one obj created
newName = new String("Jason");// one more created
name = new String("Frieda"); // one more created
String newestName = name; //created or not??
name = null;//name is eligible for gced or Frieda is eligible for
gced ?

 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<PRE>
String name;
String newName = new String("Nick");//one obj created
newName = new String("Jason");// one more created
</PRE>
and "Nick" is gone. It's now eligible for GC.
<PRE>
name = new String("Frieda"); // one more created
String newestName = name; //created or not??
</PRE>
Not. You now have two separate references to String object "Frieda"
<PRE>
name = null;//name is eligible for gced or Frieda is eligible for gced ?
</PRE>
None. name is not an object and "Freida" is still referenced by newestName.
 
Badri Sarma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am interested to know, actually how many objects are created rather than how many are eligible for GC, after executing the final statement. In above mentioned question by shan
Thanks in advance
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi shan
Three object are created
String newestName = name; //created or not??
it does not create a object
because the reference point to the same oject with name in this statement.
Please let me know if I answer wrong
thanks
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As there are three new operators, so three objects are created....
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there are six String objects created.
--new String("Nick")-- will create a string object holding the word Nick and will intern it. Then that object is passed to the constructor of String that takes another String. The constructor creates another string, but this time it wil not intern it. Thus there are two string objects created.
 
Anushkha Rana
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
posted by Jose
I think there are six String objects created.
--new String("Nick")-- will create a string object holding the word Nick and will intern it. Then that object is passed to the constructor of String that takes another String. The constructor creates another string, but this time it wil not intern it. Thus there are two string objects created.

Well! i don't understand how 6 objects are created. How can we monitor whether a string literal or constant expressions is being interned?
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jose
Why you said?
I think there are six String objects created.
Thus there are two string objects created.

String newName = new String("Nick");//one obj created
newName = new String("Jason");// one more created
name = new String("Frieda"); // one more created
Are these three object created?
 
Karin Paola Illuminate
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think 3 String objects are being created. For each "new" statement one.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This will return false. Similarly, new String("Nick"); will create two String objects the first time is encountered. The second and succesive only one becuse all the ocurrences of the literal string "Nick" points to the same (interned) string object.
 
Ramnath krishnamurthi
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There will be three string objects created in the heap with each new instance.
Thanks,
Ramnath
 
Ramnath krishnamurthi
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To explain more,
String newName = newString("Nick");
this line creates a new String object "Nick" in the heap and the reference newName is set to point it.
newName = new String("Jason");
This line again creates one more string object in the heap but this time the reference newName is set to point to "Jason".
name = new String("Frieda");
One more String object "Frieda" is created in the heap and the variable name is set to poin to it.
[B]String newestName = name;
This line will not create a new Object rather the variable newestName is set to point to the value pointed by name (i.e) "Frieda".
Hope this explanis your question.
 
Martin Smith
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
6 objects.
otherwise what is the difference between;
x = new String("a");
and
x = "a";
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course
It is the difference between;
x = new String("a");
It creates the object and return the object referance to x
and
x = "a";
but it without creates the object,it only referance to a string,it is similar to primitive data type.
I wonder if somebody can tell me that the correct meaning and answer
 
Charles Leung
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3 objects
Siu is right, back in the days of C/C++, *object* and *reference to object* are not the same
this is off-topic; just wanna remind you about special handling about String in Java.

only one object is created.
 
Lawrence Chettiar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Martin is right in a sense it should create 6 objects.
As per api doc String intern method
"All literal strings and string-valued constant expressions are interned. String literals are defined in �3.10.5 of the Java Language Specification"
And per JLS
3.10.5 String Literals
A string literal consists of zero or more characters enclosed in double quotes
Hence there should be 3 strings in the pool and 3 strings in the heap, Now the correct answer should be 6, pls confirm.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Alicea:
<PRE>
String name;
String newName = new String("Nick");//one obj created
newName = new String("Jason");// one more created
</PRE>
and "Nick" is gone. It's now eligible for GC.
<PRE>
name = new String("Frieda"); // one more created
String newestName = name; //created or not??
</PRE>
Not. You now have two separate references to String object "Frieda"
<PRE>
name = null;//name is eligible for gced or Frieda is eligible for gced ?
</PRE>
None. name is not an object and "Freida" is still referenced by newestName.

Can you please explain to me why name is not an object. I think that name is String object and now that it points to null it would be eligible for gc.
 
preeti khane
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anupam,
name is String reference variable that can point to an actual string literal which denotes a String object.
GC works only on objects that variables point to and not on the actual variables themsleves...
 
Yuan Ye
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
quote:
Hi All,
Martin is right in a sense it should create 6 objects.
As per api doc String intern method
"All literal strings and string-valued constant expressions are interned. String literals are defined in �3.10.5 of the Java Language Specification"
And per JLS
3.10.5 String Literals
A string literal consists of zero or more characters enclosed in double quotes
Hence there should be 3 strings in the pool and 3 strings in the heap, Now the correct answer should be 6, pls confirm.

I agree with you and Martin.Strictly speaking, 6 objects.
 
suresh guru
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
three objects are created and one is not eligible for CGed. but the rintime actually creates three string constants (as the string words are different). and places them in it's constant pool along with the three string Objs. So after execution, the heap will contain three objs (two eligible for GC) and three string constants.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic