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
1Primitive Type casting and Reference Type casting
2PRIMITIVE CASTING:
3implicit casting (casting smaller type to larger
4int a = 100; double c = a;)
5explicit casting (casting larger to smaller
6byte b = (byte) a;
7REFERENCE CASTING:
8upcasting - implicitly done (casting smaller type to larger)
9Collection<Integer> collection = new ArrayList<>();
10downcasting (cast larger type to smaller one )
11List <Integer> list = ( ArrayList ) collection;
12FRAMEWORK EXAMPLE:
13Up Casting, Down Casting in Multi Browser testing.
14WebDriver, Chrome Driver, Firefox Driver object
15I casted to WebDriver to making reference type.
16Whenever you taking Screenshots,
17whenever executing Java script command.
1 int x = 0;
2 x += 1.1; // just fine; hidden cast, x == 1 after assignment
3 x = x + 1.1; // won't compile! 'cannot convert from double to int'
4