Home
Map
Cast ExamplesCast variables, including objects, numbers and arrays, to compatible types.
Java
This page was last reviewed on Jan 20, 2024.
Casts. In Java 21, it is possible to cast objects to different types. A type is often considered part of the data: it is metadata, information about itself.
Casting and converting in Java is a practical operation. We use implicit (hidden) and explicit (specified) casts. Complex conversions too are needed.
instanceof
Example. This program casts a String variable to its ancestor class Object (all classes derive from Object). It then casts the object back to a String.
Step 1 An implicit cast uses no special expression syntax, and it converts one type to another.
Step 2 An explicit cast is specified in the syntax. The Object is explicitly cast to a String in the third statement.
Step 3 We can perform a cast within an instanceof operator expression. This code will not throw an exception if the cast is invalid.
public class Program { public static void main(String[] args) { String value = "cat"; // Step 1: implicitly cast to Object. Object temp = value; // Step 2: cast to String. String value2 = (String) temp; System.out.println(value2); // Step 3: test whether variable is an instance of a class. if (temp instanceof String s) { System.out.println("String s = " + s); } } }
cat String s = cat
Numeric. Numbers can be cast with the same syntax as classes. For casting to a larger byte size number (a "widening" cast) no cast is required.
But For "narrowing" casts, where a larger byte size number is reduced to fit in a smaller type, a cast is required.
Info This may occur in a narrowing conversion. Be sure the value can be represented in the new data size.
public class Program { public static void main(String[] args) { int size = 5; double size2 = size; // No cast needed. byte size3 = (byte) size; // A larger type must be cast down. System.out.println(size); System.out.println(size2); System.out.println(size3); } }
5 5.0 5
Invalid cast, ClassCastException. Some casts will fail. And often the compiler will detect an invalid cast at compile-time, saving us the trouble of running an invalid program.
Here The Object cannot be cast to String, because it is of type StringBuilder. A ClassCastException occurs.
public class Program { public static void main(String[] args) { StringBuilder builder = new StringBuilder(); builder.append("cat"); Object temp = builder; // This is fine. String value = (String) temp; // This causes an error. System.out.println(value); } }
Exception in thread "main" java.lang.ClassCastException: java.lang.StringBuilder cannot be cast to java.lang.String at program.Program.main(Program.java:10)
Array covariance. This concept means an array of a derived class can be cast to an ancestor class array. So an array of Strings can be cast to an array of Objects.
Object Array
Tip Array covariance is most often useful when we want to pass an array of any type as an Object array to a commonly-used method.
And In most programs, array covariance is not important. Using exact types is clearer and faster.
public class Program { public static void main(String[] args) { String[] values = { "cat", "dog", "rabbit" }; // A String array is also an Object array. Object[] array = values; // Loop over objects and print String lengths. for (Object v : array) { System.out.println(((String) v).length()); } } }
3 3 6
Number to String. A number cannot be cast to a String—instead we must convert it with a method like Integer.toString. This method returns a String version of the int argument.
public class Program { public static void main(String[] args) { int number = 100; String result = Integer.toString(number); // Number to String. System.out.println(result); } }
100
String to number. We convert a String to an integer by parsing it. The Integer.parseInt method can do this. It receives a String argument and returns an int.
public class Program { public static void main(String[] args) { String value = "100"; int result = Integer.parseInt(value); // String to number. System.out.println(result); } }
100
Long to int. Suppose we have a long value and wish to have an int. We can cast the long to an int directly with a cast expression. This works if the int can store the long's value.
However If the long is out of range and cannot be represented by an int, we will have an incorrect value in the int.
public class Program { public static void main(String[] args) { // Cast long to int directly. long test = 200; int result = (int) test; System.out.println("INT: " + result); } }
INT: 200
Long to int, exact. With Math.toIntExact, we can safely convert a long to an int. This may cause an exception if the value cannot be stored in the int, so we must have a try and catch.
import java.lang.Math; public class Program { public static void main(String[] args) { try { // Use Math.toIntExact to convert long to int. long test = 200; int result = Math.toIntExact(test); System.out.println("INT: " + result); } catch (ArithmeticException ex) { System.out.println("ERROR: " + ex); } } }
INT: 200
Double to int. To truncate a double, we can cast it to an int. This eliminates all digits past the decimal place. We can also truncate with Math.floor and Math.ceil.
double Truncate
public class Program { public static void main(String[] args) { double value = 100.567; int result = (int) value; // Write results. System.out.println("BEFORE: " + value); System.out.println("AFTER: " + result); } }
BEFORE: 100.567 AFTER: 100
Boolean to int. Sometimes we want to convert a boolean into an int. Typically we want true to equal 1, and false to be 0. This is done with a simple method and a ternary expression.
Convert boolean, int
Float. We can cast doubles to floats, but we must specify this with a cast expression. Some conversions to float, like from int, can be done without any special syntax.
float
Casting is complex and is often language-specific. Conversions require more steps—they are often reused in many projects and stored in utility classes.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jan 20, 2024 (new example).
Home
Changes
© 2007-2024 Sam Allen.