aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt on Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt on Strings" Watch "Doubt on Strings" New topic
Author

Doubt on Strings

Nikhil Bansal
Ranch Hand

Joined: Jan 24, 2005
Posts: 60
Hi All,

Please give the output for the foll code and with reasons.

class MWC114 {
public static void main(String[] s) {
String s1 = new String("ABCDEFG"), s2 = new String("EFGHIJ");
String s3 = s1.substring(4,7), s4 = s2.substring(0,3);
System.out.println((s3 == s4) + "," + (s3 + s4).equals(s4 + s3));
}}

I feel that since s3 = "EFG" and s4 = "EFG", then these two Strings are interned and thus s3 and s4 point to the same String object. So the result of s3 == s4 must be true.

Regards

Nikhil Bansal

Regards

Nikhil Bansal


ban$al
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

Well I havn't typed it in, but as I understand this, the thing about the Strings in the string pool only counts for literal strings (I'm still in the studying phase so please correct me if I'm wrong).

So
String a = "hello world"
goes into the pool
and if I later do
String b = "hello world"
then these 2 Strings will point to the same object in the pool. Every String you 'create' by substringing or by adding strings will not be in the pool. It will be objects on the heap.

regards,
Mark


I know where my towel is. (SCJP 5, OCPJWCD)
[Free Quiz Tips for a fun night with friends or family] Flash games
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

oops that should have been code instead of quote.. and of course ; after the statements...
Nikhil Bansal
Ranch Hand

Joined: Jan 24, 2005
Posts: 60
Hi Mark,

Thanks for the reply.

Can you post me some links for Strings.I need to clear my concepts for Interning strings.

Regards

Nikhil
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

Well I don't use the internet for that. all my knowledge on the subject comes from "Sun Certified Programmer for Java 5 - by Kathy Sierra and Bert Bates"

Regards,
Mark
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
Originally posted by Mark Uppeteer:
Every String you 'create' by substringing or by adding strings will not be in the pool. It will be objects on the heap.

Every string we create by adding strings may be INTERNED IF the string-concatentation expression is a compile-time constant expression. For example, consider the following code:-



Yours,
Abdul Rehman.


SCJP 5.0 (100%)
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

sorry for not posting links to Strings,
but your question could also be answered by a look into the API.

The entry about substring() starts with:
"returns a new String...".

and
String s1=new String("A");
String s2=new String("A");
System.out.println(s1==s2);
prints false.


Yours,
Bu.


all events occur in real time
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9426
    
    2

Hi Guys,

When you apply a method on a String, it creates a new one in the memory.

Try for example this,

String s1 = new String("ABCDEFG");
String s3 = s1.substring(4,7); //EFG
String s5 = "EFG";
String s4 = "EFG";
System.out.println(s3 == s4); //false
System.out.println(s5 == s4); //true
System.out.println(s5 == s3); //false


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on Strings