Type casting in java is used when
assigning a value of type TypeA to a variable of type TypeB.
There are two types of casting in java:
- Implicit casting
- Explicit casting
If you don’t know
how to use java variables, please check our corresponding java variables tutorial.
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.
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.
Casting in java: problem
Let’s 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 haven’t
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.
Implicit casting in java
This type of
casting is performed automatically by the compiler.
The developer
doesn’t 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.
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 don’t
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.
Explicit casting example
This is the same example mentioned above:
int a = 100; byte b = (byte) a;
Explicit casting problem
If we don’t 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.
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.
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.
To check the
other tutorials that deal with core java, please check our core java tutorial.
Please, don’t forget to follow us on our how to program Facebook page to get our
next java tutorial.
Post a Comment