File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Count the number of object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Count the number of object" Watch "Count the number of object" New topic
Author

Count the number of object

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
String s = new String("Hi");
String s1 = new String("Hello");
String s2 = "Hi";

The total number of object are 4 . Right ?
2 object on string literal pool .
2 object on garbage collection heap .

s is a reference variable that is pointing to an object that is on GCH & that object is poining to an object of SLP that has string literal Hi

s1 is a reference variable that is pointing to an object that is on GCH & that object is poining to an object of SLP that has string literal Hello

s2 is a reference that is directly pointing to the same object of SLP that has string literal Hi

Am I Right in everything ???

thanks .
[ January 08, 2005: Message edited by: rathi ji ]
Rob Harris
Greenhorn

Joined: Nov 11, 2004
Posts: 10
s and s2 are not the same object, despite containing the same content, so that makes 5. If the line initialising s2 read
s2 = s;
then there would be 4.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I think that Rathi is correct, but I don't know how to test his answer without looking at the bytecode.


Mike Gershman
SCJP 1.4, SCWCD in process
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
what is final answer ???
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Rathi,
Your explanation is absolutely correct. 4 objects.
[ January 08, 2005: Message edited by: Jay Pawar ]

Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Arjun Shastry
Ranch Hand

Joined: Mar 13, 2003
Posts: 1871
If I run javap command,I see the term 'new' only twice.


MH
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Arjun Shastry:
If I run javap command,I see the term 'new' only twice.



Do you expect more? String literal objects are not created with new.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Count the number of object
 
Similar Threads
number of object ...
string references and values
String -query
Equality Operator "=="
Garbage collection & string literal pool