• 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

a problem about mock.

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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. }
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Seany Iris
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the answer is "Prints true".
Maybe it is wrong.isn't it?
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when you compile and run the code yourself?
Marcus
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it!
I compiled and run it, and it print out nothing!
The answer is wrong!
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic