Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Using Objects as Parameters

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hiya folks, well, just like the greatest rock and roll band in the world, ever - The WHO, i'm back, after an almighty almost 3 year absence. Close family bereavments, job changes and site secondments aside, i've decided to wind up the old Java elastic band, and dive back into it. Are the Java tutorials still being done [think i completed the 1 to 99 in words exercise].

I've got back into the books, and am currently on a chapter called "A closer look at classes and methods".

The topic of "Using Objects as Parameters" has just cropped up.

The code i'm slightly confused on, is the following:-




I basically understand what the program is doing, except for the lines



Could someone explain to me what is happening in these 2 lines?

Thanks in advance

Steve
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What part don't you understand?

The equals(Object) method is defined in the Object class, which is the ultimate base class for all other classes. The class Test overloads equals() so for cases where it's called with a Test object as an argument. equals() returns a boolean value, so those two System.out.println() lines will print the string and either "true" or "false". e.g. "obj1 == obj2: false".

Did that answer your question? You understand what the constructor and equals() method in the Test class are doing, right?
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Test ob1 = new Test(100,100);
Test ob2 = new Test(100,100);

System.out.println("ob1 == ob2: " + ob1.equals(ob2));

would make more sense as an example

here ob1 == ob2 is false where as ob1.equals(ob2) is true.

This is because == will compare the hashcodes (usually the memory location) of the 2 objects. They're distinct objects and therefore not in the same memory location.

.equals, on the other hand, compares the contents of the object, which, in this case, are equal.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println("ob1 == ob2: " + ob1.equals(ob2));

That's what was throwing me off

System.out.println("ob1 == ob2: " + ob1 == ob2);

That makes more sense. Or...

System.out.println("ob1 equals ob2: " + ob1.equals(ob2));
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point, mine should read
System.out.println("==: " + ob1 == ob2 + " : equals: " + ob1.equals(ob2));
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like knowledge of "order of precedence of operators" is required for this discussion by looking at the last couple of posts.
Here's some fun:

[ August 28, 2006: Message edited by: Tony Morris ]
 
Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic