• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Objects as Parameters  RSS feed

 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • 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
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • 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?
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • 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.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Good point, mine should read
System.out.println("==: " + ob1 == ob2 + " : equals: " + ob1.equals(ob2));
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!