Java: does "9" appear in my startup Eclipse error?

When writing my code to schedule a computer in which we see the compatibility of an array of four objects, my code prints strangely. (Eclipse at first did not inform me of the error / warning).

code:

System.out.print("Fit between " + profile1.getTitle() + " and " + profile2.getTitle() + ": \n " + + '\t' + profile1.fitValue(profile2) + '\n'); 

When I tried to print, they looked like this:

Go between Elizabeth Bennett and Elizabeth Bennett: 90.0

Set between Elizabeth Bennett and Fitzwilliam Darcy: 90.55

Bout between Elizabeth Bennett and Romeo Montague: 90.3

Approach between Elizabeth Bennett and Juliet Capulet: 90.0

.................................................. ..... and so on.

In front of my numbers appeared "9".

I found the problem later: I had two concatenating operators ("+") after my "\ n". Therefore, I changed my code by deleting the extraneous "+".

New code:

 System.out.print("Fit between " + profile1.getTitle() + " and " + profile2.getTitle() + ": \n " + '\t' + profile1.fitValue(profile2) + '\n'); 

New correct start:

Go between Elizabeth Bennett and Elizabeth Bennett: 0.0

Set between Elizabeth Bennett and Fitzwilliam Darcy: 0.55

Bout between Elizabeth Bennett and Romeo Montague: 0.3

Go between Elizabeth Bennett and Juliet Capulet: 0.0

.............................................. and so on. d.

Although I gratefully found my problem, I want to know what exactly causes this. Is there any shorthand for the eclipse that leads to the appearance of "9"? Or is it due to some kind of eclipse error? I am still new to java, so I cannot draw a convincing conclusion.

I tried modifying my code a bit to see how Eclipse would react (I removed 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:

Several markers on this line

  • Syntax error in token "++", + Expected

  • Invalid argument for operation ++ / -

This makes sense because ++ itself is a kind operator. Interestingly, however, if I put a space between “++”, then the warning disappears.

I did not paste all my code for readability, but if necessary, I can edit and add it. Some clarification on this would be helpful. Thanks.

+6
source share
4 answers

The problem is not only + + in your code. But this is also due to the fact that you are using char '\t' , not String "\t" .

Therefore, when you use System.out.println("Hello" + '\t'); , \t is taken as a character literal and added to the string as \t .

But when you use System.out.println("Hello" + + '\t'); , it becomes effectively System.out.println("Hello" + (+ '\t')); . Thus, it is considered as int java. You assign only the + and - signs to int, not to characters.

Remember that you can do int i = +2 or -2 or + +2 .

And the value of ASCII \t is 9 . Therefore, it is added to the line and printed as Hello9 .

+5
source

No, this is not a mistake. This is unusual, but it is not a mistake.

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 start and end , but because + is left-associative, it doesn't make any difference here. (If start was an integer, the first binary operator + would be an integer addition in both cases.)

Basically, the extra + becomes the unary + operator with '\t' as the operand. Unary numeric promotion applies to char , giving int as a result. Thus, the code becomes efficient:

 System.out.println(start + +((int)'\t') + end); 

which is effective:

 System.out.println(start + +(9) + end); 

which is effective

 System.out.println(start + 9 + end); 

... at this moment he hopes to understand why you are getting the conclusion that you are.

To avoid accidentally terminating the processing of a character as an integer, it is best to just use strings - at this moment the code will not compile, because there is no unary + operator for String :

 System.out.println(start + +"\t" + end); 

... gives:

 Test.java:6: error: bad operand type String for unary operator '+' 
+6
source

You are using char \t , not the string "\t" with the + operator, so it uses ascii numeric value from 9 for the tab

So

 System.out.println("\n"+ +'\t'); 

coincides with

 System.out.println("\n"+ (+9)); 
+1
source

I do not think this is a problem with eclipse. It also works on the console. + '\ T' is considered an integer value of 9 in Java not as a character.

0
source

All Articles