• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Strings

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanku very much srinivasan and keith.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Srini. I got it.
 
Acetylsalicylic acid is aspirin. This could be handy too:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic