File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Number of String objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Number of String objects" Watch "Number of String objects" New topic

Number of String objects

Apurva Gaonkar

Joined: Feb 07, 2011
Posts: 7
hey ,

Thats wonderful clearing the concepts of references & values .
I just want to make things more clear here .

String a="Hello";
String b="HI";

String x=new String("ABC");
String y=new String("XYZ");

Can anyone please tell how many reference and how many object are there in four different expressions?

My Answer would be 4 reference variable and 2 Objects.
a,b,x,y are reference variables of Type String

And There are 2 Objects of Type String . As we have new String("ABC") and new String("XYZ");
here does these Objects have names ? or can we name them ?

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43368
Please don't post a new question on an existing thread. And we require you tell us where that question is from. Separating thread.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43368
I separated those posts because they were unrelated to the original thread.

Please search the fora; there are many questions asked similar to this one.
Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
Apurva Gaonkar wrote:And There are 2 Objects of Type String

Actually there are 6.
As Campbell said, search the fora for similar questions if you're not sure why.

Giovanni Lima

Joined: Mar 08, 2011
Posts: 13
In fact, you'd created 4 strings into the pool and two objects in the heap, which refer to two strings in the pool.

You can see the difference if try make any compare:


I agree. Here's the link:
subject: Number of String objects
It's not a secret anymore!