aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how many objects are created Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how many objects are created" Watch "how many objects are created" New topic
Author

how many objects are created

josephine chen
Ranch Hand

Joined: Oct 29, 2002
Posts: 216
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

Joined: Jan 30, 2000
Posts: 3222
    
    5
<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.


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Badri Sarma
Ranch Hand

Joined: Apr 01, 2003
Posts: 72
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


Thanks<br />Badri
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
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


Francis Siu
SCJP, MCDBA
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
As there are three new operators, so three objects are created....
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
Anushkha Rana
Greenhorn

Joined: Apr 13, 2003
Posts: 17
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

Joined: Jan 04, 2003
Posts: 867
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

Joined: Oct 18, 2002
Posts: 109
I think 3 String objects are being created. For each "new" statement one.


I not only use all the brains that I have, but all that I can borrow. [Laurence J. Peter]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

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

Joined: Jan 22, 2003
Posts: 56
There will be three string objects created in the heap with each new instance.
Thanks,
Ramnath
Ramnath krishnamurthi
Ranch Hand

Joined: Jan 22, 2003
Posts: 56
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

Joined: Apr 05, 2003
Posts: 22
6 objects.
otherwise what is the difference between;
x = new String("a");
and
x = "a";
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
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

Joined: Mar 02, 2003
Posts: 16
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

Joined: Apr 10, 2003
Posts: 62
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.


SCJP 1.4 (86%)<br />SCMAD Beta (77%)<br />SCEA (part I:89%,partII:93%)<br />MCAD (cleared 70-315 & 70-320 [Score 980])
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
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

Joined: Mar 12, 2003
Posts: 93
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

Joined: Mar 05, 2003
Posts: 172
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

Joined: Sep 07, 2002
Posts: 38
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.
 
Consider Paul's rocket mass heater.
 
subject: how many objects are created