Java supports two types of castings – primitive data type casting and reference type casting. Reference type casting is nothing but assigning one Java object to another object. It comes with very strict rules and is explained clearly in Object Casting. Now let us go for data type casting.
Java data type casting comes with 3 flavors.
1. Implicit casting (widening conversion)
A data type of lower size (occupying less memory) is assigned to a data type of higher size. This is done implicitly by the JVM. The lower size is widened to higher size. This is also named as automatic type conversion.
int x = 10; // occupies 4 bytes
double y = x; // occupies 8 bytes
System.out.println(y); // prints 10.0
In the above code 4 bytes integer value is assigned to 8 bytes double value.
2. Explicit casting (narrowing conversion)
A data type of higher size (occupying more memory) cannot be assigned to a data type of lower size. This is not done implicitly by the JVM and requires explicit casting; a casting operation to be performed by the programmer. The higher size is narrowed to lower size.
double x = 10.5; // 8 bytes
int y = x; // 4 bytes ; raises compilation error
In the above code, 8 bytes double value is narrowed to 4 bytes int value. It raises error. Let us explicitly type cast it.
double x = 10.5;
int y = (int) x;
The double x is explicitly converted to int y. The thumb rule is, on both sides, the same data type should exist.
3. Boolean casting
A boolean value cannot be assigned to any other data type. Except boolean, all the remaining 7 data types can be assigned to one another either implicitly or explicitly; but boolean cannot. We say, boolean is incompatible for conversion. Maximum we can assign a boolean value to another boolean.
Following raises error.
boolean x = true;
int y = x; // error
boolean x = true;
int y = (int) x; // error
byte –> short –> int –> long –> float –> double
In the above statement, left to right can be assigned implicitly and right to left requires explicit casting. That is, byte can be assigned to short implicitly but short to byte requires explicit casting.
Following char operations are possible.
public class Demo
public static void main(String args)
char ch1 = 'A';
double d1 = ch1;
System.out.println(d1); // prints 65.0
System.out.println(ch1 * ch1); // prints 4225 , 65 * 65
double d2 = 66.0;
char ch2 = (char) d2;
System.out.println(ch2); // prints B
Object casting (reference casting) discusses casting between objects of different classes involved in inheritance.
Your one stop destination for all data type conversions
String and data type conversions
Some questions for you from java.lang package.
1. How many ways you can copy one object properties to another?
Ans: 3 ways – Shallow copying, Deep copying and Cloning.
2. How many types of inner classes exist?
Ans: 4 types.
3. What are JAR files?
Ans: JAR file is a zipped file zipped by JVM.
4. How to convert a string into string form?
Ans: String to data type conversions – byte, short, int, long, float, double, char and boolean.
5. How to convert an object to string?
Ans: Object to String – toString()
6. Do you know the functionality and methods of following java.lang package classes?
Ans: 1. Root class – class Object 2. class Character 3. class System
7. What are wrapper classes?
Ans: Wrapper classes
8. What is the garbage collection mechanism in Java?
Ans: Garbage Collection – gc() and exit(0)
9. How to use the C++ destructor functionality where Java does not support destructors?
Ans: Java Destructor – finalize()
10. How to compare two objects?
Ans: Object comparison – hashCode() & equals()