Tag Archives: casting

Assign integer value to byte and short variable

Integer literals and constants can be assigned to byte and short variables without casting, providing the values are within range. Values that are out of range require explicit casting, but this can produce unwanted results due to the way signed … Continue reading

Posted in Uncategorized | Tagged , | Leave a comment

Upcasting and downcasting Java reference variables

Reference variables may be cast between different types. Casting from a subtype to a supertype is referred to as upcasting and is an inherently safe operation. Casting from a supertype to a subtype is referred to as downcasting and is … Continue reading

Posted in Uncategorized | Tagged , , , , | 1 Comment

Working with arrays of objects

Consider the following UML class diagram which shows the relationship between a superclass (Vehicle) and two subclasses (Motorbike and Car). Because Motorbike is a Vehicle and Car is a Vehicle, we can create an array of vehicles like this: The … Continue reading

Posted in Uncategorized | Tagged , , , , | Leave a comment