Widening and Narrowing in Java

In Java, assigning one primitive type to another primitive type is possible if they are related primitives

Example: We can assign long to int , short to int and float to double etc.

Way of assigning one type of primitive to other primitive type is classified as 2 categories


1.Widening(Auto or implicit)

2.Narrowing(Explicit)


1.Widening


In this type, we will assign smaller type to larger type.

Example:

short=byte
int=short
long=int
float=long
double=float

In the above case, we can see that , we are assigning smaller type to larger type (byte to short, short to int etc)

  1. package com.kb.primitives;
  2.  
  3. public class Autowidening{
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         int i = 100;
  8.         long l = i; // no explicit type casting is required
  9.         float f = l; // no explicit type casting required
  10.         System.out.println("i= " + i);
  11.         System.out.println("l= " + l);
  12.         System.out.println("f= " + f);
  13.  
  14.     }
  15.  
  16. }
package com.kb.primitives;

public class Autowidening{

	public static void main(String[] args) {

		int i = 100;
		long l = i; // no explicit type casting is required
		float f = l; // no explicit type casting required
		System.out.println("i= " + i);
		System.out.println("l= " + l);
		System.out.println("f= " + f);

	}

}



In the above program, we have done the widening and in this case, we are not doing any explicit casting and hence we call it as “Auto widening” or “Implicit widening


2.Narrowing(Explicit)


In this type, we will assign a larger type value to a variable of smaller type.

Example:

byte=short
short=int
int=long
long=float
float=double

In the above case, we can see that, we are assigning larger type to smaller type (double to float, int to short etc.)

  1. package com.kb.primitives;
  2.  
  3. public class ExplicitNarrowing {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         double d = 100.01;
  8.                // long l=d;  compile time error as we are assigning larger type to smaller type without casting
  9.         long l = (long) d; // explicit type casting is required
  10.         int i = (int) l; // explicit type casting is required
  11.  
  12.         System.out.println("i= " + i);
  13.         System.out.println("l= " + l);
  14.         System.out.println("d= " + d);
  15.  
  16.     }
  17.  
  18. }
package com.kb.primitives;

public class ExplicitNarrowing {

	public static void main(String[] args) {

		double d = 100.01;
               // long l=d;  compile time error as we are assigning larger type to smaller type without casting
		long l = (long) d; // explicit type casting is required
		int i = (int) l; // explicit type casting is required

		System.out.println("i= " + i);
		System.out.println("l= " + l);
		System.out.println("d= " + d);

	}

}


In the above program, we have done the narrowing and in this case, we are doing explicit casting of larger type to smaller type.
And hence we call it as “Explicit narrowing

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on