type casting java code example
Example 1: java type casting
int SomeNumber = 5;
double WideCastedNumber = (double)SomeNumber;
double SomeNumber = 5.39;
int NarrowCastedNumber = (int)SomeNumber;
Example 2: cast java
JAVA: Example of cast:
int SomeNumber = 5;
double WideCastedNumber = (double)SomeNumber;
double myDouble = 9.78;
int myInt = (int) myDouble;
Example 3: typecasting java
Assigning a value of one type to a variable of another type is
known as Type Casting.
Auto-boxing; is a process when you take a primitive value and
assign into wrapper class object.
Un-boxing; is a process when you take Wrapper class object
and convert to primitive.
Example 4: casting in java
Byte-->short-->char-->Int-->long-->float-->double
Example 5: down casting java
public class Fruit{}
public class Apple extends Fruit{}
public static void main(String args[]) {
Fruit parent = new Apple();
Apple child = (Apple)parent;
}