• Post Reply Bookmark Topic Watch Topic
  • New Topic

Strings and equality  RSS feed

 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone please explain the output of the program given below:



class A123
{
public static void main(String args[])
{
String s1="hello123";
String s2="hello"+String.valueOf(123);
String s3="hello"+"123";
String s5=new String("hello123");
if(s1==s2)
System.out.println("s1==s2");
if(s1==s3)
System.out.println("s1==s3");
if(s1==s5)
System.out.println("s1==s5");

if(s2==s3)
System.out.println("s2==s3");
if(s2==s5)
System.out.println("s2==s5");

if(s3==s5)
System.out.println("s3==s5");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s5);

}
}


why s1 is eaqual to s3 and not s2

 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read this?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before we give any more answers, please tell us where the question comes from, or whether you wrote it yourself, to avoid problems about copyright.
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i saw that question on facebook.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us the facebook link.
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To:Paweł Baczyński

i read the article you suggested and i know" ==" is used to whether the variable refers to the same object or not,and "equals()" is used to match the contents of the variables but i don't understand why s1 is equal to s3 and not equal to s2.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rrohit rakesh upadhyay wrote:i read the article you suggested and i know" ==" is used to whether the variable refers to the same object or not,and "equals()" is used to match the contents of the variables but i don't understand why s1 is equal to s3 and not equal to s2.

Ok, so now read this

Short story: "hello123" and "hello"+"123" are compile time constants so they go to the string pool. They represent the same string value in the string pool so they are the same object so == gives true.

"hello"+String.valueOf(123) and new String("hello123") are not compile time constants. They are evaluated on runtime creating new objects different from those in string pool, so == gives false..
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To:Paweł Baczyński


Thank you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!