Lesson 5 | Casting |
Objective | Describe how casting between numeric types and between object references occurs. |
Casting Numeric Types (between Object References)
Converting and Casting in Java
Mastery of converting and casting is very important to do well on the certification exam.
Several test questions assume that you know when and how numeric promotion takes place, when primitive types and objects are converted to
String
objects, and when casting is allowed or required.
This example illustrates that what is type casting? Type Casting refers to changing an entity of one datatype into another.
This is important for the type conversion in developing any application. If you will store a int value into a byte variable directly, this will be illegal operation.
For storing your calculated int value in a byte variable you will have to change the type of resultant data which has to be stored.This type of operation has illustrated below :
In this example we will see that how to convert the data type by using type casting. In the given line of the code
c = (char)(t?1:0);
illustrates that if t which is boolean type variable is true then value of c which is the char type variable will be 1 but 1 is a numeric value.
So, 1 is changed into character according to the Unicode value. But in this line
c = (char)(t?'1':'0');
1 is already given as a character which will be stored as it is in the char type variable c.
Converting and Casting in Java
Mastery of converting and casting is very important to do well on the certification exam.
Several test questions assume that you know when and how numeric promotion takes place, when primitive types and objects are converted to
String
objects, and when casting is allowed or required.
This example illustrates that what is type casting? Type Casting refers to changing an entity of one datatype into another.
This is important for the type conversion in developing any application. If you will store a int value into a byte variable directly, this will be illegal operation.
For storing your calculated int value in a byte variable you will have to change the type of resultant data which has to be stored.This type of operation has illustrated below :
In this example we will see that how to convert the data type by using type casting. In the given line of the code
c = (char)(t?1:0);
illustrates that if t which is boolean type variable is true then value of c which is the char type variable will be 1 but 1 is a numeric value.
So, 1 is changed into character according to the Unicode value. But in this line
c = (char)(t?'1':'0');
1 is already given as a character which will be stored as it is in the char type variable c.
Casting
The cast operator (type)
is used to convert numeric values from one numeric primitive type to another, or to change an object reference to a compatible type.
When a numeric primitive type is cast, and the resulting value is too large to fit in the size of the destination type, the high order bytes are discarded.
The Cast1 program provides an example of numeric casting.
The Cast1 program
The Cast1 program shows how simple it is to cast one numeric type to another. The d
, f
, and l
variables are assigned double
, float
, and long
values.
These values are cast to variables of the byte
, short
, and int
types. The results displayed by the program are 101, 12, and 10001, as you would expect.
Cast1
public class Cast1 {
public static void main(String[] args) {
double d = 101.5;
float f = 12.34f;
long l = 10001;
byte b = (byte)d;
short s = (short)f;
int i = (int)l;
System.out.println(b);
System.out.println(s);
System.out.println(i);
}
}
char values are unsigned integer values, so if you try to assign a negative number to one, the code will not compile. Here is an example:
char c3 = -123;
But you can forcefully assign a negative number to a char by casting it to char, as follows:
char c3 = (char)-123;
System.out.println("c3 = " + c3);
In the previous code, note how the literal value â123 is prefixed by (char). This practice is called casting.
Casting is the forceful conversion of one data type to another data type.
You can cast only compatible data types. For example, you can cast a char to an int and vice versa. But you cannot cast an int to a boolean value or vice versa.
When you cast a bigger value to a data type that has a smaller range, you tell the compiler that
you know what you are doing, so the compiler proceeds by chopping off any extra bits that may not fit into the smaller variable.
One should use casting with caution since it may not always give you the correct converted values.
Exam Tip: The exam will test your understanding of the possible values that can be assigned to a variable of type char, including whether an assignment
will result in a compilation error. The exam will not test you on the value that is actually displayed after assigning arbitrary integer values to a char data type.
When an object reference is cast from one type to another, the following rules apply:
- Any object reference can be cast to a reference of class
Object
.
- An object reference can be cast to a reference of class C if the actual class of the object is a subclass of C.
- An object reference can be cast to a reference of interface I, if the object implements I, is a subinterface of I, or if the object is an array and I is the
Cloneable
interface.
- An object reference can be cast to a reference of an array type (with element reference type T') if the object is an array (with element reference type T) such that T can be cast into T'.
The
Cast2 program illustrates the use of casting with
String
and
Object
objects.
The following link contains Java code that shows you how to cast a parent reference variable to a Child class to access a method in the child class.
Cast Parent Reference
Bitwise Operators - Exercise
Java Casting - Quiz
Click the Quiz link below to check your understanding of casting and converting.
Java Casting - Quiz