Widening and Narrowing in Java
- 22nd Aug 2017
- 1
- 29700
- how narrowing works in java how widening works in java narrowing in java significance of narrowing and widening with an example significance of widening and narrowing in java with an example what is auto widening in java what is explicit narrowing in java what is impicit widening in java what is narrowing in java with an example widening 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)
- 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);
- }
- }
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.)
- 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);
- }
- }
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”
Very good explanation provided with simple examples for beginners. Thank you