Java Type Conversions
Java automatically converts data types in expressions to maintain compatibility. This process, known as Type Conversion, can be implicit (automatic) or explicit (manual type casting). Let's explore different types of conversions in Java.
Checking the Data Type:
Java supports primitive and non-primitive data types. To check the data type of a variable, we use:
Non-Primitives :
String username = "Ravi";
System.out.println(username.getClass().getSimpleName());
Primitives :
int age = 10;
System.out.println(((Object) age).getClass().getSimpleName());
Output: Integer
float value = 10.0f;
System.out.println(((Object) value).getClass().getSimpleName());
Output: Float
char letter = 'A';
System.out.println(((Object) letter).getClass().getSimpleName());
Output: Character
Type Conversion in Java :
Java provides two types of type conversion:
Implicit Type Conversion :
Java automatically converts a lower data type to a higher data type in an expression.
Hierarchy of Data Types :
char → byte → short → int → long → float → double → String
Example: int to long Conversion :
int value1 = 4;
long value2 = 5L;
System.out.println(((Object) (value1 + value2)).getClass().getSimpleName());
Output: Long
int value1 = 4;
long value2 = 5L;
long sum = value1 + value2;
System.out.println(((Object) sum).getClass().getSimpleName());
Output: Long
Explicit Type Conversion (Type Casting) :
Programmers manually change a data type using type casting.
Example: long to float Conversion:
long x = 10L;
float y = (float) x;
System.out.println(y);
Output: 10.0
Type Conversion Using Methods :
Java provides built-in methods to convert between data types:
1. Convert Any Primitive to String :
int num = 2;
float floatNum = 2.34f;
char ch = 'a';
String str1 = String.valueOf(num);
String str2 = String.valueOf(floatNum);
String str3 = String.valueOf(ch);
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
// Output: 2, 2.34, a
2. Convert String to int:
String str = "21";
int num = Integer.parseInt(str);
System.out.println(num);
Output: 21
3. Convert char to int:
char ch = '3';
int num = Character.getNumericValue(ch);
System.out.println(num);
Output: 3
4. Convert int to char:
int num = 3;
char ch = Character.forDigit(num, 10);
System.out.println(ch);
Output: 3
5. Convert String to double:
String str = "1.666";
double doubleNum = Double.parseDouble(str);
System.out.println(doubleNum);
System.out.println(((Object) doubleNum).getClass().getSimpleName());
Output: 1.666, Double
6. Convert String to float:
String str = "1.666";
float floatNum = Float.parseFloat(str);
System.out.println(floatNum);
System.out.println(((Object) floatNum).getClass().getSimpleName());
Output: 1.666, Float
Summary
-
Use
getClass().getSimpleName()
to check data types.
getClass().getSimpleName()
to check data types.- Primitives must be cast to objects before checking their type.
- Implicit Conversion: Java automatically converts smaller data types to larger ones.
- Explicit Conversion: Requires manual type casting using
(dataType) value
.
- Conversion Methods:
String.valueOf(value)
: Convert any primitive to String.
Integer.parseInt(str)
: Convert String to int.
Character.getNumericValue(ch)
: Convert char to int.
Character.forDigit(num, base)
: Convert int to char.
Double.parseDouble(str)
: Convert String to double.
Float.parseFloat(str)
: Convert String to float.