This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes References in JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "References in JAVA" Watch "References in JAVA" New topic
Author

References in JAVA

Rd Dari
Ranch Hand

Joined: Feb 22, 2010
Posts: 206

Hi everyone,

There is an example of String, I came across this code during my interview but still I am not understand this code. And my Interviewer asked me that "how many references of String are there and if we use only two lines of this code then how many references are created?" . Please clarify it.




String st=new String("abc");
String st1=new String("abcdef");
String st2="abc";
String st3="abcde";



Thanks and Regards
RD

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

Before anyone answers your question, let's hear your own answer and your reasoning.
Rd Dari
Ranch Hand

Joined: Feb 22, 2010
Posts: 206

Yes I think that there are four references because when we create this type of code then we use 4 references and two objects.

Thanks and Regards
RD
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11250
    
  16

I believe there are four objects created. the literals "abc" and "abcdef" each cause an object to be created - even though each literal appears twice. However, since we are also using "new String", each of those calls creates ANOTHER object. So, after these four lines run, you have two strings each for "abc" and "abcdef".


Each reference - st, st1, st2, st3 - points to its own, unique String object.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

Fred, you missed the string literal "abcde", so that would make 5 objects. That's assuming it's not a typo.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11250
    
  16

Thanks, you're right. I didn't read them carefully enough.
Rd Dari
Ranch Hand

Joined: Feb 22, 2010
Posts: 206

Stephan van Hulst wrote:Fred, you missed the string literal "abcde", so that would make 5 objects. That's assuming it's not a typo.


How make 5 objects? I don't understand that.Please explain it.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

Well, each of the distinct String literals causes one object to be created: "abc", "abcdef" and "abcde". Then, you also create two new Strings explicitly (st = new String(...) and st1 = new String(...)).

So you create 5 objects.

The tree String literals will each have a reference to them in the String pool, and st2 and st3 wil also reference "abc" and "abcde". Finally, the two Strings that are explicitly created will be referenced by st and st1.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11250
    
  16

Something it took me a long time to 'get was that Strings work differently than other objects. Any time you see a literal string (something enclosed in double quotes), a String is made and put into the 'String pool'. If the exact same literal is seen more than once, like your "abc", the string is created one time, and both references point to the same String object.

Now, ANY time you see the 'new' operator used, that means a new object is created. So when you do "new String("foo")", you get TWO objects created. A string is put in the String pool for the "foo" literal (unless "foo" was already used somewhere else prior to this line), and a SECOND String object is created, this time NOT in the String pool, which also has a value of "foo". You get two-for-one when you write it that way.

which is why99% of the time, you shouldn't.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

For clarity

Rd Dari
Ranch Hand

Joined: Feb 22, 2010
Posts: 206

Thank you very much all of you for the clarification on this topic.I understand that....


Thanks once again.
 
 
subject: References in JAVA