1

I hit some strange String concatenation syntax when writing some code and was surprised to find that it compiles.

quota.setText("Cloud storage: " + used + " (" + + + + quotaUsed / quotaAvailable * 100 + " of " + total);

The strange part is around the four continuous + operators (I was going to place more strings between them, and I was surprised there were no red squiggly lines underneath them. quotaUsed and quotaAvailable are longs and used and total are Strings.

Can anyone explain how the system will interpret that statement?

3
  • I think it might be applying the ++ operator once and the unary + operator once (making the value positive) and applying concatenation. I am not sure and would need to see output to know. Commented Nov 10, 2013 at 0:40
  • 1
    @blueblob - Incorrect guess. The ++ operator cannot have a space in the middle of it. Commented Nov 10, 2013 at 0:45
  • @blueblob unary + does not "make a value positive". If x is negative, +x is still negative. Math.abs(x) would make it positive. Commented Nov 10, 2013 at 0:54

1 Answer 1

4

The first + is going to be the concatenation operator, and the next three will simply be the unary + operator, basically no-ops in this situation. Note that you will be performing integer division on your fraction, so if you write it as

(100 * quotaUsed) / quotaAvailable

you'll get better precision.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I have heard of but never used the unary plus operator before. This SO answer has a good example: stackoverflow.com/a/2624541/473637

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.