• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

String objects

 
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what happens on equating two strings i.e. if sa and sb are two strings and statement is like
sa=sb;
does the reference location of sb is assigned to sa
or value at reference location of sb is copied to reference location of sa.???
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You first answer: "the reference location of sb is assigned to sa". And to be overly specific, you say "if sa and sb are two strings ". Actually sa and sb are reference variables of type String. These variables can point at different String instances, the same String instance, or no String instances at all (null).
 
Gursewak Singh
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply.......
can you tell me the difference between these two codes..........


String a,b;
a=new String("abc");
b=new String("abc");
boolean r=a==b;
System.out.println(r);

and..............

String a,b;
a="abc";
b="abc";
boolean r=a==b;
System.out.println(r);

both these codes run correctly.but gives the different result.....
 
Greenhorn
Posts: 14
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java behaves differently with string literals (e.g a="abc") when compared to creating a String object explicitly on the heap (e.g new String("abc")).

The difference is that the JVM operates a string constant pool (SCP) and any string literals are entered into the pool at load time.

a="abc"; will enter the "abc" into the SCP, then anywhere "abc" is referred, the reference will point to the same "abc" in the SCP.

a=new String("abc"); is explicitly creating a new string object with the value "abc". For this reason the top code block creates two different String objects, therefore a != b.

The bottom code block creates the String in the SCP after a="abc";, then when you do b="abc"; the JVM sees that string already exists in the SCP and refers b to it. Therefore a==b.

Also, further to the above, you will need to remember that a=new String("abc"); will create TWO string objects, first in the SCP (at load time, because it was created as a literal) and another one on the heap (being referred to by a), however, a will refer to the new object on the heap.
 
Saloon Keeper
Posts: 14499
325
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't forget that both Strings will be created on the heap, and that the String pool only maintains references to Strings.
 
Gursewak Singh
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you sir........
but one question is that, will explicit creating cause overload or wastage of memory........
 
Stephan van Hulst
Saloon Keeper
Posts: 14499
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Strings are immutable objects, so if you have one instance for every value you need, that's more than enough. Explicit creation will usually waste memory, or at least CPU cycles.
 
Gursewak Singh
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok...
and
Thank you all for suggestion.....
 
get schwifty. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic