W
wybrand
Hai fellow programmers.
Can you please give me an answer to the following??
See next code-fragment:
class a {
public static void main( String [] arg ) {
short a;
int b;
// Implicit cast.
a = 65;
// Explicit cast which compiles.
a = (int)65;
// Explicit cast which does not compile.
b = (long)65;
}
}
The first statement compiles because of implicit typecasting and there
is no loss of information (65 < 2^16-1).
The third one does not compile because we do an explicit typecast and
a long is 64 bytes and an integer is 32.
Why does the second statement compile?? A integer doesn't fit in a
short, just like a long does not fit in a integer (third statement).
It looks like if a implicit typecast takes place just like the fist
statement.
Thanks in advance.
Greetings,
Wybrand.
Can you please give me an answer to the following??
See next code-fragment:
class a {
public static void main( String [] arg ) {
short a;
int b;
// Implicit cast.
a = 65;
// Explicit cast which compiles.
a = (int)65;
// Explicit cast which does not compile.
b = (long)65;
}
}
The first statement compiles because of implicit typecasting and there
is no loss of information (65 < 2^16-1).
The third one does not compile because we do an explicit typecast and
a long is 64 bytes and an integer is 32.
Why does the second statement compile?? A integer doesn't fit in a
short, just like a long does not fit in a integer (third statement).
It looks like if a implicit typecast takes place just like the fist
statement.
Thanks in advance.
Greetings,
Wybrand.