• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
John Quach
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
Java Java ME Opera
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it works in other direction.
 
John Quach
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
John Quach
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Quach
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One reason:

Without the cast, you can't make the animal bark, because bark() is defined for just dogs, not all animals.
 
John Quach
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic