Type Casting:
Type casting means to explicitly convert one type of data to another type of data.
Type casting has 2 types:
- Upcasting
- Downcasting
Upcasting:
Converting lower type value to upper type value is known as Upcasting.
http://instanceofjavaforus.blogspot.in/ |
Program:
package com.instanceofjavaforus;
public class Upcastingtest {
public static void main(String[] args) {
int a=100;
long b=a;
float c=b;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
Output:
100
100
100.0
Downcasting:
Converting upper type value to lower type value is known as Downcasting.
http://instanceofjavaforus.blogspot.in/ |
Program:
package com.instanceofjavaforus;
public class Downcastingtest {
public static void main(String[] args) {
double a=10.4;
long b=(long)a;
int c=(int)b;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
Output:
10.4
10
10
Program :
package com.instanceofjavaforus;
class Simple{
public static void main(String args[]){
char ch = 'A';
long a = ch;
System.out.println(a);
}
}
Output:
65
Program:
package com.instanceofjavaforus;
class Simple{
public static void main(String args[]) {
double d = 10.5;
int i = (int) d;
System.out.println(i);
}
}
Output:
10
package com.instanceofjavaforus;
class Simple{
public static void main(String args[]){
char ch = 'A';
long a = ch;
System.out.println(a);
}
}
Output:
65
Program:
package com.instanceofjavaforus;
class Simple{
public static void main(String args[]) {
double d = 10.5;
int i = (int) d;
System.out.println(i);
}
}
Output:
10
No comments