File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
class MyClass10
{
public static void main (String args[])
{
String str1="Java";
String s1 = "Ja";
String s2 = "va";
System.out.println(str1 == s1+s2);
System.out.println(str1 == "Ja"+"va");
System.out.println("Java" == "Ja" + "va");
}
}


out put is false,
true,

true..

can any one explain me.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by rami reddy marri:
class MyClass10
{
public static void main (String args[])
{
String str1="Java";
String s1 = "Ja";
String s2 = "va";
System.out.println(str1 == s1+s2);
System.out.println(str1 == "Ja"+"va");
System.out.println("Java" == "Ja" + "va");
}
}


out put is false,
true,

true..

can any one explain me.


"Ja" and "va" are compile-time constants and their concatenation can be performed at compile-time.
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
I am not able to understand this
When the program runs,
str1 -> "Java" (created in a pool, lets say ObjRef1).
s1 -> "Ja" (created in a pool, ObjRef2)
s2 -> "va" (created in a pool, ObjRef3)

First Line-
str1 == s1+s2;

s1 + s2 will form "Java" and since "Java" is already present in the pool (ObjRef1) it must return this reference.

Not able to understand why it prints false
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi swarna,

str1 == s1+s2;

At runtime, s1+s2 will create another String in HEAP and as usually tries to place another Object in Pool. but it finds "java" already there. so does nothing to pool.

str1 -- > pool reference
s1+s2 ---> new heap reference

both are not equal.

Additional : Regarding s1+s2
If any one of operands is String reference then Heap Object will be created.
If none of them include String reference then tries to create in POOL if doesn't exist.

Hope its clear for you.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
Thanku very much srinivasan and keith.
Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
If I modify the program as follows (reshuffled the SOPs),

class MyClass10
{
public static void main (String args[])
{
String str1="Java";
String s1 = "Ja";
String s2 = "va";
System.out.println(str1 == "Ja"+"va");
System.out.println(str1 == s1+s2);
System.out.println("Java" == "Ja" + "va");
}
}

As per my understanding the output will be:
false
true
true

is this correct?
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi VSSM,

[B][/B]

1) str1 == "Ja"+"va" ---> true, beacuse "Ja" , "va" both String literals(in pool) when concatenated gives "Java" which will be try to create a string in pool. But finds "Java" already created so passes the same reference of str1. thats why true.

2)str1 == s1+s2 ---> false,

Here str1 pointing to String in Pool ---> "Java" in Pool

s1+s2 as we are using the reference , creates a new String in Heap and as usual tries to create another one in pool. but finds "Java" so gives up with creating String in pool. ----> "Java" in Heap(new one)

Hence both references are not equal. so output false.

3)"Java" == "Ja" + "va"

"Java" ---> tries to create one in pool, but gives up as it is already there. and returns the reference of pool string.

"Ja" + "va" --> as explaned in case 1) tries to create "Java" in Pool, and returns the reference of existing pool String.

Hence true.
Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
Thanks Srini. I got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings