Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception  RSS feed

 
xie li
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 try {
2 if ((new Object))(.equals((new Object()))) {
3 System.out.println("equal");
4 )else{
5 System.out.println("not equal");
6 }
7 }catch (Exception e) {
8 System.out.println("exception");
9 }
What is the result?


A. equal

B. not equal

C. exception

D. Compilation fails.
 
Kavitha Govindaraj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
There are compilation errors in your code. The corrected version is



Upon execution this code will print not equal.
 
Naresh Gunda
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default implementation of equals() method in Object class returns 'true' only if the object is compared with itself, that is, as if the equality operator == had been used to compare aliases to the object.

In the given code Two different objects are created. Hence not equal.

Regards
Naresh
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!