Type Conversion in Java | Developer.com

Java Developer Tutorials

In computer programming, type conversion refers to evaluating an expression to a given data type. This evaluation can be between primitive or reference types. In this programming tutorial, we explore the concept of type conversion in Java, alongside code examples and use cases.

Read: Top Java Online Training Courses and Bundles

What is Boxing in Java?

Boxing refers to the conversion of any one of the eight primitive data types available in Java (boolean, byte, short, char, int, long, float, and double) to the corresponding wrapper classes (Boolean, Byte, Short, Character, Integer, Long, Float, and Double). These classes allow developers to perform certain functions on data types.

A wrapper class is simply a reference type that contains the value of the given primitive type in its field. Programmers can create an object of a wrapper class by assigning the value of a given type to it.

Here is an example of how to use a wrapper class in Java:

Float floatObj = 8.5;

In earlier of versions of Java, it was possible to create a wrapper class by passing the primitive value as a constructor, as shown in the following example code:

Double doubleObj = new Double(9.65);

This approach is now deprecated, however. Developers may find this approach if you do some research online. Therefore, it is worth mentioning it so that you are aware in case your compiler throws an error.

You can learn more about constructors in our tutorial: Intro to Using Constructors in Java.

Developers can also create a numerical wrapper class from a string. The parseXXX method allows you to achieve this. XXX can be any of the following: Short, Character, Integer, Long, Float, and Double.

Long longObj = Long.parseLong("8971465");

What is Unboxing in Java?

Just as the name suggests, in Java, unboxing is the reverse of boxing. To unbox a reference type r (i.e get its value), simply use the syntax below:

r.xxxValue

The string xxx above represents any of the following: Boolean, Byte, Short, Char, Int, Long, Float, or Double.

For example, to get an integer value y from an integer wrapper class, you would use the follow example code in Java:

Integer intObj = 8;
int y = intObj.intValue; // unboxing 

How to Perform String Conversion in Java

A lot of times when programming, programmers will need to use strings. Therefore, it is important to know how to convert different types to string values.

It is worth noting that you can convert all types (including objects) into a string. First, you need to convert any primitive type (if any) to a reference type. After that, you can convert it to a string using the toString() method.

Here is an example of how to use the toString() method in Java:

Float g = 7.8;
String str1 = g.toString();

It is also possible to pass your primitive type as an argument to the toString() method in order to convert it to a string value, as shown in this code example:

int i = 5;
double d = 5.68;
    
String s3 = Integer.toString(i); 
String s4 = Double.toString(d); 

You can learn more about working with primitive data types in our tutorial: Java Primitive Data Types. After that, check out our guide: Java Non-primitive Data Types.

Java Casting

In Java, casting refers to converting one primitive type to another. There are two types of casting conversion in Java: widening and narrowing.

In Java, widening refers to converting a value to a larger type in the data type hierarchy; for instance: byte > short > char > int > long > float > double.

Narrowing, on the other hand, refers to converting a value to a lower type in the data type hierarchy: double > float > long > int > char > short > byte.

Widening conversion happens automatically. However, for narrowing, you need to explicitly define it in your statement using a cast operator (). This is a unary operator () that takes in the smaller primitive type, as shown in the code example below:

double d = 74.3;
int x = (int) d; // narrowing conversion 

Assignment Conversion in Java

Assignment conversion occurs when the value of an expression is assigned to a given variable. For example:

int x = 3;
int y = 8;
int z = x * y; // value of  x * y is assigned to z

Assignment conversion can be used with boxing, unboxing, and type casting conversions.

Final Thoughts on Type Conversion in Java

In this programming tutorial, developers learned how to convert between different types in Java. Now, it is important for you to note that assignment happens from right to left (i.e the expression you are converting should be on the right-hand side of the equal sign).

Also, remember that narrowing conversion needs to be clearly defined by you the programmer. However, widening conversion is automatically handled by the compiler.

Read more Java programming tutorials and software development guides.

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: