-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeCasting.java
More file actions
22 lines (20 loc) · 827 Bytes
/
TypeCasting.java
File metadata and controls
22 lines (20 loc) · 827 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class TypeCasting {
public static void main(String[] args){
float fn= 20.5f;
int n = (int)fn;
String st= "72";
Integer s = Integer.valueOf(st);// converting string to int requires
int s1 = Integer.parseInt(st);
// System.out.print("fn = ");
// System.out.println(fn);
// System.out.print("n = ");
System.out.println(String.format("fn= %f n= %d",fn,n));
System.out.println(n);
System.out.println(s);
System.out.println(s1);
}
// Widening Casting (automatically) - converting a smaller type to a larger type size
// byte -> short -> char -> int -> long -> float -> double
// Narrowing Casting (manually) - converting a larger type to a smaller size type
// double -> float -> long -> int -> char -> short -> byte
}