• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple weird String assignment

 
Syed Tabrez
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I have some exisitng code that is assigning a String reference with the along the lines of the following assignment,
String a1 =+1+- - + - - + + 1+"";
which is undoubtedly not a nice way to code ......... but very surprisingly it compiles and runs fine!!!

If any one can please point out the reason why java compiler allows this without a warning even in eclipse 3.5.0 on java 1.5 will be grateful for the reply.

Thanks and regards,
Tabz
 
Rob Spoor
Sheriff
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's dissect this, using the fact that + is left associative if used as a binary operator, and both + and - are also unary operators.
+1+- - + - - + + 1+""
=== (+1 is the same as 1, applied twice)
1+- - + - - + 1+""
=== (+1 is the same as 1)
1+- - + - - 1+""
=== (- after - is +)
1+- - + 1+""
=== (+1 is the same as 1)
1+- - 1+""
=== (- after - is +)
1+1+""
=== (apply binary + from left to right)
2+""
=== (+ with at least one of the operands as String will do string concatenation
"2"

Please note that if you remove the spaces between the double - (making them --) and the double + (making them ++) it will fail, because those are different operators from + and -.
 
Syed Tabrez
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes it clear thanks Rob!!!
For some reason overlooked the fact that unary + or - indicates a positive or negative number ..... thank you for pointing this out.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Syed Tabrez wrote:That makes it clear thanks Rob!!!
For some reason overlooked the fact that unary + . . . indicates a positive . . . number ..... thank you for pointing this out.
Rob's good, isn't he.

Actually unary + doesn't make the number positive. I am not sure, but I think it makes it occupy more space in memory, but +(-1) is still equal to -1.
 
Rob Spoor
Sheriff
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the JLS:
The type of the operand expression of the unary + operator must be a primitive numeric type, or a compile-time error occurs. Unary numeric promotion (ยง5.6.1) is performed on the operand. The type of the unary plus expression is the promoted type of the operand. The result of the unary plus expression is not a variable, but a value, even if the result of the operand expression is a variable.

At run time, the value of the unary plus expression is the promoted value of the operand.

According to that section 5.6.1, the + basically just turns byte, short or char into int, nothing else.
 
Syed Tabrez
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the clarification.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote: . . . the + basically just turns byte, short or char into int, nothing else.
As I thought, makes it occupy more memory.
 
Rob Spoor
Sheriff
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless applied to an int, long, float or double
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic