Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

== & equals()

 
ram shah
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would be the output?

code
-----------------------------------
public class EqualsTest{

public static void main(String[] args){
String s1 = "abc";
String s2 = s1;
String s5 = "abc";
String s3 = new String("abc");
String s4 = new String("abc");
//if we remove the brackets around "s1 == s5' it gives a different result.
System.out.println("== comparison : " +(s1 == s5));
System.out.println("== comparison : " +(s1 == s2));
System.out.println("Using equals method : " +s1.equals(s2));
System.out.println("== comparison : " +s3 == s4);
System.out.println("Using equals method : " +s3.equals(s4));
}
}
-----------------------------------------

s1 & s2 point to the same object & have same content.
s3 & s4 have same reference.

I feel the output should be,
== comparison : false // incorrect.output is true
== comparison : true // correct
Using equals method :true // correct
== comparison : true // incorrect.output is false
Using equals method :true //correct

Can somebody explain?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if we remove the brackets around "s1 == s5' it gives a different result.


See the + (concatenation operator).
Suppose when you write:
System.out.println("== comparison : " +s1 == s5);
Comparison is made between "== comparison : abc" and "abc"



Thanks,
 
ram shah
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bhatt for your explanation. Pls explain the output also...
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ram,
What would be the output?

code
-----------------------------------
public class EqualsTest{

public static void main(String[] args){
String s1 = "abc";
String s2 = s1;
String s5 = "abc";
String s3 = new String("abc");
String s4 = new String("abc");
// if we remove the brackets around "s1 == s5' it gives a different result.
System.out.println("== comparison : " +(s1 == s5));
System.out.println("== comparison : " +(s1 == s2));
System.out.println("Using equals method : " +s1.equals(s2));
System.out.println("== comparison : " +s3 == s4);
System.out.println("Using equals method : " +s3.equals(s4));
}
}
-----------------------------------------

s1 & s2 point to the same object & have same content.
s3 & s4 have same reference.

I feel the output should be,
== comparison : false // incorrect.output is true
== comparison : true // correct
Using equals method :true // correct
== comparison : true // incorrect.output is false
Using equals method :true //correct

Can somebody explain?



look at the code below
String s1 = "abc";
String s2 = s1;
String s5 = "abc";
String s3 = new String("abc");
String s4 = new String("abc");
// if we remove the brackets around "s1 == s5' it gives a different result.
System.out.println("== comparison : " +(s1 == s5));
System.out.println("== comparison : " +(s1 == s2));
System.out.println("Using equals method : " +s1.equals(s2));
System.out.println("== comparison : " +s3 == s4);
System.out.println("Using equals method : " +s3.equals(s4));

"abc" will be created in string pool so when you again assign it to another string it will again refer to it so in the below output it will be true

System.out.println("== comparison : " +(s1 == s5));


Regards
NIK
SCJP 1.5
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A more in depth explanation can be found here: http://www.javaranch.com/journal/200409/Journal200409.jsp#a1
 
ram shah
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nik & Manfred for your help..

The document was of great help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic