Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

References in JAVA

 
Rd Dari
Ranch Hand
Posts: 212
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5813
61
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before anyone answers your question, let's hear your own answer and your reasoning.
 
Rd Dari
Ranch Hand
Posts: 212
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephan van Hulst
Bartender
Pie
Posts: 5813
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, you're right. I didn't read them carefully enough.
 
Rd Dari
Ranch Hand
Posts: 212
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5813
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5813
61
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For clarity

 
Rd Dari
Ranch Hand
Posts: 212
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much all of you for the clarification on this topic.I understand that....


Thanks once again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic