This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "string" Watch "string" New topic


Madan Mohan
Ranch Hand

Joined: Dec 05, 2008
Posts: 30
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�);
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));

the above program flashing the output like this:

== comparison : true
== comparison : true
Using equals method : true
Using equals method : true

can someone explain why the heck System.out.println(�== comparison : � + s3 == s4); is returning false.....

help would be very much appreaciated
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
hi madan...

when you are using new keyword then it is going to create different objects in heap.

SCJP5 and SCWCD1.5
Think Twice Act Wise...
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

This question is discussed SO SO MANY TIMES.
Just search in the forum .
And here

Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
wood burning stoves
subject: string
Similar Threads
Doubt with == ( double equals ) method.
String code
== & equals()
String question
(s1==s5) Vs s1==s5