Unit 1: Primitive Data Types

Casting and Type Conversions in AP CS A


What is Casting?

Casting is the process of converting a value from one data type to another data type. In AP CS A, we will be casting between the following data types:

  • int - A whole number (e.g. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
  • double - A decimal number (e.g. 1.0, 2.0, 3.0, 4.0, 5.0, 6.0)
  • String - A sequence of characters (e.g. "Hello World!")
  • char - A single character (e.g. 'a', 'b', 'c ', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
  • boolean - A value that is either true of false

Implicit Casting

Implicit casting is when a value is automatically converted from one data type to another data type. For example, if we have the following code:

int x = 5;
double y = x;

Implicit Casting

The value of x is implicitly casted to a double and stored in y. This is because a double can store a whole number (e.g. 5.0). However, a whole number cannot store a decimal number (e.g. 5.5). Therefore, the vale if x is implicitly casted to a double and stored in y.

Explicit Casting

Explicit casting is when a value is manually converted from one data type to another data type. For example, if we have the following code:

double x = 5.5;
int y = (int) x;

Explicit Casting

The value of x is explicitly cast to an int and stored in y. This is because an int cannot store a decimal number (e.g. 5.5). Therefore the value of x is explicitly casted to an int and stored in y.

Converting an int to a double

To convert an int to a double, we can use implicit casting. For example, if we have the following code:

in x = 5;
double y = x;

Converting an int to a double

The value of x is implicitly casted to a double and stored in y. This is because a double can store a whole number (e.g. 5.0). However, a whole number cannot store a decimal number (e.g. 5.5). Therefore, the vale if x is implicitly casted to a double and stored in y.

Converting a double to an int

To convert a double to an int, we can use explicit casting. For example, if we have the following code:

double x = 5.5;
int y = (int) x;

Converting a double to an int

The value of x is explicitly casted to an int and stored in y. This is because an int cannot store a decimal number (e.g. 5.5). Therefore the value of x is explicitly casted to an int and stored in y.

Converting a String to an int

To convert a String to an int, we can use the Integer.parseInt() method. For example, if we have the following code:

String x = "5";
int y = Integer.parseInt(x);

Converting a String to an int

The value of x is converted to an int and stored in y. This is because a String cannot store a whole number (e.g. "5"). Therefore the value of x is converted to an int and stored in y.

Converting an int to a String

To convert an int to a String, we can use the Integer.toString() method. For example, if we have the following code:

int x = 5;
String y = Integer.toString(x);

Converting an int to a String

The value of x is converted to a String and stored in y. This is because a String cannot store a whole number (e.g. "5"). Therefore the value of x is converted to a String and stored in y.

Converting a String to a double

To convert a String to a double, we can use the Double.parseDouble() method. For example, if we have the following code:

String x = "5.5";
double y = Double.parseDouble(x);

Converting a String to a double

The value of x is converted to a double and stored in y. This is because a String cannot store a decimal number (e.g. "5.5"). Therefore the value of x is converted to a double and stored in y.

Converting a double to a String

To convert a double to a String, we can use the Double.toString() method. For example, if we have the following code:

double x = 5.5;
String y = Double.toString(x);

Converting a double to a String

The value of x is converted to a String and stored in y. This is because a String cannot store a decimal number (e.g. "5.5"). Therefore the value of x is converted to a String and stored in y.

Converting a String to a char

To convert a String to a char, we can use the String.charAt() method. For example, if we have the following code:

String x = "a";
char y = x.charAt(0);

Converting a String to a char

The value of x is converted to a char and stored in y. This is because a String cannot store a single character (e.g. "a"). Therefore the value of x is converted to a char and stored in y.

Converting a char to a String

To convert a char to a String, we can use the Character.toString() method. For example, if we have the following code:

char x = 'a';
String y = Character.toString(x);

Converting a char to a String

The value of x is converted to a String and stored in y. This is because a String cannot store a single character (e.g. "a"). Therefore the value of x is converted to a String and stored in y.

Converting a String to a boolean

To convert a String to a boolean, we can use the Boolean.parseBoolean() method. For example, if we have the following code:

String x = "true";
boolean y = Boolean.parseBoolean(x);

Converting a String to a boolean

The value of x is converted to a boolean and stored in y. This is because a String cannot store a boolean value (e.g. "true"). Therefore the value of x is converted to a boolean and stored in y.

Converting a boolean to a String

To convert a boolean to a String, we can use the Boolean.toString() method. For example, if we have the following code:

boolean x = true;
String y = Boolean.toString(x);

Converting a boolean to a String

The value of x is converted to a String and stored in y. This is because a String cannot store a boolean value (e.g. "true"). Therefore the value of x is converted to a String and stored in y.


Common Errors

Converting a String to an int

If we try to convert a String to an int and the String does not contain a whole number, we will get a NumberFormatException. For example, if we have the following code:

String x = "5.5";
int y = Integer.parseInt(x);

Converting a String to an int

The value of x is converted to an int and stored in y. This is because a String cannot store a whole number (e.g. "5.5"). Therefore the value of x is converted to an int and stored in y.

Converting a String to a double

If we try to convert a String to a double and the String does not contain a decimal number, we will get a NumberFormatException. For example, if we have the following code:

String x = "5";
double y = Double.parseDouble(x);

Converting a String to a double

The value of x is converted to a double and stored in y. This is because a String cannot store a decimal number (e.g. "5"). Therefore the value of x is converted to a double and stored in y.

Converting a String to a char

If we try to convert a String to a char and the String does not contain a single character, we will get a StringIndexOutOfBoundsException. For example, if we have the following code:

String x = "abc";
char y = x.charAt(0);

Converting a String to a char

The value of x is converted to a char and stored in y. This is because a String cannot store a single character (e.g. "abc"). Therefore the value of x is converted to a char and stored in y.

Converting a char to a String

If we try to convert a char to a String and the char does not contain a single character, we will get a StringIndexOutOfBoundsException. For example, if we have the following code:

char x = 'abc';
String y = Character.toString(x);

Converting a char to a String

The value of x is converted to a String and stored in y. This is because a String cannot store a single character (e.g. "abc"). Therefore the value of x is converted to a String and stored in y.


Summary

In this lesson, we learned about casting and type conversions in AP CS A. We learned about implicit casting, explicit casting, and type conversions in AP CS A. We learned about converting an int to a double, converting a double to an int, converting a String to an int, converting an int to a String, converting a String to a double, converting a double to a String, converting a String to a char, converting a char to a String, converting a String to a boolean, and converting a boolean to a String in AP CS A.


References


AP CSA Homework Assignment

Assignment

Instructions

Use casting and type conversions to convert the number entered by the user to a double, String, char, and boolean. Write a program that asks the user to enter a number. The program should then print out the following:

  • The number entered by the user
  • The number entered by the user as a double
  • The number entered by the user as a String
  • The number entered by the user as a char
  • The number entered by the user as a boolean

An example project:

  • Build a program that asks the user to enter a number. The program should then print out the following:
    • The number entered by the user
    • The number entered by the user as a double
    • The number entered by the user as a String
    • The number entered by the user as a char
    • The number entered by the user as a boolean
Previous
Operators