Type cast problem.

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)-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.
 
J

Joona I Palaste

wybrand said:
Hai fellow programmers.
Can you please give me an answer to the following??

What's wrong with the answers already given on this newsgroup?

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"When a man talks dirty to a woman, that's sexual harassment. When a woman talks
dirty to a man, that's 14.99 per minute + local telephone charges!"
- Ruben Stiller
 
C

Chris Smith

(e-mail address removed) says...
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)-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.

The second statement is identical to the first, since the literal '65'
is already an int. The same rules for assignment conversion apply, and
so it's no less likely to compile that the first statement. These
assignment conversion rules don't apply to 'long' variables, so the
third statement fails to compile. This is in JLS section 5.2, which you
are free to read.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Cast problem. 4
EJB Bindings - Class Cast Exception 0
cast musings 19
type cast problem 5
cleaner alternative for cast 111
cast question 4
Is a function argument an implicit cast? 9
Minimum Total Difficulty 0

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,225
Latest member
Top Crypto Podcasts

Latest Threads

Top