File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes I've looked through the threads on casting to try to get my head wrapped around it. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I Watch "I New topic
Author

I've looked through the threads on casting to try to get my head wrapped around it.

John Quach
Ranch Hand

Joined: Sep 28, 2011
Posts: 50
When I first heard about casting, it was dealing with math primitives. I can cast a double into an int and vice versa.

I didn't know classes can be cast as well. From what I understand, you can cast an "animal" class into a "dog" class but not the other way around because an animal is not always a dog.

Am I in the ballpark here?

Thanks in advance!
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

No, it works in other direction.
John Quach
Ranch Hand

Joined: Sep 28, 2011
Posts: 50
crap! I didn't realize that other thread about casting was on the same page!

So a "Dog" will always be an "Animal" but an "Animal" may not always be a dog....makes sense.


I wonder, how come it works both ways with int and double but not with classes?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

John Quach wrote:
I wonder, how come it works both ways with int and double but not with classes?

It might "work" but do you get the same value?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
John Quach
Ranch Hand

Joined: Sep 28, 2011
Posts: 50
Maneesh Godbole wrote:
John Quach wrote:
I wonder, how come it works both ways with int and double but not with classes?

It might "work" but do you get the same value?


...........................................oh

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

There's a difference between implicit and explicit casting. Implicit is when it just happens, explicit is when you have to add a cast yourself.

A Dog can be implicitly cast to an Animal, because a dog is always an animal:But an Animal cannot be implicitly cast to a Dog, because it might not be:
However, because an animal might be a dog, you can add the cast yourself. This is telling the compiler "trust me, it's a dog", and you'll get an error at run-time if it isn't:
Where this fails is if the cast can't possibly work. Try casting an Animal to a Daffodil, and even an explicit cast won't compile.

The general rule is:
- An implicit cast is allowed if it will always work
- An explicit cast is allowed if it will sometimes work
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
It is unfortunate that the same word “casting” is used for casting reference types and casting primitives. In the case of primitives, you can change the type (and value) of data, but you cannot change the type of a reference type.
There are more details in the Java Language Specification, but that is not usually easy to read. You find they are called narrowing conversions or similar.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

Something that I think always gets overlooked with casting objects...

you don't actually change the object. If you had a Dog object, it is always and forever will be a Dog object.

Casting is telling the compiler that the object referred to is something other than the reference type. For example:


Animal a = new Dog();

This works, because a Dog IS-A Animal. But now, suppose I do this:

Dog d = a;

The compiler will probably complain. It doesn't know that a actually refers to a Dog object, it just knows it refers to some kind of Animal. So you do this:

Dog d = (Dog) a;

Here, you are saying "I know for a fact that the thing a is referring to really IS a dog, so this is OK". The compiler believes you, since it is possible that a can point to a dog. You may get a run-time error if a is pointing to a Chicken, but the compiler can't know that.

regardless, the only object ever created here is a Dog. Casting it or assigning it to a different reference variable doesn't change that.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
John Quach
Ranch Hand

Joined: Sep 28, 2011
Posts: 50
I understand why Integers are explicitly cast as Doubles.

But why do we need to cast objects as different types?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

One reason:

Without the cast, you can't make the animal bark, because bark() is defined for just dogs, not all animals.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
John Quach
Ranch Hand

Joined: Sep 28, 2011
Posts: 50
Bear Bibeault wrote:One reason:

Without the cast, you can't make the animal bark, because bark() is defined for just dogs, not all animals.


Is it the same situation as a highSchoolStudent not being able to earnCollegeCredits()?

Or a robot that has taken human qualities and sometimes has to be cast as human?

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

Using the analogy from Head First Java, I would say it is more akin to having a fancy TV that does tons of stuff, and a simple remote control vs. one with scores of buttons.

A basic remote can turn on/off any television. If the remote doesn't have a "picture in picture' button, it doesn't matter if the TV can show it or not.

The reference variable is the remote, and the Object is the TV.

if you have an Animal reference (a basic remote), there is simply no button to push to call the bark() method, regardless of what the object can do.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I've looked through the threads on casting to try to get my head wrapped around it.
 
Similar Threads
Generics Help please !
Compile time exceptions vs. Runtime exceptions
Problems with Lists
Does casting apply to references or to objects? (was Doubt???)