Categories: Java

Type casting in Java

In Java, type casting is a method or process for manually and automatically converting one data type into another. The compiler performs the automatic conversion, while the programmer performs the manual conversion. In this section, we’ll go over typecasting and the various types with examples.

Types of Type Casting

There are two types of typecasting:

  • Implicit conversion
  • Explicit conversion
Implicit conversion

Implicit type casting is the process of converting a lower data type to a higher one. It’s also known as casting down or implicit conversion. It’s done for you automatically. Furthermore, it is secure because there is no risk of data loss. It occurs when the following occurs:

  • Both data types must be compatible with each other.
  • The target type must be larger than the source type.
Example:

public class TypeCastingExample  
{  
public static void main(String args[])  
{  
int x = 7;  
//automatically converts the integer type into long type  
long y = x;  
//automatically converts the long type into float type  
float z = y;  
System.out.println("Before conversion, int value "+x);  
System.out.println("After conversion, long value "+y);  
System.out.println("After conversion, float value "+z);  
}  
}  
Output:
Before conversion, int value 7
After conversion, long value 7
After conversion, float value 7.0
Explicit conversion

Explicit type casting is the process of converting a higher data type to a lower one. It’s also known as casting up or explicit conversion. The programmer performs this task manually. The compiler will report a compile-time error if we do not perform casting.

Example:

public class TypeCastingExample  
{  
public static void main(String args[])  
{  
double d = 166.66;  
//converting double data type into long data type  
long l = (long)d;  
//converting long data type into int data type  
int i = (int)l;  
System.out.println("Before conversion: "+d);  
//fractional part lost  
System.out.println("After conversion into long type: "+l);  
//fractional part lost  
System.out.println("After conversion into int type: "+i);  
}  
}  
Output:
Before conversion: 166.66
After conversion into long type: 166
After conversion into int type: 166

Note: also read about the Operator Precedence & Associativity

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago