What is Type Conversion in Java? [With Examples]

If you are beginning with your Java programming journey, one of the primary concepts to get a hold of is type casting or type conversion. Whether you are looking to master Java programming or want to start a career in software development, the concepts of typecasting will always be useful for you. 

Type conversion is simply the process of converting data of one data type into another. This process is known as type conversion, typecasting, or even type coercion. The Java programming language allows programmers to convert both primitive as well as reference data types. By performing type conversion, the value of the data remains the same, just that its type has changed. One thing to note is that it is not possible to perform typecasting or type conversion on Boolean data type variables. 

Java offers various types of type conversions for programmers. Let’s look at some of the more useful types conversions! 

Learn software development courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Primitive Type Conversion in Java

Primitive data types are simply the data types defined by the programming language for the developers to use. Java offers seven different primitive data types such as: 

  • Boolean
  • Byte
  • Char
  • Short
  • Int
  • Long
  • Float
  • Double

By default, whenever you assign values to any variable in Java programming, you perform primitive type conversion since you assign the value of one type to another type. 

Implicit Type Conversion in Java

Other than primitive type casting done by default, there are two more type conversions in Java – implicit and explicit. 

Implicit type conversion, also known as widening casting, is all about casting a data type with lower values to a data type with higher values without incurring any data loss. Here is an example of implicit type conversion in Java:

public class Conv{

public static void main(String[] args)

{

int i = 1500;

//automatic type conversion

long l = i;

//automatic type conversion

float f = l;

System.out.println(“Int“+i);

System.out.println(“Long“+l);

System.out.println(“Float“+f);

}

}

Output:

Int value 15000

Long value 15000

Float value 15000.0

In all the three statements above, implicit type casting occurs, and the value of a ‘long’ data type is cast as int and float. 

Implicit type conversion in Java takes place when primarily these two conditions are met: 

  • The two data types should be compatible with one another. For instance, numeric data types are compatible with other numeric data types but not with string or boolean types. Likewise, the string data type is not compatible with a boolean data type. 
  • The targeted value that needs to be type converted should have a smaller length. For example, 4 bytes of data can be cast to an 8-byte data type.

The order of data types in which implicit type conversion in Java happens is as follows: 

Byte > Short > Char > Int > Long > Float > Double 

Explicit Type Conversion in Java

Explicit type conversion is the opposite of implicit type conversion. So, instead of widening, this type conversion works towards narrowing. The developer assigns the value of a higher data type to a relatively lower one. Since we are casting data from a broader data type to a narrower data type, there are chances of data loss, so careful executions need to be performed while carrying out explicit type conversion in Java. 

While implicit type conversion in Java can be done automatically by the compiler, the developer does explicit type conversion. Here is an example of explicit type casting in Java:

public class Narrow

{

public static void main(String[] args)

{

double d = 1500.06;

//explicit type conversion

long l = (long)d;

//explicit type conversion

int i = (int)l;

System.out.println(“Double value “+d);

//fractional part lost

System.out.println(“Long value “+l);

//fractional part lost

System.out.println(“Int value “+i);

}

}

Output:

Double Data type value 1500.06

Long Data type value 1500

Int Data type value 1500

Explicit type conversion in Java works in the following order of conversion: 

Double > Float > Long > Int > Char > Short > Byte

In Conclusion

That is pretty much all the basics you need to know when it comes to type conversion in Java. From here on, the more you practice, the better grasp you’ll have over these concepts. Keep in mind that data structures (and algorithms, even) is one of the more essential aspects of Computer Science, and therefore it is equally vital for software development as well. Whatever be the tech stack you work on, you will be working with data and data structures. If you are working on a complex project, you will most likely be dealing with multiple data types and data structures to get to your destination. In all such cases, and in general, too, the concepts of type conversion will help you stay afloat without any difficulty. 

So, you should start practising and playing with different data types, try to implement explicit type conversions in Java, encounter some errors and grapple your way out of it! That is the only way of mastering a programming language. At upGrad, we believe in the value of practice and personal guidance, which reflects in our courses and teaching pedagogy. If you’re an aspiring software developer looking to work in full-stack development, our Executive PG Programme in Software Development – Specialisation in Full Stack Development is designed just for you. It is built to help learners start from scratch, understand the most complex concepts, and master them. Check out our curriculum and register soon!

Does type conversion in Java always have to be performed by the programmer?

Explicit type conversion needs to be done by the programmer. However, implicit type conversion is done by the compiler itself.

When is type conversion required in Java?

Programmers and software developers need to regularly check the compatibility of the different data types they are working with. In all those scenarios, type conversion helps programmers mould the data types as per the requirement of their program.

Which type conversion is automatically performed in Java?

Implicit type conversion in Java is automatically performed.

Be a Master in Software Development

0 replies on “What is Type Conversion in Java? [With Examples]”

×
Let’s do it!
No, thanks.