Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Just to bring to the notice of the junta

 
Preety Narashimhan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Khalid Azim Mughal’s book which I am referring to prepare for the JCP1.2 exam I have found this typos. In the chapter 3.Operators and Assignments, when he refers to equals method, it mentions this error:
Pizza pizza1= new Pizza(“VeggiesDelight”) ;
Pizza pizza2= new Pizza(“VeggiesDelight”) ;
boolean test3 = pizza1.equals(pizza2); // false
which is not right. The value returned is true since the equals method compares the value rather than the memory location of the objects.
If this is a repetition please ignore
;)
[ August 31, 2002: Message edited by: Thomas Paul ]
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may or may not be correct, depending on whether the Pizza class overrode the equals() method, and if so, what the overridden method does.
[ August 31, 2002: Message edited by: Ron Newman ]
 
Preety Narashimhan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ron,
There seems to be no explicitly mentioned overridden method of the equlas method. and when i tried running that piece of code it gave me true which confirmed my understanding. Please confirm if there is any mistake from my side.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default equals method is a reference comparison. If it was not overridden then it would return false in the example above. Why not show us the complete code for the Pizza class?
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also need to check whether any superclass of Pizza overrode the equals() method. What class does Pizza extend?
 
Preety Narashimhan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Thomas,
It is not an entire pizza class written in the book he just explains the use of the default "equals()" method.
Correct me if i am wrong ..
String movie1= "This is it";
String movie2= "This is it";
boolean b = movie1.equals(movie2); // returns true
But instead if i had done
String movie1= new String("This is it");
String movie2= new String("This is it");
boolean b = movie1.equals(movie2);
What wud the value of "b" be? I think it wud be false.
 
Preety Narashimhan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i wanted to add ..
but when i ran both pieces of code it gave teh boolean value to be true.
This is purely considering the default String class's equals() method.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will be true because String overrides the equals() method, and String.equals() compares the strings character-by-character for equality.
I don't see how you could have run the other program at all unless you had code for the Pizza class. If you do have such code, please show us what class Pizza extends.
[ August 31, 2002: Message edited by: Ron Newman ]
 
Preety Narashimhan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Ron,
Since that part was just explaining the concept of the equals() method, instead of making the Pizza class i introduced two Strings in one of my classes.no separate code for the Pizza class was written.
Basically, when we use the new operator a separate memory area is created for the object and hence the equlas method returns a false.
ok..got it tx a lot
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the author of the book didn't indicate what class Pizza extends, then he was remiss and it is not possible to determine the correct answer.
Assuming that class Pizza doesn't explicitly extend another class, and doesn't define its own equals() method, then it will use the Object.equals() method which returns true only if the two object references are the same.
[ August 31, 2002: Message edited by: Ron Newman ]
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ron Newman:
If the author of the book didn't indicate what class Pizza extends, then he was remiss and it is not possible to determine the correct answer.

Actually Mughal did (p. 61), and Preety overlooked it.
Mughal wrote:
//Pizza class does not override the equals() method,
// can use either equals (inherited from Object) or ==
Pizza pizza1= new Pizza(“VeggiesDelight”);
Pizza pizza2= new Pizza(“VeggiesDelight”);
...
boolean test3 = pizza1.equals(pizza2); // false
[ September 01, 2002: Message edited by: Thomas Paul ]
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:sigh: aw hell this just isn't my day... :roll:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic