• 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

what do the reference type casting really does?

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

Actually ,i've quite a few confusion while reference type casting.
first:

is there a place here where i get clean idea about casting?


secondly:

what happens the to the original object after it's casted?


Third:

what is the difference between casting a reference variable and casting the real object pointed to?


fourth:


what does the compiler check while casting?
what is checked at the runtime?



could someone help me with this?

Thankyou.
 
Marshal
Posts: 76412
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

s sivaraman wrote:. . .what happens the to the original object after it's casted?

Nothing. It remains exactly the same. Nothing changes. If you try to cast it to a type not compatible with its runtime type, you will suffer an exception.

what is the difference between casting a reference variable and casting the real object pointed to?

That is a meaningless question. All you can do is to tell the runtime that the reference will point to a particular type of object.

what does the compiler check while casting? . . .

The compiler does not do any casting. Any casting is done at runtime. The compiler can tell whether there is a possibility of the object being that type at runtime; if yes it can allow the code to compile otherwise not. Code like the following should fail to compile:-The following should compile because there is a chance that a List is an ArrayList. But if the runtime type is different it will throw an Exception:-If the List happens to be a LinkedList you will suffer an exception.

Hope that helps.

And welcome to the Ranch
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

s sivaraman wrote:secondly:

what happens the to the original object after it's casted?


Nothing. Casting has no effect on the object.

s sivaraman wrote:Third:

what is the difference between casting a reference variable and casting the real object pointed to?


Not completely sure what you mean here... but... I am going to assume ... Think of casting as to how to treat the instance type -- now, whether that instance is created and cast, or is specified by a reference, does it really matter?

s sivaraman wrote:fourth:


what does the compiler check while casting?
what is checked at the runtime?


At compile time, the compiler only checks to see if it is theoretically possible. At runtime, it is actually checked -- and will fail if the instance isn't IS-A the type expected.

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

Campbell Ritchie wrote:

s sivaraman wrote:what is the difference between casting a reference variable and casting the real object pointed to?

That is a meaningless question. All you can do is to tell the runtime that the reference will point to a particular type of object.The compiler does not do any casting. Any casting is done at runtime.



i mean effect of casting on below line of code.
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

s sivaraman wrote:
i mean effect of casting on below line of code.



Since a subclass is always IS-A superclass, the compiler will allow the implicit cast at compile time.

At runtime, the object should be checked during the assignment, and it should pass -- and the reference should refer to the object. There should be *no* effect on the object itself.

Henry
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Casting a reference variable from one type to another type does not change the object at all. It does not do any kind of conversion. You were asking about "the original object", but there is only one object - casting does not create a new object or change the object in any way.

Casting is a way to circumvent the compiler's normal type checking. Casting means that you tell the compiler "I have a reference to an object here, and I want you to treat it as if it refers to an object of type X - don't give me an error". It defers the type check from compile-time to runtime - when you run the program, the JVM will check if the object is really of the type that you said it is, and if it's not, you'll get a ClassCastException.

That's all that a cast does.


i mean effect of casting on below line of code.


You're not doing any casting in that line of code.
 
I was her plaything! And so was this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic