• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Interesting contradiction...have a look!

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all!
Look at this interesting contradiction...
This compiles and runs:
int i=1;
char _c='c';
_c+=i;
System.out.println(_c);
This not:
int i=1;
char _c='c';
_c=_c+i;
System.out.println(_c);
But the result should be the same if we look at the definition of the operators!!?
What do you think? Where is the mistake?
Thanks...
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefano,
Welcome to Javaranch!
We'd like you to read the Javaranch Naming Policy and register again. Thank you.
[ February 08, 2002: Message edited by: Valentin Crettaz ]
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
compiler will translate _c+=i int _c = (char) _c + i;
So it will compile.
-SR
This compiles and runs:
int i=1;
char _c='c';
_c+=i;
System.out.println(_c);
 
Sheriff
Posts: 15983
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefano, sun ram:
Actually, the JLS 15.26.2 states that the compound assignment given is equivalent to:
_c = (char)(_c + i); // legal: result is a char
which is different from
_c = (char)c + i; // illegal: result is an int

Junilu
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by sun ram:
compiler will translate _c+=i int _c = (char) _c + i;


As Sun is saying, anytime you use the arithmetic extended assignment operators (+=, -=, *=, etc.), the compiler does an implicit cast back to the type of the variable you're assigning to. Look at these:

The implicit cast can be very important (as you already found). Look at lines 1 and 2 above. Since b + a and s - a return an int value, and we're trying to assign to a byte and a short, respectively, the cast is required. In line 3, the cast is unnecessary, but it is still performed by the compiler.
This is an important behavior of the arithmetic extended assignment opeators.
Corey
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the key idea for the exam would be that the compiler will internally give an explicit cast for the assignment operators, right?
Zac
 
Junilu Lacar
Sheriff
Posts: 15983
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Zac Roberts:
So the key idea for the exam would be that the compiler will internally give an explicit cast for the assignment operators, right?



I think I know what you mean, Zac, but it's important to use the correct terms to avoid confusing yourself and others:
"For compound assignment operators, the compiler will make an implicit cast to the type of the left-hand side operand."
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see this thread:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014737
and for goodness sakes, DON'T CROSS POST IN THE SAME FORUM!!!
 
Junilu Lacar
Sheriff
Posts: 15983
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, Rob, this thread came first so the other thread is the dup.
Valentin, do want to close the other one now?
Junilu
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu,
I realize that now, but I answered the other one first, so this thread was the duplicate for me
 
Stefan Koeltze
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob!...finally I got it ...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic