• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting question

 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Corey for your help on this matter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic