• 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

Reg. == for object references

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is regarding comparing object reference using ==

If we are comparing object references using == , then Both the operands should be type-compatible, that is it must be possible to cast one into the other's type, otherwise compile time error occurs.

( taken for Khalid. book )
Can someone explain this to me ?
Consider the code:
Integer i = new Integer(1);
Long l = new Long(1);
if( i == l)
...

String s = new String("an");
StringBuffer sb = new StringBuffer("an");
if( s == sb)
...
Both the above give compile time error.
Is it something to do with the peer classes( two classes at the
same level of hierarchy )or unrelated classes which cannot be compared ?
Can someone explain to me in better terms .

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Angela,
You have hit the nail on the head when you say that your examples are peer classes. The java.lang package contains the heirarchy in question. Wrapper classes Integer and Long are both sub-classes of Number while String and StringBuffer are both sub-classes of Object. This means both pairs are peer classes.
Kind regards,
James.
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I just want to get clear
As Integer and Long are subclasses of Number class
they are peer classes for each other.Same is the case for String, StringBuffer which descend from java.lang.Object class , so they too are peer classes.
Am i right ?
I am not still clear about the comparison as to which operands
comparison can result in true.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Angela
Whether they are peer classes or completely unrelated it doesn't matter either one will cause a compiler error. the two must be directly realted in the hierarchy to be compared using ==, in other words one must castable to the other. The following code should explain:

In the line that gives the compiler error even though c2 and c3 can both be cast to c1 there is still an error because one can not be cast to the other.
From the JLS 15.21.3


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...


hope that helps you out
------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Dave Vick (edited August 31, 2001).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic