Home  >  Article  >  Java  >  Conversion in Java

Conversion in Java

WBOY
WBOYOriginal
2024-08-30 15:12:13838browse

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 Tests

Types of  Conversion in java

Depending on which data type to which data type, a variable has been converted, we can categories it into two:

1. Implicit Conversion

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:

Conversion in Java

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

  • If, in case, any of the data types is non-compatible with the other one, the conversion won’t happen. If the source data type is of the Numeric category, the destination must be of the same category as well.
  • Numeric data types are quite compatible with each other and are easy to convert. But these same numeric data types cannot be easily converted to a char type or a boolean one.
  • Byte, Short, Int, Long, Float, and Double are the numeric data types.

b. The destination data type must be larger than the source data type

  • This simply means the data type to which we are attempting to convert must be of larger bit size.
  • For example, we assigned an int value to a long variable. Here, bits size of int is 32 bits; on the contrary, bits size for long is 64 bits. Meaning, the 64 bits long is of larger value than 32 bits int.

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.

2. Explicit Conversion

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:

Conversion in Java

Below is the list of Possible Conversions in Java:

  • String to int (using Integer.parseInt() which returns primitive int)
  • String to long (using Long.parseLong() which returns primitive long)
  • String to float (using Float.parseFloat(), it returns primitive float)
  • String to Boolean (using Boolean.parseBoolean(), it returns primitive boolean, either TRUE or FALSE)

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.

  • String to Date (using parse(), it converts a value of String into Date object)
  • Date to String (using format(), simply converts a Date into String)

The above listed are the possible conversion types and the methods required, and the output it returns.

Conclusion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Java BooleansNext article:Java Booleans