• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is equals() in A refer to its original style?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following statements are true?
A. The equals() method determines if reference values refer to the same object.
B. The == operator determines if the contents and type of two separate objects match.
C. The equals() method returns true only when the contents of two objects match.
D. The class File overrides equals() to return true if the contents and type of two separate objects match.
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by tian Lau:
Which of the following statements are true?
A. The equals() method determines if reference values refer to the same object.
B. The == operator determines if the contents and type of two separate objects match.
C. The equals() method returns true only when the contents of two objects match.
D. The class File overrides equals() to return true if the contents and type of two separate objects match.


My guess would be C & D. It DOES return true if two references refer to the same object, but equals() does not determine only if 2 references refer to the same object.
Savithri
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My answers would also be C and D.
However, I tried a small pgm and concluded that -
1.equals() returns true if both content and reference are same.
2.== returns true if just the refernces are same. which in turn makes the contents to be same too. right!!!
Any thoughts?
class Base {int i = 10;}
public class myjava {
private static void main(String [] args){
Base b = new Base();
Base q = new Base();
System.out.print( "before equating :");
System.out.println("b.equals(q)=" + b.equals(q) + " (b == q) is " + (b==q));
q = b;
System.out.print( "after equating :");
System.out.println("b.equals(q)=" + b.equals(q) + " (b == q) is " + (b==q));
}
}
 
Ranjan Sarangi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
forgot to put the results. Here are they -
before equating : b.equals(q)=false ( b==q) is false
after equating : b.equals(q)=true ( b==q ) is true
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the original equals() in the Object class works just like "==", returning true if the references are identical. But you should override it for your own class for "deep comparison"(contents not references).
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Tian,
1. A = true as equals() method determines if they return the same object - the return value can be True/False. So in my guess
C is not true
2. == operator checks to see if the sequence of the content of the 2 ojects is the same.Hence I would say that B is true.

Originally posted by tian Lau:
Which of the following statements are true?
A. The equals() method determines if reference values refer to the same object.
B. The == operator determines if the contents and type of two separate objects match.
C. The equals() method returns true only when the contents of two objects match.
D. The class File overrides equals() to return true if the contents and type of two separate objects match.


 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following statements are true?
A. The equals() method determines if reference values refer to the same object.
Sometimes yes, sometimes no - it depends on whether (and how) the equals() method has been overridden. In general though this statement is NOT TRUE.
B. The == operator determines if the contents and type of two separate objects match.
FALSE.
C. The equals() method returns true only when the contents of two objects match.
FALSE. Again, it depends on whether and how equals() is overridden.
D. The class File overrides equals() to return true if the contents and type of two separate objects match.
Close enough - I'll say TRUE. In the case of a File the concept of "matching" is rather loose. E.g. <code>new File("list.txt")</code>, <code>new File(".\list.txt")</code>, and <code>new File("LIST.TXT")</code> can all refer to the same file (on Windows at least), and so equals() considers them all to "match".
[This message has been edited by Jim Yingst (edited July 11, 2000).]
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic