• 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
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

== and equals question required

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1:
public class Myclass
{
int x;
Myclass(int i){x = i;}

public static void main(String args[])
{
Myclass m1 = new Myclass(100);
Myclass m2 = new Myclass(100);

System.out.println(m1.equals(m2));
}
}
The result is :false .
Why ? I think it should be :true.
2:
public class test{
public static void main(String[] a){
String s1 = "amit";
String s3 = "arit";
String s4 = "arit";
String s2= s1.replace('m','r');
System.out.println(s2 == s3);
System.out.println(s3 == s4);
}
}
The answer is :
false
true
But I think it should be :false false.

Thanks !!
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi PETER,
1. Unless u override the equals() method, it will just compares the object references. here in SOP(System.out.println) the Object class' equals() method is called. As u r creating new 2 references, it will compare the BITS(internal representation , nothing but address(long integer)). So they are differnt.It will result in false.

2. When u call String s1 = "amit"; it will internally calls like this,
String s1= new String("amit");
JVM's String handling is like that, while creating Strings, for OPTIMIZATION, it will point the 2 references to the same object whose contents are same.But in S2==S3 case, this is somewhat different, as you are calling replace()method, The String.replace method will create a new object, As I think at this stage it wont look for optimization.

Correct me If I am Wrong,
Regards,
Nandish
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

Whenever you use the "==" , it checks whether both the variables point to the same memory addres. However when you use "equals" it checks for the contents of the objects . However in case of strings they act a little bit differently, why ? I would suggest you to write small code to check that, it would clear all your doubts. try these two (equals and ==) with strings and objects, compare the outputs and you would see that you have cleared all your doubts, (and once you learn them yourself, I bet you would never forget them ).
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nandish KB:
Hi PETER,
1. ...just compares the object references. ... nothing but address(long integer)). ...
2. ...while creating Strings, for OPTIMIZATION, it will point the 2 references to the same object whose contents are same.But in S2==S3 case, this is somewhat different, as you are calling replace()method, The String.replace method will create a new object, As I think at this stage it wont look for optimization.



Pretty much. A few notes. The equals() method of Object is being used if you create a subclass of Object and don't override the method yourself; you will therefore get results that are precisely the same as ==

There's no indication in java as what exactly a reference is. (It's not a long integer).

2. If you create Strings using literals s="hello", the literal goes into a literal pool. Any other literals being assigened in the program also go in that pool. If there is a 2nd literal that's the same as any other literal in the pool, the reference points to the same address.
 
reply
    Bookmark Topic Watch Topic
  • New Topic