Java supports two types of castings
1. Primitive data type casting:
Assigning a value of one type to a variable of another type is known as type casting.
Type casting is of two types in java:
Widening(implicit type):- Also called as 'Automatic type' conversion
casting from byte->short->int->float..
Ex:
public class First
{
public static void main(String s[])
{
int a=10;
float i=a;
double b=i;//It is of type widening and can be converted automatically
System.out.println("Int value" +a);
System.out.println("Float value" +i);
System.out.println("Double value" +b);
}
}
An implicit cast means you don't have to write code for cast.
public class First
{
public static void main(String s[])
{
double a=25;
long i=(long)a;
int b=(int)i;
System.out.println("Int value" +b);
System.out.println("long value" +i);
System.out.println("Double value" +a);
}
}
1. Primitive data type casting:
Assigning a value of one type to a variable of another type is known as type casting.
Type casting is of two types in java:
Widening(implicit type):- Also called as 'Automatic type' conversion
casting from byte->short->int->float..
Ex:
public class First
{
public static void main(String s[])
{
int a=10;
float i=a;
double b=i;//It is of type widening and can be converted automatically
System.out.println("Int value" +a);
System.out.println("Float value" +i);
System.out.println("Double value" +b);
}
}
Output:
>javac First.java
>java First
Int value10
Float value10.0
Double value10.0
An implicit cast means you don't have to write code for cast.
Narrowing(explicit type):
casting from float->int->short->bytepublic class First
{
public static void main(String s[])
{
double a=25;
long i=(long)a;
int b=(int)i;
System.out.println("Int value" +b);
System.out.println("long value" +i);
System.out.println("Double value" +a);
}
}
Output:
>javac First.java
>java First
Int value25
long value25
Double value25.0
We can not perform conversion in following ways:
Any primitive to any reference type
The null value to any primitive type
A boolean to any primitive
Any primitive to a boolean
Assignment conversion:
It happens only when you assign a value to a variable of another type.
Ex:
public class First{
public static void main(String[] argv) {
int i;
float f;
i = 10;
f = i; // Assign an int value to a float variable
System.out.println(f);
System.out.println(i);
}
}
>javac First.java
>java First
10.0
10
Consider the following program:
public class First{
public static void main(String[] argv){
short i=9;
short d=10;
short j=i+d;
System.out.println(j);
}
}
Most expected answer is: 19
But it is not. Why?
In Java, numerical operators result in int or long.
So what actually it results in:
>javac First.java
First.java:5: error: possible loss of precision
short j=i+d;
^
required: short
found: int
1 error
Any primitive to any reference type
The null value to any primitive type
A boolean to any primitive
Any primitive to a boolean
Assignment conversion:
It happens only when you assign a value to a variable of another type.
Ex:
public class First{
public static void main(String[] argv) {
int i;
float f;
i = 10;
f = i; // Assign an int value to a float variable
System.out.println(f);
System.out.println(i);
}
}
>javac First.java
>java First
10.0
10
Consider the following program:
public class First{
public static void main(String[] argv){
short i=9;
short d=10;
short j=i+d;
System.out.println(j);
}
}
Most expected answer is: 19
But it is not. Why?
In Java, numerical operators result in int or long.
So what actually it results in:
>javac First.java
First.java:5: error: possible loss of precision
short j=i+d;
^
required: short
found: int
1 error
For more info: follow link
To get our answer, you should replace short j=i+d; with short j=(short) (i+d);
Note: It is same in the case of byte too.
2. Reference type casting:
One object type casting into another object reference is called reference type casting. This class can be its own class type or subclass type or super class type or interfaces.
There are two types of casting:
Upcasting(Widening reference conversion): Casting reference of subclass to parent class.
Note: If you upcast an object, it will lose all it's properties, which were inherited from below it's current position. And can acquire those properties after down casting it again.
Downcasting(Narrowing reference conversion): Casting reference of super class to subclass
There are some compile time and run time rules for casting in java
Cast exceptions thrown whenever user tries to cast the reference of one type with other type that is unrelated.
Below example will illustrate all the scenarios of reference casting:
class X {
public void first() {
System.out.println("First");
}
}
class Y extends X {
public void second() {
System.out.println("Second");
}
}
class Z extends X {
public void third() {
System.out.println("Third");
}
public static void main(String a[]) {
Y y = new Y();
y.first();
y.second();
Z z = new Z();
z.first();
z.third();
}
}
Output:
>javac X.java
>java Z
First
Second
First
Third
From the same example:
Case 1
X xy=new Y();//Cast applicable: upcasting
X xz=new Z();//Cast applicable: upcasting
xy.first();//first will be displayed
Case 2
Y yz = new Z(); incompatible type error is displayed
Y y1 = new X(); X is not a Y
Z z1 = new X(); X is not a Z
Case 3
X x=new X();
Y y=(Y) x;//Down casting but results in run time error
Output:
>javac X.java
>java Z
Exception in thread "main" java.lang.ClassCastException: X cannot be cast to Y
at Z.main(X.java:24)
Case 4:
X x=new X();
Y y= x;//Down casting but results in compile time error
Output:
>javac X.java
X.java:24: error: incompatible types
Y y=x;
^
required: Y
found: X
1 error
It is throwing both run time and compile time errors then how it can be used. Well, Down casting used to compare one object to another.
One object type casting into another object reference is called reference type casting. This class can be its own class type or subclass type or super class type or interfaces.
There are two types of casting:
Upcasting(Widening reference conversion): Casting reference of subclass to parent class.
Note: If you upcast an object, it will lose all it's properties, which were inherited from below it's current position. And can acquire those properties after down casting it again.
Downcasting(Narrowing reference conversion): Casting reference of super class to subclass
There are some compile time and run time rules for casting in java
Cast exceptions thrown whenever user tries to cast the reference of one type with other type that is unrelated.
Below example will illustrate all the scenarios of reference casting:
class X {
public void first() {
System.out.println("First");
}
}
class Y extends X {
public void second() {
System.out.println("Second");
}
}
class Z extends X {
public void third() {
System.out.println("Third");
}
public static void main(String a[]) {
Y y = new Y();
y.first();
y.second();
Z z = new Z();
z.first();
z.third();
}
}
Output:
>javac X.java
>java Z
First
Second
First
Third
From the same example:
Case 1
X xy=new Y();//Cast applicable: upcasting
X xz=new Z();//Cast applicable: upcasting
xy.first();//first will be displayed
Case 2
Y yz = new Z(); incompatible type error is displayed
Y y1 = new X(); X is not a Y
Z z1 = new X(); X is not a Z
Case 3
X x=new X();
Y y=(Y) x;//Down casting but results in run time error
Output:
>javac X.java
>java Z
Exception in thread "main" java.lang.ClassCastException: X cannot be cast to Y
at Z.main(X.java:24)
Case 4:
X x=new X();
Y y= x;//Down casting but results in compile time error
Output:
>javac X.java
X.java:24: error: incompatible types
Y y=x;
^
required: Y
found: X
1 error
It is throwing both run time and compile time errors then how it can be used. Well, Down casting used to compare one object to another.
No comments:
Post a Comment