• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

References in JAVA

 
Ranch Hand
Posts: 214
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before anyone answers your question, let's hear your own answer and your reasoning.
 
Rd Dari
Ranch Hand
Posts: 214
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, you're right. I didn't read them carefully enough.
 
Rd Dari
Ranch Hand
Posts: 214
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For clarity

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


Thanks once again.
 
reply
    Bookmark Topic Watch Topic
  • New Topic