Type casting in java
Casting is a process of changing one type value to another type. In java we can cast one type of value to another type value using through type casting. It is known as type casting.
Basically two type of casting in java.
1 Widening Casting
2 Narrowing Casting
• Widening Casting (Implicit)
In Widening Type Casting, Java automatically converts one data type to another data type.
Example: Converting int to double
class Test { public static void main(String[] args) { // create int type variable int number = 15; System.out.println("Integer value: " + num); // convert into double type double data = number; System.out.println("double value: " + data); } }
Output
Integer value: 10
double value: 10.0
In the above example, we are assigning the int type variable number to a double type variable data.
Here, the Java first converts the int type data into the double type. And then assign it to the double variable.
In the case of Widening Type Casting, the lower data type (having smaller size) is converted into the higher data type (having larger size). Hence there is no loss in data. This is why this type of conversion happens automatically.
Note: This is also known as Implicit Type Casting.
• Narrowing Casting (Explicitly done)
Narrowing casting java
In Narrowing Type Casting, we manually convert one data type into another using the parenthesis.
Example: Converting double into an int
class Main {
public static void main(String[] args) { // create double type variable double num = 10.99; System.out.println("The double value: " + num); // convert into int type int data = (int)num; System.out.println("The integer value: " + data); } }
Output
The double value: 10.99
The integer value: 10
In the above example, we are assigning the double type variable named num to an int type variable named data.
Notice the line,
int data = (int)num;
Here, the int keyword inside the parenthesis indicates that that the num variable is converted into the int type.
In the case of Narrowing Type Casting, the higher data types (having larger size) are converted into lower data types (having smaller size). Hence there is the loss of data. This is why this type of conversion does not happen automatically.
Note: This is also known as Explicit Type Casting.
Let's see some of the examples of other type conversions in Java.
Example 1: Type conversion from int to String
class Main { public static void main(String[] args) { // create int type variable int num = 10; System.out.println("The integer value is: " + num); // converts int to string type String data = String.valueOf(num); System.out.println("The string value is: " + data); } }
Output
The integer value is: 10
The string value is: 10
In the above program, notice the line
String data = String.valueOf(num);
Here, we have used the valueOf() method of the Java String class to convert the int type variable into a string.
Example 2: Type conversion from String to int
class Main { public static void main(String[] args) { // create string type variable String data = "10"; System.out.println("The string value is: " + data); // convert string variable to int int num = Integer.parseInt(data); System.out.println("The integer value is: " + num); } }
Output
The string value is: 10
The integer value is: 10
In the above example, notice the line
int num = Integer.parseInt(data);
Here, we have used the parseInt() method of the Java Integer class to convert a string type variable into an int variable.
Note: If the string variable cannot be converted into the integer variable then an exception named NumberFormatException occurs.
No comments: