File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings

 
Ram Reddy
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ram Reddy
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanku very much srinivasan and keith.
 
Srinivas Kumar
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Srini. I got it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic