Java Data Type Casting Type Conversion
Summary: By the end of this tutorial "Java Data Type Casting Type Conversion", you will be comfortable with converting one data type to another either implicitly or explicitly.
Java supports two types of castings – primitive data type casting and reference type casting. Reference type casting is nothing but assigning one Java object to another object. It comes with very strict rules and is explained clearly in Object Casting. Now let us go for data type casting.
Java data type casting comes with 3 flavors.
- Implicit casting
- Explicit casting
- Boolean casting.
1. Implicit casting (widening conversion)
A data type of lower size (occupying less memory) is assigned to a data type of higher size. This is done implicitly by the JVM. The lower size is widened to higher size. This is also named as automatic type conversion.
Examples:
int x = 10; // occupies 4 bytes
double y = x; // occupies 8 bytes
System.out.println(y); // prints 10.0
In the above code 4 bytes integer value is assigned to 8 bytes double value.
2. Explicit casting (narrowing conversion)
A data type of higher size (occupying more memory) cannot be assigned to a data type of lower size. This is not done implicitly by the JVM and requires explicit casting; a casting operation to be performed by the programmer. The higher size is narrowed to lower size.
double x = 10.5; // 8 bytes
int y = x; // 4 bytes ; raises compilation error
In the above code, 8 bytes double value is narrowed to 4 bytes int value. It raises error. Let us explicitly type cast it.
double x = 10.5;
int y = (int) x;
The double x is explicitly converted to int y. The thumb rule is, on both sides, the same data type should exist.
3. Boolean casting
A boolean value cannot be assigned to any other data type. Except boolean, all the remaining 7 data types can be assigned to one another either implicitly or explicitly; but boolean cannot. We say, boolean is incompatible for conversion. Maximum we can assign a boolean value to another boolean.
Following raises error.
boolean x = true;
int y = x; // error
boolean x = true;
int y = (int) x; // error
byte –> short –> int –> long –> float –> double
In the above statement, left to right can be assigned implicitly and right to left requires explicit casting. That is, byte can be assigned to short implicitly but short to byte requires explicit casting.
Following char operations are possible
public class Demo
{
public static void main(String args[])
{
char ch1 = 'A';
double d1 = ch1;
System.out.println(d1); // prints 65.0
System.out.println(ch1 * ch1); // prints 4225 , 65 * 65
double d2 = 66.0;
char ch2 = (char) d2;
System.out.println(ch2); // prints B
}
}
Pass your comments for the betterment of this tutorial "Java Data Type Casting Type Conversion".
=============================================================================
Your one stop destination for all data type conversions
byte TO | short | int | long | float | double | char | boolean |
short TO | byte | int | long | float | double | char | boolean |
int TO | byte | short | long | float | double | char | boolean |
float TO | byte | short | int | long | double | char | boolean |
double TO | byte | short | int | long | float | char | boolean |
char TO | byte | short | int | long | float | double | boolean |
boolean TO | byte | short | int | long | float | double | char |
String and data type conversions
String TO | byte | short | int | long | float | double | char | boolean |
byte | short | int | long | float | double | char | boolean | TO String |
Sir , i have a problem plz explain me how?
char a=’p’:
int i=(int)a;
And it’s answer is 112
How ???
sir , why the value of d1 is printed as 65 when ch1 is ‘A’ ?
65 is the ASCII integer value of A.
because it prints its ascill value
is it possible to convert float to int explicitly in java why ?how it works?
Mantissa part is removed from float and whole number part assigned to int.
Sir can you please tell what is wrong with this fragment of code ?
byte b=10;
byte c=20;
byte d=b+c; – This statement generates an error stating “possible loss of precision”
b+c implicitly becomes an int. Assigning int to byte requires casting like this.
byte d = (byte)(b+c);
Define type conversion plz
Type conversion in a language means converting one data type value to another. In Java, type casting also applies to objects.
Suppose if i give inputs from keyboard to a variable and if i declare that variable as int and the given input is in double type(eg.2.3) and i have declared int for that variable so i get exceptions pls guide me
Program
import java.util.Scanner;
class Program
{
public static void main(String[] args)
{
Scanner s1=new Scanner(System.in);
int x1=s1.nextInt();// if i give here input as 12.3 i get error
System.out.println(“x1 value : “+x1);
}
}
This is where casting required.
It is naturally. nextInt() reads int only and cannot cast double to int.
Hi Amar,
Your attempting to cast a double (12.3) as an integer, which is why the error is occuring. To fix this do the following:
import java.util.Scanner;
class Program
{
public static void main(String[] args)
{
Scanner s1= new Scanner(System.in);
double x1=s1.nextDouble();
System.out.println(“x1 value:”+x1);
The output will be in decimal format since it is a Double that is awaiting input by the user. If you put in an integer it will automatically convert that into a Double format as well.
Sir as per your notes from small data type to large data type it is implicit conversion and for high data type to small data type we reqiure explicit conversion so how a long which is a data type converted automatically into float by jvm automatically as per your type promotion chart.
Pls reply important
See my notes given:
byte –> short –> int –> long –> float –> double
The left-side value can be assigned to any right-side value and is done implicitly. The reverse like float to short requires explicit casting.
I put another question to you. double and long each take 8 bytes memory. Which can be assigned to what? long can be assigned to double but double cannot be assigned to long. Why? It is due to floating-point acceptance.
Sir what is this floating point acceptance could you pls explain.
floating-point matters in the designing of data types conversions. For this reason, long can be implicitly converted to double but double cannot be implicitly into long.
hi frnds, i wanna to learn Java , how can i do this in the simple way.
Go as per way2java, category-wise.
Take a course in Java by a certified and reputational company or educational institute
sir, is type conversion and type casting the same in java?
Both are used in the same sense.
Write a program to print the largest value of byte, short, int, long, float and double data types. Output should be separated by space.
Do not ask anybody to write a program give to you. Ask anyone some tips how to solve the problem. For your problem use the methods like
public static final float MAX_VALUE;
Life every wrapper class has MAX_VALUE, check and find in Java API.
sir,
Can a byte value be cast to a double ?
No, problem, it can be done. Refer the site.
sir,Is there any benifit of implicit casting.
According to good coding standard,Which is better to use composition or inheritence in?
Without object castings no Java coding at all. You use casting operations in collections classes (DS), RMI and in many places. Reg. composition and inheritance, everyone has got their own applications in realtime coding.