Java Type Casting
Type casting is converting an expression of a given one type into another type. It is denoted by (type). It is used to convert lower data type to higher data type to avoid data loss.
There are two types of type casting:
- Implicit conversion
- Explicit conversion

Syntax:-
(type)value;
Implicit conversion:
It does not require any operator for converted. They are automatically performed when a value is copied to a consistent type in the program.
The value has been promoted from int to double and we have not to specify any type-casting operator. This is known as standard conversion.
Example:-
public class Implicitcasting { public static void main(String[] args) { int i=20; double p; p=i; System.out.println("Implicit value is "+p); } }
Explicit conversion:
Many conversions that involve a different interpretation of the value require an explicit conversion.
They are not automatically performed. It need to give explicit type casting in the program.
Example:-
public class Explicitcasting { public static void main(String[] args) { int i=9, j=2; double z; z = (double) i / j; System.out.println("Value of Z : "+z); } }

If the float is converted to int, data which is present after the decimal point will be lost.
Quickly Find What You Are Looking For
Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.