Type Casting in java

Implicit conversion :
  1. The compiler is responsible for Implicit type cast conversion
  2. Whenever we need to cast a smaller data type value to a bigger data type variable then implicit conversion is used.
  3. Implicit typecast is also know as widening or upcasting.
  4. Implicit type casting no loss of information
Example  1:
int a = 'a';
System.out.prinitln(a);  // 97

In the above Example compiler converts char to int automatically by implicit typecasting.

Example 2:
double d = 10;
System.out.println(d);  // 10.0

In the above Example compiler converts int values to double automatically by implicit typecasting.

Below are possible implicit typecasting :

Explicit conversion :
  1. The programmer is responsible for Explicit type cast conversion
  2. Whenever we need to cast a Bigger data type value to a smaller data type variable then Explicit conversion is used.
  3. Explicit typecast is also know as narrowing or downcasting.
  4. in the Explicit type casting loss of information
Example  1:
Char a = (char)10;
System.out.prinitln(a);  


Below are possible Explicit typecasting :











Comments

Popular posts from this blog

Primitive Data Types in java

let - Keyword in JavaScript (Block scope)

Coding Problem 4: Smallest window containing 0, 1 and 2 [GFG - Problem Solving]