Java Data Type Conversion and Casting: Comprehensive Guide

Rumman Ansari   Software Engineer   2024-08-19 11:50:51   15725  Share
Subject Syllabus DetailsSubject Details 4 Questions
☰ TContent
☰Fullscreen

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.

type conversion in java

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 type conversion in java 1

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 type conversion in java 2

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 type conversion in java 2

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:

  1. The two types are compatible.
  2. 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.

type conversion in java

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.

Important Point Although the automatic type conversions are helpful, they will not fulfill all needs. For example, what if you want to assign a float value to a byte variable? This conversion will not be performed automatically because a byte is smaller than a float. This kind of conversion is sometimes called a narrowing conversion since you are explicitly making the value narrower so that it will fit into the target type.

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:

  1. The two types are incompatible.
  2. 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.

type conversion in java

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 . . .
truncation: when a floating-point value is assigned to an integer type: truncation takes place, As you know, integers do not have fractional components. Thus, when a floating-point value is assigned to an integer type, the fractional component is lost.
For example, if the value 45.12 is assigned to an integer, the resulting value will simply be 45. The 0.12 will have been truncated.

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 . . .
MCQ Available

There are 1 MCQs available for this topic.

1 MCQ