• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strings and equality

 
Ranch Hand
Posts: 94
1
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you read this?
 
Marshal
Posts: 79236
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i saw that question on facebook.
 
Campbell Ritchie
Marshal
Posts: 79236
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please show us the facebook link.
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is the link:

https://m.facebook.com/groups/183792688409636?view=permalink&id=593934157395485&refid=18&_ft_&__tn__=%2As
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To:Paweł Baczyński


Thank you
 
reply
    Bookmark Topic Watch Topic
  • New Topic