GroTechMinds

Primitive Type Casting in Java Widening and Narrowing

Primitive Type Casting in Java: Widening and Narrowing

Introduction:

Primitive type casting is an important concept in Java, that allows us to convert one primitive data type into another. This process can be broadly classified into two types, namely widening and narrowing. Understanding these concepts is very much essential for writing efficient Java programs. With this blog, let us have a detailed overview of primitive type casting with examples to further our understanding.

Range of data type:

To understand typecasting, we must understand the range of the data types we are going to upcast. The range can be calculated using the formula “-2^(n-1) to (-2^(n-1))-1”, where n is the bits value corresponding to each data type. The range of various data types and how to calculate them are given as follows.

Code Snippet:
				
					package core_java;
public class Typecasting_Range {
public static void main(String[] args) {
double d=1.61803398874989484820458683436563811772030917980576286213544862270526046281890;
float f=1.61803398874989484820458683436563811772030917980576286213544862270526046281890f;
System.out.println("The range of byte data type is from "+(byte)Math.pow(-2, (8-1))+" to "+(byte)((Math.pow(2, (8-1)))-1));
System.out.println("The range of short data type is from "+(short)Math.pow(-2, (16-1))+" to "+(short)((Math.pow(2, (16-1)))-1));
System.out.println("The range of int data type is from "+(int)Math.pow(-2, (32-1))+" to "+(int)((Math.pow(2, (32-1)))-1));
System.out.println("The range of long data type is from "+(long)Math.pow(-2, (64-1))+" to "+(long)((Math.pow(2, (64-1)))-1));
System.out.println("The double value "+d+" can hold upto 15 decimal places");
System.out.println("The float value "+f+" can hold upto 6 decimal places");
}
}

				
			
Output Screenshot:
java 1
What is Type Casting?

Type casting is the process of converting a variable from one data type to another. In Java, there are two main types of type casting for primitive data types.

java 2
1. Widening (can be done implicitly and explicitly)

Widening is the conversion of a smaller data type into a larger one. This type of casting is done automatically by Java and does not require any explicit syntax.

Code Snippet:
				
					public static void main(String[] args) {
        	byte a=70;
        	short b=a;
        	int c=a;
        	long d=a;
        	float e=a;
        	double f=a;  
        	System.out.println(a+" is of byte datatype");
        	System.out.println(b+" is of short datatype");
        	System.out.println(c+" is of int datatype");
        	System.out.println(d+" is of long datatype");
        	System.out.println(e+" is of float datatype");
        	System.out.println(f+" is of double datatype");
}

				
			
Output Screenshot:
java 3
2. Narrowing (can be done only in explicit way)

Narrowing is the conversion of a larger data type into a smaller one. Unlike widening, this type of casting requires explicit syntax.

Code Snippet:
				
					package core_java;
public class Typecasting_Narrowing {
public static void main(String[] args) {
        	double a=Math.PI;  
        	float b=(float) a;
        	long c=(long) a;
        	int d=(int) a;
        	short e=(short) a;
        	byte f=(byte) a;
        	System.out.println(a+" is of double datatype");
        	System.out.println(b+" is of float datatype");
        	System.out.println(c+" is of long datatype");
        	System.out.println(d+" is of int datatype");
        	System.out.println(e+" is of short datatype");
        	System.out.println(f+" is of byte datatype");
}
}

				
			
Output Screenshot:
java 4
Compilation error when we try narrowing implicitly:
java 5
Some Important Considerations when it comes to primitive type casting:
1. Data Loss: Narrowing can result in data loss.

(e.g.,) type casting a double value to a float variable will discard all the fractional parts except the six digits after the decimal place, and when it is converted into int data type, all the decimal places are lost.

Code Snippet:
				
					public static void main(String[] args) {
double golden_ratio=1.61803398874989484820458683436563811;
float golden_ratio_f=(float) golden_ratio;
int golden_raio_i=(int) golden_ratio;
System.out.println(golden_ratio_f+" is the float value after type casting (narrowing)");
System.out.println(golden_raio_i+" is the int value after type casting (narrowing)");
}

				
			
Output Screenshot:
java 6
2. Range Overflow:

When casting larger integers to smaller integers (e.g., int to byte), if the value exceeds the range of the smaller type, it wraps around. In narrowing data overflow happens as a result of a data type’s smaller value range.

Code Snippet:
				
					public static void main(String[] args) {
double pi_pow=Math.pow(Math.PI, 10);
float pi_f=(float) pi_pow;
long pi_l=(long) pi_pow;
int pi_i=(int) pi_pow;
short pi_s=(short) pi_pow;
byte pi_b=(byte) pi_pow;
System.out.println(pi_pow+" is the value before type casting");
System.out.println(pi_f+" is the float value after type casting (narrowing)");
System.out.println(pi_l+" is the long value after type casting (narrowing)");
System.out.println(pi_i+" is the int value after type casting (narrowing)");
System.out.println(pi_s+" is the short value after type casting (narrowing)");
System.out.println(pi_b+" is the byte value after type casting (narrowing)");
}

				
			
Output Screenshot:
java 7
  1. Since widening happens from a smaller data type to a bigger data type, there is space for accommodation, and data loss or data overflow is not a concern when it comes to widening.
Conclusion:

In Java, primitive type casting is a very fundamental concept that allows us to convert a value to a value that represents our desired data type in correspondence with its range. Understanding the differences between widening and narrowing is crucial for writing robust and reliable programs.

Upskill Yourself
Consult Us