• 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

equals() v/s == operator?

 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A very interesting thing I have pointed out here



output false.

This code is fine, it won't give any compilation error as both are Objects.

It gives output false as both objects are of different class.

Whereas when I test manually (t==sb), it gives compilation error--incompatible type.

It seems that you can test the references of two object of different classes by equals but not by == operator.

Why the compiler is not compiling t==sb. If it gets compiled, it will surely run and give the same output false.

There is some flaw here.

Same thing you can test by equals but not by ==


Naseem
[ June 30, 2006: Message edited by: Naseem Khan ]
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


A compile-time error occurs if it is impossible to convert the type of either operand to the type of the other by a casting conversion (�5.5).


If you do the following code, it will work for "=="
System.out.println((t == (Object)sb);
System.out.println((Object)t == sb);
System.out.println((Object)t == (Object)sb);
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks wise. I compiled your three cases and it compiles and runs giving same output false.

It means both are consistent with each other. They are not conflicting with other.

Thanks onnce again

Regards

Naseem
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic