0% found this document useful (0 votes)
53 views

Casting in Java

casting in java tutorial that explains what is type casting in java with many examples.type casting in java has two types upcasting and downcasting

Uploaded by

Mary James
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Casting in Java

casting in java tutorial that explains what is type casting in java with many examples.type casting in java has two types upcasting and downcasting

Uploaded by

Mary James
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Casting in JAVA

1) Introduction
Casting in java is used when assigning a value of type TypeA to a variable of type TypeB.
If you dont know how to use java variables, please check our corresponding java variables
tutorial.
There are two types of casting in java:

Implicit casting

Explicit casting
Each variable has a type in java.
Each data type has a size (number of bits used by that type).
byte < short < (char)< int < long < float < double
Each data type has a range of values that it can hold. This range of values is based on the size
of the data type.
The byte, long, float and double are signed.
The char type is unsigned. We cannot assign a byte or a short to a char and vice versa.

2) Casting example in java


The size of byte data type is 8 bits.
The range of values that can be coded using 8 bits is the following: from 0 to 127.
We can imagine that each type is a bag. The bags are of different sizes.
The bag associated to the byte type is the smallest one.
The bag associated to the double type is the biggest one.
A bag can be either full or partially filled.

3) Casting in java: problem


Lets imagine that we have a big bag and a small one.
The big bag is filled up with items.
What happens when we put the content of the big bag inside the small one?
The answer is that we cannot do that.
The same problem occurs when we try to assign a bigger type value to a smaller type variable.
int a = 100;
byte b = a;
Here we try to assign an int value (the content of the variable a) to a variable of type byte.
In this case, the java compiler will complain.

But, w said above that the byte type can hold the value 100.
Here, the java compiler complains because there is a chance that the variable a contains a
value that cannot be held by a variable of byte type.
We can imagine here that the bag associated to the int type is partially filled. But, it can be
totally filled in the future when our app runs.
So, we havent made a mistake.
Here where comes the necessity of casting.
In fact, we can use casting here to tell the compiler to trust us and not to complain.
The code becomes like this:
int a = 100;
byte b = (byte) a;
So, the casting in java is related to assignment.
It is done by putting the type of the variable on the left hand side of the assignment between
parentheses.
This is the only way to do casting.

4) Implicit casting in java


This type of casting is performed automatically by the compiler.
The developer doesnt do anything to make implicit casting.
This is an example of implicit casting:
char c = a;
int b = c;
In this case, the compiler converts the character a into its ASCHI code.
Then, it assigns this ASCHI code to the variable b.
The implicit casting is performed when assigning a smaller data type value to a bigger data
type variable.
This is called widening assignment or upcasting.
Here, we put the content of a small bag into a big bag. This operation can be done with no
problem.
There is no loss of information when making this operation.

5) Explicit casting in java


This type of casting is performed by the developer.
The explicit casting is performed when assigning a bigger data type value to a smaller data
type variable.
This is called narrowing assignment or downcasting.
If we dont perform the explicit casting, we will get a compiler error.
Here, we put the content of a big bag into a small bag.

This operation is problematic because it may cause a loss of information when making.
This is why we have to perform the java cast in this situation.

a. Explicit casting example


This is the same example mentioned above:
int a = 100;
byte b = (byte) a;

b. Explicit casting problem


If we dont use the explicit casting wisely, we may cause a loss of information:
int x = 128;
byte b = (byte) x;
b will get the value -127 which is incorrect. The maximum value that can be held by a byte is
128.

6) Not allowed casts in JAVA


int x = 20;
boolean b = (boolean) x;
We cannot cast a numerical type to a boolean type and vice versa.
int x = 20;
String s = (String) x; -> compiler error
We cannot cast a numeric type to a String type and vice versa.

7) The cast in JAVA has high precedence


byte b = (byte) 124 - 20;
The 124 is cast to a byte type then the 124 is promoted to int.
The whole expression in the right is of type int. We cannot assign an int to a byte without cast.
The solution is the following:
byte b = (byte) (124 - 20);
This java tutorial which deals with the java casting arrives at its end.
You can check also the full version of the casting in java tutorial online.
To check the other tutorials that deal with core java, please check our core java tutorial.
Please, dont forget to follow us on our how to program Facebook page to get our next java
tutorial.

You might also like