Advertisement



< Prev
Next >



Widening and Narrowing











Widening a smaller primitive value to a bigger primitive type.


class A
{
public static void main(String... ar)
{
byte b=10;

short s= b;	//byte value is widened to short
int i=b;	//byte value is widened to int
long l=b;	//byte value is widened to long
float f=b;	//byte value is widened to float
double d=b;	//byte value is widened to double

System.out.println("short value : "+ s);
System.out.println("int value : "+ i);
System.out.println("long value : "+ l);
System.out.println("float value : "+ f);
System.out.println("double value : "+ d);
}
}


Output-


short value : 10
int value : 10
long value : 10
float value : 10.0
double value : 10.0
In the preceding code, we have widened a smaller byte value to several bigger primitive values like byte, short, int, long and float.




Widening a subclass object reference to a wider superclass object reference.


This is also known as upcasting the subclass reference to its superclass reference.
class A
{
public void message()
{
System.out.println("message from A");
}
}

class B extends A
{

public void message()
{
System.out.println("message from B");
}

public static void main(String... ar)
{
B b = new B();
A a = b;   //reference of a subclass(B) type is widened to the reference of superclass(A) type.
a.message();
}
}


Output-


Message from B



Advertisement










Narrowing a bigger primitive value to a small primitive value.


class A
{
public static void main(String... ar)
{
double d =10.5;

byte b = (byte)d;		//Narrowing double to byte 
short s= (short)d;		//Narrowing double to short 
int i= (int)d;			//Narrowing double to int 
long l= (long)d;		//Narrowing double to long
float f= (float)d;		//Narrowing double to float 
 

System.out.println("Original double value : " +d);
System.out.println("Narrowing double value to short : "+ s);
System.out.println("Narrowing double value to int : "+ i);
System.out.println("Narrowing double value to long : "+ l);
System.out.println("Narrowing double value to float : "+ f);
System.out.println("Narrowing double value to byte : "+ b);

}
}


Output-

Original double value : 10.5
Narrowing double value to short : 10
Narrowing double value to int : 10
Narrowing double value to long : 10
Narrowing double value to float : 10.5
Narrowing double value to byte : 10
In the preceding code, we have narrowed a bigger double value to several smaller primitive values like byte, short, int, long and float by putting a cast around the double value.




Narrowing an superclass object reference to subclass object reference.


This is also known as downcasting the superclass reference to its subclass reference.
class A				//superclass
{
public void message()
{
System.out.println("message from A");
}
}

class B extends A		//subclass
{

public void message()
{
System.out.println("message from B");
}

public static void main(String... ar)
{
A a =  new B();	//object of subclass(B) is referenced by reference of superclass(A)
B b = (B)a;	//reference of a superclass(B) type is downcasted/narrowed to the reference of subclass(A) type.
b.message();
}
}


Output -


message from B




Please share this article -




< Prev
Next >
< Autoboxing and Unboxing
Java Exception>



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement