3

Few days ago, I made a typo in java code but it compiled and worked well. (though the result is strange.)

My code is:

public  static  void    main(String args[])  {
    String  strOut;
    char    cSEP = '|';
    String  sSEP = "|";

    strOut = "AA" + cSEP + "BB";    // Correct assignment
    System.out.println(strOut);     // The result is "AA|BB". This is OK.

    strOut = "AA" + + cSEP + "BB";  // No Error : no token between two +
    System.out.println(strOut);     // The result is "AA124BB"

    strOut = "AA" + + sSEP + "BB";  // This is compiler error !!!
    System.out.println(strOut);
}

I cannot find why the 2nd assignment is not error and 124 is printed. (Of course, '|' is 124 in ASCII code. But why "124", not "|" ?)

Is this compiler bug? Or correct java syntax that I did not know yet ?

user727062
  • 127
  • 2
  • 9

3 Answers3

10

The difference between a String and a char is significant. some numeric operators, when applied on a char, turns the char into an int (this is called unary numeric promotion). On the other hand, only the binary + operator is defined for Strings.

In the second and third line of your code, the expression is parsed like this:

strOut = "AA" + (+ cSEP) + "BB";

The unary + operator, when applied on a char, turns the whole expression into an int through unary numeric promotion. The value is equal to the encoded value of the character. So the expression becomes:

strOut = "AA" + 124 + "BB";

which is valid.

But if cSEP were to be replaced with sSEP:

strOut = "AA" + (+ sSEP) + "BB";

The Java compiler doesn't know what + sSEP means. The + unary operator is not defined for String!

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Sweeper
  • 213,210
  • 22
  • 193
  • 313
4

This is normal Java (expression) syntax. In the expression:

"AA" + + cSep

the second + is the unary + operator which applies to (only) numeric types. The char type is a numeric type in Java.

Note that unary + has a higher precedence than binary + so the above is equivalent to:

"AA" + ( + cSep )

But the expression:

"AA" + + sSep

is illegal because the unary + operator is not defined for String.


But what does "AA" + + cSep actually do?

Well the unary + operator performs a unary numeric promotion which converts the char to an int. In context, it is equivalent to:

"AA" + ((int) cSep)

which is in turn equivalent to:

"AA" + Integer.toString((int) cSep)

That means that there will be a decimal number following the AA rather than a pipe character.

References:

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

It is correct java syntax. In java if we apply + unary operator on a character then it convert that character into it's unicode value. like +'a' then it will convert it into 97 because unicode value of character 'a' is 97.

so in your case strOut = "AA" + + cSEP + "BB"; = "AA" + + '|' + "BB";

+'|' so this '|' will be converted into it's unicode value and unicode value of '|' is 124.

on String we cann't apply unary + operator so in this case strOut = "AA" + + sSEP + "BB"; you are getting error