Friday, August 20, 2010

Assignments.

By the title I actually mean the 3rd chapter of the K&B SCJP book. I have to admit that it was really surprising. I have learnt many new things that I was completly unaware of. Of course, in the real projects, when you use some IDE it's nearly impossible to make mistakes of this sort but on the exam they can be very savage. So below you will find some important points from this chapter that you really should pay attention to, during the exam.

First, we'll talk about the literals. Remember that every integer literal is an int. So there is no problem if we want to do the following assignments:
int a = 5; (fits well)
long b = 6; (fits well since long is bigger than int)
but what if we try to assign the integer literal (so an int) to the smaller type? Let's have a look!
byte c = 7;
And yeah, there is still no problem. That's of course good for us but basing on the above that shouldn't work. Well, that happens because the compiler makes an implicit cast so in fact, it looks like this:
byte c = (byte)7;
Alright. That's very convenient for us. But this implicit cast is applied only to literals! So this is not going to work:
int a = 5;
byte b = a;
To compile the code consisting of the above assignments you need to do an explicit cast:
int a = 5;
byte b = (byte)a;
Also keep in mind the following rule. The result of any expression involving an int or anything smaller, like byte and short will result in an int! So the following will... not compile!
byte a = 5;
byte b = 1;
byte c = a + b;
You need to put an explicit cast! One more thing about the floating points literals. They are always doubles. And here the compiler won't make an implicit cast, the following code will not compile:
float d = 2.5;
Instead you should write:
float d = (float)2.5;
Or mark that what you mean is really a float by adding and f or F at the end of the literal:\
float d = 2.5f;
Ok, that's it for the literals! Now let's move on to the primitive casting.
First of all, it's important to remember that the implicit cast happens when we assing a smaller to a bigger. So for instance:
int a = 5;
byte b = (byte)a;
long c = 'a' + 'b';

a = b;
c = a;
c = b;
These are all legal assignments. But the followings are not:
b = a;
b = c;
a = c;
And what do we need? An explicit cast!
b = (byte)a;
b = (byte)c;
a = (int)c;
Et voila monsieur! Also the same thing happens with floating points. All the following are legal:
double d = 2.5;
float f = 3.4f;
int i = 12;
long l = 45;

d = l; // assigning long to double
d = f; // assigning float to double
f = i; // assigning integer to float
But in case when a truncation may occur we have to make an explicit cast otherwise our code will produce an error.
The followings are legal but the cast is necessary:
double d = 2.5;
float f = 3.4f;
int i = 12;
long l = 45;

f = (float)d;
i = (int)f;
l = (long)d;
That's a piece of cake but I'm sure that you've found here something new and interesting for you!

No comments:

Post a Comment