• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Casting in java

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

I have dout in java upcasting is allowed compiles and runs fine.But in down casting when explicit casting is done Compiles fine and then fails at runtime.So does this mean that java doesnt support Down casting please clarify.

Thanks
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well if you make an inappropriate cast, it will fail at runtime.

Consider this example.



This code compiles because to the compiler the casts are appropriate.

But during runtime the actual objects are created and the interpreter tries to perform the casts.

This first cast works fine because the actual runtime type of the object is Integer, so you can downcast the Object to a Number.

But you can't downcast the Object to a String.
[ April 30, 2007: Message edited by: Keith Lynn ]
 
Chandra shekar M
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


public class Test41 {
public static void main(String[] args) {
Object o = 1; //1
Number n = (Number)o; //2
String s = (String)o; } }

So in the above code line one that is "o" is actually pointing to object Integer 1 at runtime and in line two to satisfie compiler we casted it to Number but at runtime as sed above it is actually Integer Object that got created "so at runtime in line two we will be casting a Integer to Number which is actully a Upcast".Is my understanding correct.

Thanks
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it will be a downcast because the Integer was upcast to an Object.
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi keith,
Can you explain in detail?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object o = 1; //1
Number n = (Number)o; //2

line 1 is an upcast
line 2 is a downcast.

upcast or downcast depends on the position in hierarchy.

every object in Jave entends from Object so line 1 is an upcast From Integer to Object.

am I correct?
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In line 4, 1 is autoboxed into an Integer object, but then a reference of type Object is assigned to refer to it. This is an implicit upcast.

So now o is a referece to the object.

Since the reference type of o is Object, and Number and String are both subclasses of Object, the compiler allows both casts.

However, at run-time, the actual object is built.

The run-time type of the object is Integer.

Number is a superclass of Integer.

So the reference o can be downcast to a Number reference and still legitimately refer to the Integer object.

However, String is not a superclass of Integer.

So the cast to String fails.
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi keith i have one doubt. Is casting based on reference type?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Downcasting is the place where compiler has to rely on the programmer.

See the self explanatory code below:

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

I think line # 1 would not compile. But I got what was meant, I think it was meant


Now Object is a superclass for Number and String. So a reference of type object "o" can point to a sub class object "Integer" here. Now at runtime, "o" will be pointing to an "Integer" object, hence it could be assigned to any reference of type "Integer" or it super classes "Number" here. But it cannot be assigned to "String" type reference as "String" is nowhere in the inheritance hierarchy of "Integer"
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line #1 compiles because of autoboxing if you are using a compiler >= 1.5.
 
Chandra shekar M
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Keith Lynn. I got it

Thanks
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic