• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding scjp 1.5

 
anveshana bandu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai

Please explain this...........very urgent....

1. public class EqualsTest{
2. public static void main(String args[]){
3. Long L = new Long( 7 );
4. Integer J = new Integer( 7 ) ;
5. if( L.equals( J )) System.out.println("Equal");
6. else System.out.println("Not Equal");
7. }
8. }

The out put is "Not Equal".....How???
Content is equal............
I did not understood how equals method checks whether two objects are equal...
Please explain how equals() method works....
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because their classes are different (a Long is NOT an Integer).
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15482
43
Android IntelliJ IDE Java Scala Spring
 
srk bhushan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here the two classes are different, you cant compare to class objects with equals method. If you want you have to overload the equals method in your class. Using equals method you can compare primitive and reference variable's equality but not objects.
 
venkat kaka
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
equals method works on the data of the objects if both object are of the same class.
 
anveshana bandu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much to you all for giving reply to my doubt...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic