-1

We had an odd thing in our logging happen today. Printed is a list of integers and longs separated by commas, like the following code:

public class Main {

    public static void main(String[] args) throws InterruptedException {
        long l = 10;
        System.out.println(l + ';' + "text");
    }
}

The problem was that the ; disappeared from the output.

Rovanion
  • 4,382
  • 3
  • 29
  • 49
  • 1
    Was the disappearing `;` the *only* problem, or was the number also something other than `10`? The answer mentions a different number, but the question doesn't. It's fine to post questions and answer them yourself (after all, you know what the solution is), but make sure that the question is still high-quality and describes the entire problem. Including output here would be a good thing to do. – Joshua Taylor May 14 '14 at 16:00
  • Make the `';'` be `";"` instead. – Hot Licks May 14 '14 at 16:03
  • @JoshuaTaylor Yes the number was 69 instead of 10. I just left this Q&A style SO-post so that it could help someone else in the future. – Rovanion May 15 '14 at 10:02

1 Answers1

0

The problem here is caused by the overload of the + operator. It acts in one way when operating on a String and a long, and another when operating on a char and a long. When one of the operands is a string it will try to cast the other operand to a string if it's not already one and then concatenate the two.

But when the operators are numbers, like int and long, the + operator acts as the normal mathematical plus operator. And since char is a number and nothing else, ';' + l is treated as a numerical operation and thus the output of the code in the question is 69text and not 10;text.

Rovanion
  • 4,382
  • 3
  • 29
  • 49