Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, sometimes you may need to convert between different data types, such as converting an int
to a double
, a double
to a String
, or a String
to an int
. There are two types of conversion: implicit and explicit. In this tutorial, we'll cover both types of conversion and provide examples for common use cases.
Implicit conversion, also known as "widening" or "upcasting," occurs automatically when you assign a value of a smaller data type to a variable of a larger data type. For example, assigning an int
to a double
. No data loss occurs during implicit conversion.
Example:
public class Main { public static void main(String[] args) { int intValue = 42; double doubleValue = intValue; System.out.println("int: " + intValue); // Output: 42 System.out.println("double: " + doubleValue); // Output: 42.0 } }
In the example above, the int
value 42
was implicitly converted to a double
value 42.0
.
Explicit conversion, also known as "narrowing" or "downcasting," requires the programmer to explicitly cast the value to the desired data type. This is required when you assign a value of a larger data type to a variable of a smaller data type, such as assigning a double
to an int
. Explicit conversion may result in data loss.
Example:
public class Main { public static void main(String[] args) { double doubleValue = 42.5; int intValue = (int) doubleValue; System.out.println("double: " + doubleValue); // Output: 42.5 System.out.println("int: " + intValue); // Output: 42 (fractional part is lost) } }
In the example above, the double
value 42.5
was explicitly cast to an int
value 42
. The fractional part was lost during the conversion.
String
and numeric typesa. Converting String
to numeric types:
To convert a String
to a numeric type, use the parseInt()
, parseDouble()
, etc. methods from the corresponding wrapper classes (Integer
, Double
, etc.).
Example:
public class Main { public static void main(String[] args) { String intStr = "42"; String doubleStr = "42.5"; int intValue = Integer.parseInt(intStr); double doubleValue = Double.parseDouble(doubleStr); System.out.println("int: " + intValue); // Output: 42 System.out.println("double: " + doubleValue); // Output: 42.5 } }
b. Converting numeric types to String
:
To convert a numeric type to a String
, use the toString()
method from the corresponding wrapper classes or the String.valueOf()
method.
Example:
public class Main { public static void main(String[] args) { int intValue = 42; double doubleValue = 42.5; String intStr = Integer.toString(intValue); String doubleStr = String.valueOf(doubleValue); System.out.println("String (int): " + intStr); // Output: "42" System.out.println("String (double): " + doubleStr); // Output: "42.5" } }
In summary, Java supports both implicit and explicit data type conversions.
Explicit type casting in Java
Explicit type casting is the manual conversion of a data type to another. It is required when there is a possibility of data loss.
double doubleValue = 3.14; int intValue = (int) doubleValue;
Automatic type conversion in Java
Automatic type conversion, also known as type promotion, occurs when smaller data types are automatically converted to larger ones.
int intValue = 42; double doubleValue = intValue; // Automatic type conversion
Casting between primitive data types in Java
Casting between primitive data types is done explicitly, and it may result in data loss if the target type cannot represent the entire range of the source type.
int intValue = 42; char charValue = (char) intValue;
Java widening and narrowing type conversion
Widening conversion (automatic) occurs when a smaller data type is converted to a larger one, and narrowing conversion (explicit) is the opposite.
int intValue = 42; double doubleValue = intValue; // Widening (automatic) byte byteValue = (byte) intValue; // Narrowing (explicit)
Implicit type casting in Java
Implicit type casting, or widening conversion, is the automatic conversion of smaller data types to larger ones.
int intValue = 42; double doubleValue = intValue; // Implicit type casting
Type promotion in Java expressions
Type promotion occurs when operands of different data types are involved in an expression. The smaller type is promoted to the larger type before the operation.
int intValue = 42; double doubleValue = 3.14; double result = intValue + doubleValue; // Type promotion
Type casting objects in Java
Type casting objects is done when working with classes and interfaces. It involves explicitly specifying the target type.
Object obj = "Hello, Java!"; String str = (String) obj; // Type casting object
Handling type casting exceptions in Java
When casting objects, a ClassCastException
may occur if the object is not of the expected type. To handle this, use instanceof
to check before casting.
Object obj = "Hello, Java!"; if (obj instanceof String) { String str = (String) obj; // Handle the string } else { // Handle the case where obj is not a String }