Win a copy of Rust Web Development this week in the Other Languages forum!
    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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

doubt in mock exam java belt

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
program from mock exam java belt
explain program in full detail

01: class MyClass {
02: static int maxElements;
03:
04: MyClass(int maxElements) {
05: this.maxElements = maxElements;
06: }
07: }
08:
09: public class Q19 {
10: public static void main(String[] args) {
11: MyClass a = new MyClass(100);
12: MyClass b = new MyClass(100);
13:
14: if (a.equals(b))
15: System.out.println("Objects have the same values");
16: else
17: System.out.println("Objects have different values");
18: }
19: }

i thought the output will be
Prints "Objects have the same values"
but the output is "Objects have different values"
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please don't post duplicates. Continue this discussion here.
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
the O/P for this would be "Objects have different values" because class MyClass doesn't override the equals() method. Hence when you called a.equlas(b) the equals method of Object class get called.Object class's equlas() method returns true if both the reference varialble refers to the same object i.e
public boolean equals(Object o) {
.
. // Partial implementation of equals method in Object class..
.
if(this==o)
return true;
else return false;
}

MyClass a = new MyClass(100);
MyClass b = new MyClass(100);
here both a & b refers to two different objects on the heap..hence a.equlas(b) returns false...

change your code to::
MyClass a = new MyClass(100);
MyClass b = a;
System.out.println(a.equals(b));
it will now return true since both a & b refers to the same objetc on heap!!

Read Chapter 6 in K&B for more help..
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Again, this is a duplicate post. Please continue this discussion under the other thread.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic