1Assigning a value of one type to a variable of another type is
2known as Type Casting.
3Auto-boxing; is a process when you take a primitive value and
4assign into wrapper class object.
5Un-boxing; is a process when you take Wrapper class object
6and convert to primitive.
7
1// You can typecast to convert a variable of one data type to another.
2// Wide Casting converts small data types to larger ones.
3// Narrow Casting converts large data types to smaller ones.
4// Java can automatically Wide Cast.
5// Java will throw an error when trying to automatically Narrow Cast.
6// This is because data is often lost when Narrow Casting.
7// Narrow Casting must be done manually.
8
9//Wide Cast:
10int SomeNumber = 5;
11double WideCastedNumber = (double)SomeNumber;
12
13//Narrow Cast:
14double SomeNumber = 5.39;
15int NarrowCastedNumber = (int)SomeNumber;
16//Note: The data that holds the decimal places will be lost!
1JAVA: Example of cast:
2
3int SomeNumber = 5; //int
4double WideCastedNumber = (double)SomeNumber; //from int to double
5
6double myDouble = 9.78;
7int myInt = (int) myDouble; // from double to int