This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Confused with primitive casting

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does:
char c = "a";
int i = 1;
c += i; // compiles okay
BUT
c = c + i; // does not compile?
Can you please explain.
Thanks
[This message has been edited by Ali (edited May 16, 2000).]
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ali,
Firstly, I will bet my three months' salary, the code you have given,
<PRE>char c = "a";</PRE>
will not compile because "a" is a String, not a character. I guess it was a typo
Having said that, here is why += compiles. Basically all the shorthand assignment operators +=, -=, *= , %= etc etc do not need explicit casting. Though often times it is a coding convenience, you will risk not detecting overflows here. You gotta be careful.
Hope this helps.
Ajith
 
Ali
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajith,
Many thanks for your reply.
I'm sorry, the "a" was a silly typo!
"Basically all the shorthand assignment operators +=, -=, *= , %= etc etc do not need explicit casting."
Can you please point me to where I could get more info on the above statement.
Regards
 
Ali
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I've got it. I have looked in the JLS and it says under section 15.25.2 Compound Assignment Operators:
"the result of the binary operation is converted to the type of the left-hand variable and the result of the conversion is stored into the variable."
And it gives this example:
short x = 3;
x += 4.6;
and results in x having the value 7 because it is equivalent to:
short x = 3;
x = (short)(x + 4.6);
so, coming back to my earlier problem:
char c = 'a';
int i = 1;
c += i;
is equivalent to c = (char) ( c + i );
Thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic