aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Literals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Literals" Watch "String Literals" New topic
Author

String Literals

Aneek Banerjee
Ranch Hand

Joined: Jun 20, 2012
Posts: 61

String s="abc";
String s1="abc1";

Will these create a new string object?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30780
    
157

They are both string literals. I don't understand what you are asking.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Aneek Banerjee
Ranch Hand

Joined: Jun 20, 2012
Posts: 61

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

Joined: Sep 16, 2013
Posts: 121

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"


OCPJP 6 - 96%
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

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



Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Aneek Banerjee
Ranch Hand

Joined: Jun 20, 2012
Posts: 61

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

Joined: Sep 16, 2013
Posts: 121

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

Joined: Jul 09, 2001
Posts: 1180
Aneek,

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

Regards,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Aneek Banerjee
Ranch Hand

Joined: Jun 20, 2012
Posts: 61

Thanks Dan
timo corn
Ranch Hand

Joined: Oct 26, 2013
Posts: 68

The three literals will be s, s1 and "1"?


hth
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

"abc", "abc1" and "1". And in the following case?

Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Also worth reading, StringsLiterally


http://muhammadkhojaye.blogspot.com/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Literals