wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Just to bring to the notice of the junta Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Just to bring to the notice of the junta " Watch "Just to bring to the notice of the junta " New topic
Author

Just to bring to the notice of the junta

Preety Narashimhan
Greenhorn

Joined: Aug 19, 2002
Posts: 28
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

Joined: Jun 06, 2002
Posts: 1056
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 ]

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Preety Narashimhan
Greenhorn

Joined: Aug 19, 2002
Posts: 28
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

Joined: May 05, 2000
Posts: 13974
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?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
You also need to check whether any superclass of Pizza overrode the equals() method. What class does Pizza extend?
Preety Narashimhan
Greenhorn

Joined: Aug 19, 2002
Posts: 28
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

Joined: Aug 19, 2002
Posts: 28
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

Joined: Jun 06, 2002
Posts: 1056
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

Joined: Aug 19, 2002
Posts: 28
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

Joined: Jun 06, 2002
Posts: 1056
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

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Mar 22, 2002
Posts: 1055
:sigh: aw hell this just isn't my day... :roll:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Just to bring to the notice of the junta
 
Similar Threads
equals() fails here
param
some prob with ...comparing object..
Where's the old object?
Assigning References