aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == & equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "== & equals()" Watch "== & equals()" New topic
Author

== & equals()

ram shah
Greenhorn

Joined: Jun 02, 2007
Posts: 28
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

Joined: Feb 28, 2007
Posts: 1707

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,


cmbhatt
ram shah
Greenhorn

Joined: Jun 02, 2007
Posts: 28
Thanks Bhatt for your explanation. Pls explain the output also...
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
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

Joined: Jun 04, 2007
Posts: 377
A more in depth explanation can be found here: http://www.javaranch.com/journal/200409/Journal200409.jsp#a1
ram shah
Greenhorn

Joined: Jun 02, 2007
Posts: 28
Thanks Nik & Manfred for your help..

The document was of great help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: == & equals()
 
Similar Threads
Strings...
string
Strings, StringBuffer and equals()
(s1==s5) Vs s1==s5
Doubt with == ( double equals ) method.