Types of Conversions in Java

  • By Chetna Malagi
  • August 5, 2023
  • JAVA Programming
Types of Conversions in Java

Types of Conversions in Java

In Java, conversion is a typical process that involves changing one data type into another. It is typically used for ensuring the compatibility of data types in various operations. There are two main Types of conversions in Java. implicit and explicit conversion. Join SevenMentor’s comprehensive Java training in Pune and gain expertise in this powerful programming language.

  • Implicit Conversion (also known as automatic type conversion or widening conversion)

Implicit conversion occurs automatically by the Java compiler when you assign a smaller data type to a larger data type. For example, you can set an integer value to a double variable without any issues, because the double data type is larger than the integer type.

Here’s an example:

Types of Conversions in Java

Implicit conversion is safe because there’s no risk of losing information. In the above example, the integer ‘i’ is converted to a double ‘d’, and no data is lost.

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

  • Explicit Conversion (also known as casting or narrowing conversion).

Explicit conversion, or casting, is necessary when you want to assign a larger data type to a smaller one. This could potentially lead to data loss, because the smaller data type may not be capable of storing all the information present in the larger one.

Here’s an example:

Types of Conversions in Java

In this case, the decimal part of the double ‘d’ is lost when it is converted to an integer ‘i’. Hence, casting needs to be done carefully.

Conversions are essential in Java and other programming languages for a variety of reasons, including:

  • Data compatibility: Conversions allow different data types to be used together in operations. Without conversions, you couldn’t operate on an integer and a double.

 

  • Data manipulation: Certain operations may require data to be in a certain form. For instance, you might need to convert an integer to a string to use string manipulation functions on it.

 

  • Resource optimization: Sometimes, to save memory or optimize performance, it’s better to convert data to a smaller type when you’re sure that the data would not exceed the capacity of the smaller type.

Here we will see some more examples of explicit conversion.

  • Here we are converting primitive data type to String type

In this example, we are converting the Integer type of data into a String type.

Types of Conversions in Java

Here the 200 is in number format which will be converted into String “200”

When you use the ‘+’ value to int-type data it will add the value but the same when you use it with the string it will join both the value

200+100 in Intereger    →300

“200”+100 in String     →200100

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

  • Here we are converting primitive data type to String type

In this example, we are converting the Long type of data into a String type.

Types of Conversions in Java

Here the 200 is in number format which will be converted into String “200”

When you use the ‘+’ value to long-type data it will add the value but the same when you use it with the string it will join both the value

300+500 in Long    →800

“300”+500 in String     →300500

In the above example here we can convert any primitive data to a string by using following any function.

  1. valueOf()–>from String class
  2. toString()–>is available in all Wrapper class

 

  • Here we are converting the String type to any Primitive data type.

In this example, we are converting a String type to an Integer type.

Types of Conversions in Java

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

Here the 200 is in number format which in String “200” you can convert to any primitive data type by using the parse method 

Note: A string type of data that is in number format only that type of data can be converted into primitive type if its string format and try to convert to primitive then we get NumberFormatException.

Do visit our channel to learn more: Click Here

In the above example here we can convert a string into any primitive by using the following function.

  1. Convert to Integer—>Integer.parseInt(String str)
  2. Convert to Long—>Long.parseLong(String str)
  3. Convert to Float—>Float.parseFloat(String str)
  4. Convert to Double—>Double.parseDouble(String str)

Author:-

Chetna Malagi

Call the Trainer and Book your free demo Class For Java Call now!!!
| SevenMentor Pvt Ltd.

© Copyright 2021 | SevenMentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*