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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Objects

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the output of the following code?Can someone explain the answer to me..? The base class should define the objects a, b or what?anything i shuld read to clear this concept???
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. equals() method was available in base class Object.
So it won't give any compilation error.
Here MyClass is a user-defined class,
so the user has to implement equals() method
according to his requirments.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
faizaharis,
If you change MyClass to the following, it prints "Objects have the same values".
class MyClass
{
static int maxElements;
MyClass(int maxElements)
{
this.maxElements = maxElements;
}
public boolean equals(Object obj)
{
if (this.maxElements == ((MyClass) obj).maxElements)
return true;
else
return false;
}
}
The way I implement the equals method is used only as a illustration.
Try to understand it, do some research, and come back to tell us what you have learned about "equals", "==", and how these two methods are supposed to be used in java.lang.Object and its subclasses.
Edward
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Faiza... All classes inherit the equals() method from the Object class. Unless the equals() method is overridden in the class that you declare, it will act in a way similar to ' == '.
In your code, since 'a' and 'b' are two different instances of class Myclass (as they were created using the new keyword leading to two new objects on the heap) the == operator will return false, as this operator compares the references for the two objects.
The two references are different (as they are different objects) and thus == returns false....
 
if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!