Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

equals()

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the output of the following code?

1: class MyClass
2: {
3: static int maxElements;
4:
5: MyClass(int maxElements)
6: {
7: this.maxElements = maxElements;
8: }
9:
10: }
11:
12: public class Q19
13: {
14: public static void main(String[] args)
15: {
16:
17: MyClass a = new MyClass(100);
18: MyClass b = new MyClass(100);
19:
20: if(a.equals(b))
21: System.out.println("Objects have the same values");
22: else
23: System.out.println("Objects have different values");
24: }
25: }

A) Compilation error at line 20. equals() method was not defined.
B) Compiles fine, runtime exception at line 20.
C) Prints "Objects have the same values".
D) Prints "Objects have different values".

Answer is D

Can anyone please explain, I'll highly appreciate help. Thanks so much
uzma
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case MyClass hasn't overridded equals method .so equals method checks whether a and b are referring to the same instance , in this case they are NOT equal.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
u did not override the equals() of objest class ,so default behaviour of equals method is as follows:-


"The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true)."

in ur code a and b are two different objects,so a==b results in false.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey plz can anyone explain wat to modify to get "they are equal".
i got a litle confused abt this method.
wat u mean we didnt override the equal() mnethod.
explain using the same example.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harish and Uzma,

About the "equals()" method, you must be knowing it is defined in the Object class. So, each n every class in JAVA will have that method. But default implementation of "equals()" method(in the Object class) compares the "reference" values of the two( passed reference and "this" reference on which it is invoked.).

But, according to your design, this may not be sufficient. Suppose, According to your program logic, two objects are considered to be equal if "both the reference are of same class, and some attributes are of same value". then in that case you should override "equals()" method in your class.

Hope this will help, better to read about both, overridding "equals" and "hashcode" method.

Thanks and Regards,
Mausam
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just in case the word "reference" is causing you any confusion, let me clear it up for you. When objects are created, they are placed in locations in memory. In line 17, an object of type MyClass is created. The object is then put in memory. The variable a holds the information of where in memory it actually is. This information or location is called a reference. In line 18, another object of type MyClass is created and its reference is held in a variable named b. Keep in mind that this new object b is put into a different location in memory then object a. Each object inherits the method equals() from the Object class. Unless overwritten for different use, the equals() method checks to see whether two objects point to the same reference. In the case of your code, a and b point to different references even though they hold the same information and in turn the equals() method returns false. The equals() method needs to be overwritten to check that the contents of two objects are equal. If not overwritten it will check whether two object references are equal.


12: public class Q19
13: {
14: public static void main(String[] args)
15: {
16:
17: MyClass a = new MyClass(100);
18: MyClass b = new MyClass(100);
19:
20: if(a.equals(b))
21: System.out.println("Objects have the same values");
22: else
23: System.out.println("Objects have different values");
24: }
25: }
 
uzma Akbar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code will give

1: Byte b1 = new Byte("127");
2:
3: if(b1.toString() == b1.toString())
4: System.out.println("True");
5: else
6: System.out.println("False");

A) Compilation error, toString() is not avialable for Byte.
B) Prints "True".
C) Prints "False".

But can anyone explain this why the answer is C in this case though object is only one.


and if the reference thing is correct then:

Double a = new Double(Double.NaN);
2: Double b = new Double(Double.NaN);
3:
4: if( Double.NaN == Double.NaN )
5: System.out.println("True");
6: else
7: System.out.println("False");
8:
9: if( a.equals(b) )
10: System.out.println("True");
11: else
12: System.out.println("False");

A) True
True

B) True
False

C) False
True

D) False
False
why is the answer C and why over here equals() is true. while in the last it was not.

Sorry for asking too many queries but all of them are related.
I'll appreciate your effort in explaining. Without you it must have been difficult.
Thanks again!

Uzma
 
reply
    Bookmark Topic Watch Topic
  • New Topic