Java is a strongly-typed programming language, which means that variables must be declared with a specific data type. However, there are situations where you may need to convert one data type to another. This process is known as type conversion or type casting. In this article, we will explore the different types of type conversion in Java and provide examples and explanations to help beginners understand the concepts.
In Java, implicit type conversion occurs when a smaller data type is automatically converted to a larger data type. This conversion is also known as widening conversion and does not require any explicit casting.
Example:
int num = 10;
double result = num;
System.out.println(result);
Output
10.0
Explanation: In the above example, the integer variable num is implicitly converted to a double data type because double can represent a larger range of values than int. The value of num (10) is automatically converted to a double value (10.0) and assigned to the variable result. The output will be 10.0.
Explicit type conversion, also known as narrowing conversion, occurs when a larger data type is explicitly converted to a smaller data type. This conversion requires the use of casting and may result in data loss or truncation.
Example:
double num = 10.5;
int result = (int) num;
System.out.println(result);
Output
10
Explanation: In the above example, the double variable num is explicitly converted to an integer data type using (int) casting. The fractional part of the double value is truncated, and the integer value (10) is assigned to the variable result. The output will be 10.
Java provides methods to convert strings to their corresponding primitive data types. These methods are part of the wrapper classes for primitive types.
Example:
String number = "25";
int result = Integer.parseInt(number);
System.out.println(result);
Output
25
Explanation: In the above example, the string variable number contains the value "25". We use the Integer.parseInt() method to convert this string to an integer value. The converted integer value (25) is assigned to the variable result, and the output will be 25.
Java also provides methods to convert primitive data types to their string representation.
Example:
int number = 50;
String result = Integer.toString(number);
System.out.println(result);
Output
50
Explanation: In the above example, the integer variable number contains the value 50. We use the Integer.toString() method to convert this integer value to its string representation. The converted string value ("50") is assigned to the variable result, and the output will be 50.
Problem 1: Convert a float value to an integer and display the result.
float num = 15.8f;
int result = (int) num;
System.out.println(result);
Output
15
Problem 2: Convert a string "true" to a boolean value and display the result.
String value = "true";
boolean result = Boolean.parseBoolean(value);
System.out.println(result);
Output
true
Type conversion or type casting is an essential concept in Java programming. Implicit type conversion allows for automatic conversion between compatible types, while explicit type conversion requires casting and may involve data loss. Additionally, Java provides methods to convert strings to primitive types and vice versa. By understanding and using these concepts effectively, you can manipulate and transform data within your programs.
60 videos|37 docs|12 tests
|
|
Explore Courses for Software Development exam
|