• 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

String Literals

 
Ranch Hand
Posts: 61
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s="abc";
String s1="abc1";

Will these create a new string object?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are both string literals. I don't understand what you are asking.
 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:They are both string literals. I don't understand what you are asking.




I mean to ask will these create a new String object in the heap?
 
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aneek Banerjee wrote:

Jeanne Boyarsky wrote:They are both string literals. I don't understand what you are asking.




I mean to ask will these create a new String object in the heap?



Yes "abc" and "abc1" are different. So there'll be two String literals in the heap, i.e "abc" and "abc1"

Had it been like this:
String s = "abc";
String s1 = "abc";

Java heap will only have 1 String literal, i.e "abc"
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And tell me, Aneek, what will happen in the following case:

 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dieter Quickfend wrote:And tell me, Aneek, what will happen in the following case:



Dieter This will give abc1 (concatenation) with s1 as an reference.
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aneek Banerjee wrote:

Dieter Quickfend wrote:And tell me, Aneek, what will happen in the following case:



Dieter This will give abc1 (concatenation) with s1 as an reference.



Yes, it'll. And there'll be 3 String literals in the heap.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aneek,

String literal pool and JLS rules for String objects with "abc" and "abc1".

Regards,
Dan
 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dan
 
Ranch Hand
Posts: 68
MyEclipse IDE PHP Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The three literals will be s, s1 and "1"?
 
Dieter Quickfend
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"abc", "abc1" and "1". And in the following case?

 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also worth reading, StringsLiterally
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic