I'm having trouble in figuring out why the output for theses two lines is different ..

public static void main(String[] args) {

The output

closed as off-topic by amon, gnat, Jörg W Mittag, Thomas Owens Oct 10 '16 at 22:45

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for assistance in writing or debugging existing code are off-topic on Programmers. These questions can be asked on Stack Overflow if they include the desired behavior, a specific problem or error, and the shortest code necessary to reproduce it in the question itself. See How To Create a Minimal, Complete, and Verifiable Example." – gnat, Jörg W Mittag, Thomas Owens
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    + is left associative, so "6.0+1="+6.0 is the string "6.0+1=6.0" and the +1 concatenates "1". – CodesInChaos Oct 8 '16 at 14:21

Your example don't use unary plus.

You use it for string concatenation, and for addition.

"6.0+1="+6.0+1 will be evaluated in types as string + number + number. So first "6.0+1" + 6.0 will use + as the string concatenation operator, resulting in a new string: "6.0+1=6.0". Then the same happens with the + 1 at the end, resulting in another string concatenation.

"6.0+1="+(6.0+1) wil first evaluate the (...) part, in which you add two numbers, so the result will be 7.0 (floating point + integer => floating type). Then the string concatenation as in the first variation takes place.

Unary operators would work on a single variable/constant/expression, i.e. the unary minus operator as in x = -1.


The answers would definitely be different because of the use of Bracket on the second operation,the operation would first start from bracket to other operators.

Not the answer you're looking for? Browse other questions tagged or ask your own question.