Java Data Type Conversion and Casting: Comprehensive Guide
Table of Content:
Type Conversion in Java
When you assign the value of one data type to another, the two types might not be compatible with each other. If the data types are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion or widening and if not then they need to be cast or converted explicitly known as casting or narrowing.
Casting is an operation that converts a value of one data type into a value of another data type. Casting a type with a small range of a type with a larger range is known as widening a type. Casting a type with a large range of a type with a smaller range is known as narrowing a type.
it is always possible to assign an int value to a long variable.
there is no automatic conversion defined from double to byte.
Type Conversion in Java
The syntax for casting a type is to specify the target type in parentheses, followed by the variable name or the value to be cast. For example, the following statement
Syntax for Type Conversion (int <- float)
class Casting{
public static void main(String[] args){
int number;
float fval= 32.33f;
number= (int)fval;
System.out.println(number);
}
}
Output:
32
Press any key to continue . . .
Syntax for Type Conversion (int <- double)
class Casting{
public static void main(String[] args){
int number;
double dval= 32.33;
number= (int)dval;
System.out.println(number);
}
}
Output:
32
Press any key to continue . . .
Syntax for Type Conversion
class Casting{
public static void main(String[] args){
System.out.println((int)1.87);
System.out.println((double)1 / 2);
}
}
Output:
1
0.5
Press any key to continue . . .
Automatic Conversions or widening
class Casting{ public static void main(String[] args){ int number; double dval= 32.33; number= (int)dval; System.out.println(number); } }Output:
32 Press any key to continue . . .
Syntax for Type Conversion
class Casting{
public static void main(String[] args){
System.out.println((int)1.87);
System.out.println((double)1 / 2);
}
}
Output:
1
0.5
Press any key to continue . . .
Automatic Conversions or widening
When one type of data is assigned to another type of variable, an automatic type conversion
will
take place if the following two conditions are met:
- The two types are compatible.
- The destination type is larger than the source type.
When these two conditions are met, a widening conversion takes place. For example, the int type is always large enough to hold all valid byte values, so no explicit cast statement is required.
Widening Example
class Widening{
public static void main(String[] args){
int number=10;
float point=number;
System.out.println(number);
System.out.println(point);
}
}
Output:
10
10.0
Press any key to continue . . .
The numeric types, including integer and floating-point types, are compatible with each other.
No automatic conversions from the numeric types to char or boolean. Also, char and boolean are not compatible with each other.
Casting Incompatible Types or Narrowing
When one type of data is assigned to another type of variable, a narrowing type conversion
will take
place if the following two conditions are met:
- The two types are incompatible.
- The destination type is smaller than the source type.
When these two conditions are met, a narrowing conversion takes place. For example, the int is not large enough to hold all valid double values, so explicit cast statement is required.
Narrowing Example
class Narrowing {
public static void main(String[] args){
float point=10.5f;
//int a=f;//Compile time error
int number=(int)point;
System.out.println(point);
System.out.println(point);
}
}
Output:
10.5
10.5
Press any key to continue . . .
Overflow
class Overflow{
public static void main(String[] args){
//Overflow
int number=150;
byte b=(byte)number;
System.out.println(number);
System.out.println(b);
}
}
Output:
150
-106
Press any key to continue . . .
Adding Lower Type
class AddingLowerType{
public static void main(String[] args){
byte r=10;
byte s=10;
//byte c=r+s;//Compile Time Error: because r+s=20 will be int
byte c=(byte)(r+s);
System.out.println(c);
}
}
Output:
20
Press any key to continue . . .
class Overflow{ public static void main(String[] args){ //Overflow int number=150; byte b=(byte)number; System.out.println(number); System.out.println(b); } }Output:
150 -106 Press any key to continue . . .