Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

== & equals()  RSS feed

 
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?
 
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...
 
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
 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!