Conversion in Java is a phenomenon where the variable can be declared as a certain data type, and it is converted into a different data type for the sake of a specific operation/ function to be executed successfully. This data type conversion is applicable for all the eight types of data: int, char, long, boolean, float, double, byte, and short. The types of conversion can be classified as implicit and explicit conversion. The Implicit conversion method is achieved either when both the data types are compatible or when the destination data type is bigger than the source data type. The explicit conversion method for strings is achieved by a number of method like ‘string to int’, ‘string to long’, ‘string to float’, ‘string to boolean’, and date conversion is achieved by ‘string to date’ and ‘date to the string’.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsDepending on which data type to which data type, a variable has been converted, we can categories it into two:
It is also known as an automatic conversion, as it does not require any explicit code for the conversion process and is as easy as assigning a variable with another data type value. A very basic example would be assigning an integer value into a long variable. Let’s demonstrate Simple Implicit Conversion with example.
Sample Code:
public class con_java { public static void main(String[] args) { int a = 22; long b = a; System.out.println("Converted Value is : " + b); } }
In the above-given example, we have simply provided an integer value to the long variable, and it works like a charm. Int and Long, both being numeric data types, works smoothly with each other.
Code Interpretation: In a simple class with main, we declared an integer “a” variable with value 22 and then long variable “b”. Here we assigned integer value of a to b, which is of long data type. With the print line, the value of long data type b is printed. Being implicit conversion, no extra code is required.
Output:
For Implicit Conversion, there are two of basic rules. Only when these properties are satisfied conversion will be smoothly executed.
a. Both the data types must be compatible
b. The destination data type must be larger than the source data type
With the satisfaction of the above-mentioned rules, a simple implicit conversion happens. Now, let’s understand Explicit Conversion.
The second Requirement for Implicit conversion is where lower bits data type can only be converted to larger bits data type, which results in no loss of data in conversion. But what if we need to covert larger bit size data type into smaller ones? Here data loss is inevitable, and the java compiler will throw an error “UserWarni: Possible precision loss when converting” or another error depending on code. Explicit Conversion is what we use when we are aware of conversion properties and the error it throws.
To implement Explicit Conversion is to override java’s default type conversion by explicitly defining our custom interim data type as per the requirements. When we explicitly provide a type conversion, the value’s data type is changed to the desired data type for the short term. Explicit Conversion is also known as Narrowing a type. The syntax for Type Conversion is:
Vaiable2 = (type) Variable1;
Here, variable2 is the destination variable of the different data types to which the Variable1 must be converted. (type) is the specification of the data type to which Variable1 is converted into and assigned to Variable2.
Explicit Conversion can be of immense use, where a small part of the number is kept on hold while the calculation is executed. Application for explicit conversion can be a simple calculator, where the percentage of the student has to be calculated. To demonstrate the working of Explicit Conversion, let’s try an example.
Sample Code:
public class exp_con_java { public static void main(String[] args) { double dou_Variable = 120.14; long long_Variable = (long) dou_Variable; int intVariable = (int)long_Variable; System.out.println("The Double value is "+dou_Variable); System.out.println("The Long value is "+long_Variable); System.out.println("The Integer value is "+intVariable); } }
Output:
Below is the list of Possible Conversions in Java:
SimpleDateFormat(): is a Java class that helps in formatting and parsing of the data. It simply allows us to convert a simple String into a Date object.
The above listed are the possible conversion types and the methods required, and the output it returns.
Type conversion in Java or any other language is a better way of utilizing its functions and getting the desired output. We have understood two types of conversion based on properties and data types. Implicit conversion does not need any extra effort but must follow two properties. And Explicit conversion must be explicitly defined in order to override Java’s default conversion. We have understood both types with program examples.
The above is the detailed content of Conversion in Java. For more information, please follow other related articles on the PHP Chinese website!