Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a problem about mock. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a problem about mock." Watch "a problem about mock." New topic
Author

a problem about mock.

Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
What will be the output when the following code is run?

1. class ThisClass{
2. public static void main(String args[]){
3. Object o=(Object)new ThisClass();
4. Object s=new Object();
5. if(o.equals(s))
6. System.out.println("true");
7. }
8. }


help you means help me
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Answer: No output
The default behaviour of class Object equals() method is to compare the reference values. If the reference values are the same, then true is returned, otherwise false.
To see an example of this, play around with the String class and StringBuffer class. The wrapper String class overrides the equals() method to compare the string literals, but the StringBuffer class just compares the reference values!
String s1 = new String("abc");
String s2 = new String("abc");
s1.equals(s2); // returns true
StringBuffer sb1 = new StringBuffer("abc");
StringBuffer sb2 = new StringBuffer("abc");
sb1.equals(sb2); // return false
[ January 23, 2002: Message edited by: Rajinder Yadav ]

<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
But the answer is "Prints true".
Maybe it is wrong.isn't it?
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
What happens when you compile and run the code yourself?
Marcus


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Nisheeth Kaushal
Ranch Hand

Joined: Jul 20, 2001
Posts: 87
Oh Marcus,
Its great to see a mail from a person like u.
Well in this question i think that even there is no need to cast the object of ThisClass, b'coz every object is a blueprint of Object class bydefault.
And this can be more understandable if explain it by an example-
Suppose we have a vehicle class, which gives us a methodology that whenever we can implement, we get a new vehicle.
And suppose we have a car class, which extends the vehicle class, and provide another object or methodology or formula or blueprint that on implementing gives us a car.
but this does not mean that methodology of implementing the vehicle is also the method for implementing a car.
HTH
-Nisheeth
Mark Nolan
Greenhorn

Joined: Jan 23, 2002
Posts: 19
I just cut and paste the code from above compiled and ran ( just to be sure ) and I deffinatly get no output from it.
I think if in doubt with some code the best bet is to follow Marcus's sugestion and compile and run the code to see what it does, from there you can go on to work out why it's done it.
Mark
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
I got it!
I compiled and run it, and it print out nothing!
The answer is wrong!
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
What was the answer supposed to be? You posted the original question, but not the answers, or the "correct" answer according to the test.

Rob


Rob
SCJP 1.4
eric sun
Greenhorn

Joined: Jan 22, 2002
Posts: 5
Originally posted by Seany Iris:
What will be the output when the following code is run?

1. class ThisClass{
2. public static void main(String args[]){
3. Object o=(Object)new ThisClass();
4. Object s=new Object();
5. if(o.equals(s))
6. System.out.println("true");
7. }
8. }


The answer is No output. Why ? Because the method equals() be overridden.
The equals() method in Object and all Object extends it. So you can override this method in new SubClass.
String , Integer .... etc class all override this method, so you can see if their content is same the result is true. But more object do not rewrite this method, the result is all false, like StringBuffer or Untitled5 .
Exp:
public class Untitled5 {
public static void main( String[] args[]){
Object obj0 = new Object();
Object obj1 = new Object();
Object obj2=(Object) new Untitled5() ;
Untitled5 u5_0 = new Untitled5() ;
Untitled5 u5_1 = new Untitled5() ;
Integer i_0 = new Integer(3);
Integer i_1 = new Integer(3);
System.out.println("obj0 equals obj1 ? "+obj0.equals(obj1));
System.out.println("obj0 equals obj2 ? "+obj0.equals(obj2));
System.out.println("u5_0 equals u5_1 ? "+u5_0.equals(u5_1));
System.out.println("i_0 equals i_1 ? "+i_0.equals(i_1));
}
}
The result is :
obj0 equals obj1 ? false
obj0 equals obj2 ? false
u5_0 equals u5_1 ? false
i_0 equals i_1 ? true


Nice to meet you here. I like Java.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a problem about mock.