While writing my code for a computer dating assignment in which we see the compatibility of an array of four objects, my code printed strangely. (Eclipse didn't give me an error/warning at first).
Code:
System.out.print("Fit between " + profile1.getTitle() + " and " + profile2.getTitle() + ": \n " +
+ '\t' + profile1.fitValue(profile2) + '\n');
When I tried to print things out, they appeared like this:
Fit between Elizabeth Bennett and Elizabeth Bennett: 90.0
Fit between Elizabeth Bennett and Fitzwilliam Darcy: 90.55
Fit between Elizabeth Bennett and Romeo Montague: 90.3
Fit between Elizabeth Bennett and Juliet Capulet: 90.0
.......................................................so on so forth.
There was a "9" appearing in front of my numbers.
I found the problem later: I had two concatenating operators ("+") after my "\n". So I changed my code, deleting the extraneous "+".
New code:
System.out.print("Fit between " + profile1.getTitle() + " and " + profile2.getTitle() + ": \n " +
'\t' + profile1.fitValue(profile2) + '\n');
New correct run:
Fit between Elizabeth Bennett and Elizabeth Bennett: 0.0
Fit between Elizabeth Bennett and Fitzwilliam Darcy: 0.55
Fit between Elizabeth Bennett and Romeo Montague: 0.3
Fit between Elizabeth Bennett and Juliet Capulet: 0.0
..............................................so on so forth.
Though I've thankfully found my problem, I want to know what exactly is causing this to happen. Is there some sort of eclipse shorthand that leads to the "9"'s appearance? Or is this due to some eclipse bug? I'm still a beginner at java, so I can't make a solid conclusion.
I tried modifying my code slightly to see how Eclipse would react (I deleted the space between my original "+ +"):
System.out.print("Fit between " + profile1.getTitle() + " and " + profile2.getTitle() + ": \n " ++
'\t' + profile1.fitValue(profile2) + '\n');
Eclipse gave me this warning:
Multiple markers at this line
Syntax error on token "++", + expected
Invalid argument to operation ++/--
This makes sense, because ++ on its own is an operator of sorts. Interestingly, however, if I put a space between the "++", then the warning disappears.
I didn't paste my entire code for the sake of readability, but if necessary, I can edit and add it in. Some clarification regarding this would be helpful. Thank you.
No, it's not a bug. It's unusual, but it's not a bug.
The first thing to do is simplify the example massively:
public class Test {
public static void main(String[] args) {
String start = "Start";
String end = "End";
System.out.println(start + '\t' + end);
System.out.println(start + +'\t' + end);
}
}
Result:
Start End
Start9End
Now admittedly I'm using strings for both start
and end
, but due to +
being left-associative, that doesn't make any difference here. (If start
were an integer, the first binary +
operator would be integer addition in both cases.)
Basically, the extra +
ends up being a unary +
operator with '\t'
as the operand. Unary numeric promotion is applied to the char
, giving int
as a result. So the code becomes effectively:
System.out.println(start + +((int)'\t') + end);
which is effectively:
System.out.println(start + +(9) + end);
which is effectively
System.out.println(start + 9 + end);
... at which point it's hopefully clear why you're getting the output you are.
To avoid accidentally ending up treating a character as an integer, it's probably best to just use strings instead - at which point the code won't compile because there's no unary +
operator for String
:
System.out.println(start + +"\t" + end);
... gives:
Test.java:6: error: bad operand type String for unary operator '+'
The problem is not just with the + +
in your code. But it's also with the fact that you are using char '\t'
and not String "\t"
.
So when you use System.out.println("Hello" + '\t');
, the \t
is taken as a character literal and appended to the string as \t
.
But when you use System.out.println("Hello" + + '\t');
, it becomes effectively System.out.println("Hello" + (+ '\t'));
. So it is treated as an int
by java. You only assign +
and -
signs to ints, not chars.
Remember you can do int i = +2
or -2
or + +2
.
And the ASCII value of \t
is 9
. Hence it gets appended to the string and get's printed as Hello9
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With