Advertisement
//Java - Example of Widening a smaller primitive value to a larger 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);
}
}
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 larger primitive values like byte, short, int, long and float.
//Java - Example of Widening a subclass object reference to a wider superclass object reference
//Superclass
class A
{
public void message()
{
System.out.println("message from A");
}
}
//Subclass
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 a reference of superclass(A) type.
a.message();
}
}
Message from B
Advertisement
//Java - Example of 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);
}
}
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 explictly narrowed a biggerdouble value to several smaller primitive values like byte, short, int, long and float
by casting a double value.
//Java - Example of narrowing a superclass object reference to a subclass object reference
//Superclass
class A
{
public void message()
{
System.out.println("message from A");
}
}
//Subclass
class B extends A
{
public void message()
{
System.out.println("message from B");
}
public static void main(String... ar)
{
A a = new B(); //An object of subclass(B) is referenced by a reference of superclass(A)
B b = (B)a; //A reference of superclass(A) type is downcasted/narrowed to the reference of subclass(B) type.
b.message();
}
}
message from B
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement