• 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

Doubt About Object Reference Casting Rule

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
friends,
RHE book states following compile-time rule for Object ref casting,
Oldtype: non-final class & Newtype : Array -> Newtype must be Object

Oldtype: Array & Newtype: non-final class -> Oldtype must be Object

Object --> Fruit --> Citrus (implements Squeezable) -->Lemon
-->Tangelo
-->Grapefruit
Tangelo tang = new Tangelo();
Object[] objAry;
objAry = tang; // as per rule this should complile, but won't
However, Object ob = tang; //This will compile
Can somebody clarify this rule.
sujit

 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sujit,
I don't see any problem with the reasoning. You are complaining about trying to convert an non-final class to an array. That is not possible in Java. Your third line gives you the answer:
non-final class to Array -> Newtype must be Object.
You are trying to read it with the last word being Array instead of Object!
In Java an array is an object therefore we can always do the following:
int[] intArray = new int[25];
Object ob = intArray
or
MyClass c = new MyClass();
Object ob = c;
We can not however, do the following:
int[] intArray = new int[25];
Object[] obArr = intArray;
or
MyClass c = new MyClass();
Object[] obArr = c;
In your case you could have performed the following:

Regards,
Manfred.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sujit,
I have the same doubt.
RH rule for casting reference:
Oldtype: non-final class & Newtype : Array -> Newtype must be Object
When Newtype is first said as Array, how come the rule is that Newtype must be Object? We are talking about array as new type, not Object! Then we try Object[] as new type, wrong too.
You may argue that an array is an object; but an object of Object type is not always an array. So I think the book should make this more clear.

Howard
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What RHE is saying is that if you are trying to convert a non-final class to an array, the best you can do is convert it to an Object, the superclass of an array. Basically that means you can't convert a non-final class to an Array.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I don't own the RHE book and am not sure what the rules are referring to; however, you can cast/assign arrays if the array type is legally assignable.

The above assignments and cast compile and run ok.
If you comment out '1', the code compiles ok but produces a runtime error as Fruit cannot be legally assigned to an Apple type.
 
Howard Hu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jane,
Thanks. The issue here is not about whether you can cast an array to an array. It is about one rule in the book talking about casting from a non-final class to an array. I think the discussion so far has made that issue very clear.
--Howard
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add to the discussion, we've had a handful of readers write in and report our explanation here as an error. Possibly the rule is too terse at first sight, but it is correct. Thanks to Bill and Manfred for explaining our intent so clearly!

------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Sujit Kurtadikar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to Manfred and Bill for giving clarification about my doubt.
special thanks for Michael Ernest, really I didn't expect the reply from author of book. Thanks Michael.
 
reply
    Bookmark Topic Watch Topic
  • New Topic