• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Casting question

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Corey for your help on this matter.
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic