Type Casting

We often encounter situation where we need to store a value of one type into a variable of Another type. In such a case , we must casting the value to be stored by proceeding it with the type name in parentheses.

We know the automatic type conversions are helpful, they will not fulfill all needs. For example, if you want to assign an int value to a byte variable then? This conversion not be perform automatically, because a byte is smaller than an integer. This kind of conversion are in sometimes called a narrowing conversion, since you are explicitly making the value narrower, so that it will fit into the target type.

To create a conversion between two incompatible types, you must use a casting.

A casting is simply an explicit data-type conversion.

For Example :  int m=60;
		Byte n= (byte)m;
		Long count = (long)m;

This following fragment casting an int to a byte. If the integer’s value is larger than the range of byte, it will be reduced modulo (the remainder of an integer division by the) byte’s range.

int x;
 byte y; 
// ... 
y = (byte) x;

A different type of conversion will occur when a floating-point value is assigned to an integer type: truncation. you know, integers don’t have fractional components.

When the floating-point value is assigned to an integer type, the fractional component is lost. an example, if the value 1.35 is assigned to an integer, the resulting value will simply be 1.

The 0.26 will have been truncated. Of course, if the size of the whole number component is too large to fit into target integer type, then that value will be reduced modulo the target type’s range.

This following program demonstrates some type conversions that require casting:

// Demonstrate casts. 
class Conversion {
	public static void main(String args[]) {
		byte b;
		int i = 257;
		double d = 323.142;
		System.out.println("\nConversion of int to byte.");
		b = (byte) i;
		System.out.println("i and b " + i + " " + b);
		System.out.println("\nConversion of double to int.");
		i = (int) d;
		System.out.println("d and i " + d + " " + i);
		System.out.println("\nConversion of double to byte.");
		b = (byte) d;
		System.out.println("d and b " + d + " " + b);

This program generates the following output:

Conversion of int to byte. i and b 257 1
Conversion of double to int. d and i 323.142 323
Conversion of double to byte. d and b 323.142 67

Casting is often necessary when the method return a different type than the one we require. For integer type can be cast to any other type except boolean. When Casting into a smaller data type may result in a loss of data. List those casts that are guaranteed to Result in no loss of information. type-casting-java

Automatic Conversion

For some situation it is possible to assign a value of one type to a variable of different type without a cast. Java does the conversion of assigned value automatically it is known as automatic type conversion.

It is possible only if the destination type has enough precession to store the source values.

For example:
Int is large enough to hold value of a byte.

byte  b=  45;
int  a=b;

above statements are valid statements. In that process of assigning a smaller type to a large one is known as promotion and that of assigning a larger type to a smaller one is known as narrowing. The narrowing may result in loss of information.

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : We have closed registration for training

Please do email to chercher.tech@gmail.com for any queries

If you already registered then please do check your email for the Webex link for the training starting from 15th Nov 2018
Join My Facebook Group
Join Group