aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting question" Watch "Casting question" New topic
Author

Casting question

Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
From JQ+ # 952739436260
Given:
byte b = 1;
char c = 1;
short s = 1;
int i = 1;
Which of these are valid?
s = b * b;
i = b << b;
s << = b;
c = c + b;
s + = i;
and WHY??


Rob Petterson
SCJP
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Let's follow that question with a question? Which ones do you think are valid, and why? I'll be happy to correct you, but take a minute or two and think about it first. Most likely, you already have, but I'd like to see what your thought process is first.
Corey


SCJP Tipline, etc.
Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
The only one that I think is right would be i=b<<b
The reason being that byte b is promoted to ints which assigns without a cast req to i.
The rest of the assignments would all be promoted to int's and would therefore require a cast in order to be assigned to short and char.
[ June 03, 2002: Message edited by: Rob Petterson ]
Swati Gupta
Ranch Hand

Joined: May 28, 2002
Posts: 106
I think the answer should be
i = b << b;
s << = b;
s + = i;
first is as it is being assigned to int.
rest two are extended assignment which does the casting implicitly.
Is this is right?
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
I think except s = b* b and c = c + b, everythingelse is correct. These two explicit cast to int, char respectivly. In first Assingment (i = b << b), both b will be cast to int before the operation << takes place. In third assignment, both b and s are converted to int before the operation << i.e s = (short) (s << b) Then an implicit cast to short will take place. The same for last assignment too.


Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
Thanks Swati and Thiru.
So what you are saying is that when you have extended assignment, the cast is implicitly applied.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
There are, indeed, 3 correct answers. In order to come to this conclusion, you could have simply compiled this code to see which lines caused errors.
In short, the following lines are incorrect:

Line 1 is incorrect because the result is promoted to an int, which is unassignable (without a cast) to a short.
Line 2 is incorrect for the same reason. The result is promoted to an int, which is unassignable to a char.

The following lines are legal:

In line 1, the result is promoted to an int, but, as you're assigning to an int, this is legal.
In lines 2 and 3, compund assignment operators contain an implicit cast to the type of the operand. You can read more about it in the JLS, §15.26.2 Compound Assignment Operators. Therefore, these lines are also legal.
I hope that helps clear this up for you,
Corey
Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
Thanks Corey for your help on this matter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting question