Back to Primitive Types

Lesson PreviewLesson 5 of 7 in this unit

1.5 Casting and Ranges of Variables

Part of Primitive Types in AP® Computer Science A. Read the opening section, then create a free account for the full interactive lesson, practice, and progress tracking.

Casting and Ranges of Variables

In programming, casting is the way we convert one data type to another. To cast something from one data type to another, put the data type you want to cast to in parentheses. For instance, you might want to turn a double into an int, or convert an int into a double. Below is an example of casting a byte to a double.

java
byte number = 5;
System.out.println((double) number); // Remember, whenever you are printing a double that is a whole number, the computer will add ".0" to the end!

Output

5.0

Continue the full lesson in Stellar

Create a free account to unlock the remaining sections, checkpoints, course practice, and progress tracking for AP® Computer Science A.