Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Getting There Type Casting

Wan Nor Adzahari Wan Tajuddin
Wan Nor Adzahari Wan Tajuddin
2,438 Points

Anyone know any practice to get a better grip of typecasting?

I'm finding typecasting to be the toughest subject of my Java learning so far. I did pass the code challenge (after blowing my brains out for two days), however, I don't think I've understood typecasting deeply enough. So I wanna know, does anyone know any good way to practice typecasting? Any website that provides code challenges like this? It'll be a great help if I can get more practicing on typecasting done.

1 Answer

Hi Wan Nor Adzahari Wan Tajuddin,

I think knowing what about typecasting is causing you problems would be helpful to know where to go on this, but I'll give it a try.

The Java docs actually explain the idea of casting quite beautifully, which is pretty rare as they can be rather difficult to grok sometimes. Below, though, is a simplified explanation of the docs, using the docs' own example.

If you understand object inheritance, explicit casting is pretty straightforward. Objects are the class from which they are instantiated;

public MountainBike myBike = new MountainBike(); myBike is of type MountainBike.

MountainBike descends from Bicycle and Object, so it can be used as either a Bicycle or an Object with implicit casting;

Object obj = new MountainBike(); then obj is both an Object and a MountainBike (until such time as obj is assigned another object that is not a MountainBike).

Implicit casting works because every child object is descended from the parent object. The relationship is a one-to-many, though; all MountainBike objects are Objects, but not all Objects are MountainBikes. This is where the explicit casting comes into play.

MountainBike myBike = (MountainBike)obj; This cast inserts a runtime check that obj is assigned a MountainBike so that the compiler can safely assume that obj is a MountainBike. If obj is not a MountainBike at runtime, an exception will be thrown. If obj wasn't cast as a MountainBike, we would get a compile-time error because obj is not known to the compiler to be a MountainBike. When you cast an object, you are promising that that object can be used as the casting type.

For practice, I'd probably recommend going through some of the beginner Android courses here on Treehouse. You have to typecast Views from the generic View they are pulled in as into the specific View they actually are before you can use them, so you'll definitely get practice in that way.

Please let me know if you have any questions about any of this, I'll be happy to expand (or inflate, if you get into Android) on any of this if it would help.

Hope that helps!