File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Literals

 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s="abc";
String s1="abc1";

Will these create a new string object?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33697
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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 Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Sidharth Khattri
Ranch Hand
Posts: 125
Java Linux Scala
  • 0
  • Mark post as helpful
  • send pies
  • 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"
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And tell me, Aneek, what will happen in the following case:

 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Linux Scala
  • 1
  • Mark post as helpful
  • send pies
  • 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.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • 0
  • Mark post as helpful
  • send pies
  • 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 Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dan
 
timo corn
Ranch Hand
Posts: 68
MyEclipse IDE PHP Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The three literals will be s, s1 and "1"?
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"abc", "abc1" and "1". And in the following case?

 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also worth reading, StringsLiterally
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic