• Post Reply Bookmark Topic Watch Topic
  • New Topic

what do the reference type casting really does?  RSS feed

 
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!