File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Simple weird String assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple weird String assignment" Watch "Simple weird String assignment" New topic
Author

Simple weird String assignment

Syed Tabrez
Greenhorn

Joined: Dec 22, 2009
Posts: 9
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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 -.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Syed Tabrez
Greenhorn

Joined: Dec 22, 2009
Posts: 9
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

Joined: Oct 13, 2005
Posts: 39393
    
  28
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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

Joined: Dec 22, 2009
Posts: 9
Thank you for the clarification.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

Unless applied to an int, long, float or double
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple weird String assignment