Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Type Conversion?
2.1.
Java
3.
What is Type Casting?
3.1.
Java
4.
Difference Between Type Casting and Type Conversion
5.
Frequently Asked Questions
5.1.
What is meant by type conversion and type casting in Java?
5.2.
What are the common methods used to change data types in Java?
5.3.
What is explicit type conversion?
5.4.
What is automatic type conversion?
5.5.
Differentiate between type conversion and type casting in Java based on data loss.
6.
Conclusion
Last Updated: Apr 16, 2024
Easy

Type Conversion And Type Casting In Java

Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

Introduction

A significant part of programming involves playing around with data. While performing some tasks, situations might arise where the data type has to be changed from one type to another. For example, while calculating the area of a circle using Pi, we get a decimal number, and that needs to be converted to integer if we desire to obtain the answer in integer format. 

Type Conversion And Type Casting In Java

Since Java is strongly typed, one often needs to convert from one data type to another. The programmer or the compiler can easily do this by using type casting and type conversion concepts.

In Java, there are thirteen types of type Conversions. You can explore more about them by referring to the Java Type Conversion section in the Official Oracle Java Documentation. This blog will dive deep into the concept of type conversion and type casting in Java in detail, along with some examples.

Before learning Type Conversion and Type Casting in Java, you should know about Java Data Types. So check out the blog on Java Data Types and Identifiers to get a better understanding.

What is Type Conversion?

Type conversion is a process in which the data type is automatically converted into another data type. The compiler does this automatic conversion at compile time. The data type to which the conversion happens is called the destination data type, and the data type from which the conversion happens is called the source data type. If the source and destination data types are compatible, then automatic type conversion takes place.

For type conversion to take place, the destination data type must be larger than the source type. In short, the below flow chart has to be followed.

Flow chart for Type Conversion

Flow chart for Type Conversion

This type of type conversion is also called Widening Type Conversion/ Implicit Conversion/ Casting Down.  In this case, as the lower data types with smaller sizes are converted into higher ones with a larger size, there is no chance of data loss. This makes it safe for usage.

Type conversion

In type conversion, the source data type with a smaller size is converted into the destination data type with a larger size.

Before looking at an example for the type conversion type, let's see what happens when incompatible data types are given.

public class Main {
 public static void main(String[] args) {
   int intType = 20;
   // Short is of lower data type than int
   short shortType = intType;

   System.out.println("intType: "+intType);
   System.out.println("shortType: "+shortType);
 }
}

 

Compile Time Error:

compile time error

In the above case, short is of lower data type than int, hence a compile-time error occurs. In the next section, we will see how to solve this error.

Example for Type Conversion in Java:

  • Java

Java

public class Main {
public static void main(String[] args) {
  int intType = 20;
  // Float is of higher data type than int
  float floatType = intType;

  System.out.println("intType: "+intType);
  System.out.println("floatType: "+floatType);
}
}

 

Output:

intType: 20
floatType: 20.0
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is Type Casting?

Type casting is a process in which the programmer manually converts one data type into another data type. For this the casting operator (), the parenthesis is used. Unlike type conversion, the source data type must be larger than the destination type in type casting. The below flow chart has to be followed for successful type casting.

Flow chart for Type Casting

Flow chart for Type Casting

 

Type casting is also called Narrowing Type Casting/ Explicit Conversion/ Casting Up. In this case, as the higher data types with a larger size are converted into lower ones with a smaller size, there is a chance of data loss. This is the reason that this type of conversion does not happen automatically.

Type Casting

In type casting, the source data type with a larger size is converted into the destination data type with a smaller size.

In one of the examples covered in type conversion, you might recollect we encountered an error while converting an int data type to short. Now, we will see how to solve that error by using type casting. 

Example for Type Casting in Java:

  • Java

Java

public class Main {
public static void main(String[] args) {
int intType = 20;
// Short is of lower data type than int
short shortType = (short)intType;
 System.out.println("intType: "+intType);
 System.out.println("shortType: "+shortType);
}
}


Output:

intType: 20
shortType: 20

Also see, Duck Number in Java

Difference Between Type Casting and Type Conversion

ParameterType CastingType Conversion
DefinitionType casting is a process in which the programmer manually converts one data type into another data type.Type conversion is a process in which the data type is automatically converted into another data type
MethodExplicitly done by the programmer using casting operators.Can be implicit or explicit, depending on the language and context.
Syntax(newType) variableFunction calls, constructors, or other conversion methods.
CompatibilityMust be compatible data types. Can involve conversions between incompatible data types. 
ChangeImmediate change in the data type of the variable.The result may involve data loss or rounding errors, depending on the conversion m

Frequently Asked Questions

What is meant by type conversion and type casting in Java?

In type conversion, a compiler converts a lower data type into a higher data type. In typecasting, a higher data type is converted into a lower data type by the programmer.

What are the common methods used to change data types in Java?

Type Conversion and Type Casting in Java enable converting one data type into another.

What is explicit type conversion?

Explicit type conversion is when a higher data type is converted into a lower data type by the programmer explicitly.

What is automatic type conversion?

Automatic type conversion is when a compiler automatically converts a lower data type into a higher data type.

Differentiate between type conversion and type casting in Java based on data loss.

There is a chance of data loss in typecasting while the data is safe in type conversion.

Conclusion

That was all about Type Conversion and Type Casting in Java. This blog covered various concepts, examples, important points, and frequently asked questions relating to Type Conversion and Type Casting in Java were covered in detail. 

With the help of this blog, you are now in a position to solve some questions related to Type Conversion and Type Casting in Java on our Coding Ninjas Studio Platform.

Check this out, Access modifiers in java

To study more about data types, refer to Abstract Data Types in C++.

Don’t stop here. Check out our Java guided path to learn Java from Scratch. You can also check out the blog Difference between Type Casting and Type Conversion to understand Type Conversion and Type Casting in Java better.

We hope you found this blog useful. Feel free to let us know your thoughts in the comments section. 

Previous article
How to fix NoSuchElementException in Java?
Next article
Happy Number in Java
Live masterclass