aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes number of objects created? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "number of objects created?" Watch "number of objects created?" New topic
Author

number of objects created?

srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
hi ,

String x=new String("xyz");
y="abc";
x=x+y;

how many string objects are created???


thanks & regards

srikanth reddy


Thanks & Regards<br /> <br />-Srikanth
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

4 String objects are created.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Stephen O'Kane
Greenhorn

Joined: Aug 17, 2005
Posts: 26
what are the four string objects?

one for x
one for y
one for storing an intermediate value?
one for storing the final value of x?

thanks
Sok
Joji Doi
Greenhorn

Joined: Jun 23, 2005
Posts: 3
String x=new String("xyz"); // two objects are created. xyz and new String
y="abc"; // one object is created. abc
x=x+y; // one object is created abcxyz

Total of four objects are created unless xyz and/or abc are already in the constant pool.
Vijay Gade
Ranch Hand

Joined: Jul 04, 2005
Posts: 81

String x=new String("xyz"); // two objects are created. xyz and new String


This is real news to me. I thought only one object is created. But my question is, if ) creates two objects, (where one is a new String(), and the other is "xyz"), then how can the reference x (as in String x) point to two objects (new String() and "xyz" at the same time?

Thanks,

-Vijay
Vijay Gade
Ranch Hand

Joined: Jul 04, 2005
Posts: 81
Hi,

Got the answer to that one.

String x = new String ("xyz");

is equivalant to

char data[] = {'x', 'y', 'z'}; //--> Array Object #1
String x = new String (data); //--> String Object #2

That's two objects.

Thanks,

-Vijay
Dinesh Kodiyat
Greenhorn

Joined: Oct 31, 2002
Posts: 6
That was realy interesting one!.Thanks


<b>Dinesh</b><br /><b>All Time Java Addict!</b>
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Hi,

Got the answer to that one.

String x = new String ("xyz");

is equivalant to

char data[] = {'x', 'y', 'z'}; //--> Array Object #1
String x = new String (data); //--> String Object #2

That's two objects.


That's not quite how it works. The original line of code contains a String literal. When the class is loaded, a String object is created on the heap based on the character sequence "xyz", and a reference to this String is retained in a pool. When the method containing this line is run, a new String is created on the heap, containing the same character sequence as the previously created String object. After this code executes, there are two Strings on the heap, each containing the character sequence "xyz".

If you compare the object references with x == "xyz", you'll find it returns false, meaning the references point to different objects.

In practical code, lines such as the one above (new String("whatever"))are impractical and wasteful, as they create unnecessary String objects. It's useful for testing purposes, however, to know what's going on, and that such a line results in two separate String objects.

Hope this helps!
Vijay Gade
Ranch Hand

Joined: Jul 04, 2005
Posts: 81
Hello Steve,

Thank you very much. I guess I'm the third kind of the guy who cannot count!

-Vijay
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: number of objects created?