8

I know I can use the comma operator like this

for (int i = 1, j = 15; j>10; i++, j--) {
    // do something neat
}

but some articles seem to suggest that the comma operator can be used outside of the for loop declaration, for example

int j = 2, k = 4 ;
int x ;
// Assignment statement with comma operator
x = j + 1, k ;

source: http://www.cs.umd.edu/~clin/MoreJava/ControlFlow/comma.html

or

int x = (expression) ? (i++,2) : 3;

source: https://stackoverflow.com/a/12047433/1084813

This would be a neat trick for a code obfuscation contest or to confuse my colleagues, but neither of the examples will compile (Java 1.6, Eclipse Juno), the error is "The left-hand side of an assignment must be a variable". I tried looking at the compiler settings to see whether it could be forbidden to prevent bad code, but without luck.

What's wrong? Was the comma operator a part of an older specification which later changed? Are the people that wrote those examples using a different Java setup that allows this?

Community
  • 1
  • 1
JohnEye
  • 6,436
  • 4
  • 41
  • 67
  • 10
    The JLS says: [Unlike C and C++, the Java programming language has no comma operator.](http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.27) – Jesper Sep 26 '12 at 12:24
  • 8
    "or to confuse my colleagues". Most people try to make their code MORE readable, not less. – Shawn D. Sep 26 '12 at 12:26
  • Its incorrect syntax, also why would you want to confuse your collegues? The code will become legacy soon enough without extra obfuscation efforts. – dngfng Sep 26 '12 at 12:26
  • Confuse only for fun, of course, not to be used in serious code. The compiler says that "The left-hand side of an assignment must be a variable" – JohnEye Sep 26 '12 at 12:30
  • Don't do that, the readability is messed up. – Foredoomed Sep 26 '12 at 12:36
  • Can we introduce a "confusion" tag, please? – f_puras Sep 26 '12 at 12:38
  • @Jesper: I know about this, but both examples are from places related to Java, are you suggesting that both authors were just C programmers in disguise? – JohnEye Sep 26 '12 at 12:54
  • @JohnEye As you noticed the Java compiler doesn't accept it, so it isn't valid Java code. Probably the people who wrote that mistakenly thought that the comma operator was another C feature which Java also has. – Jesper Sep 26 '12 at 13:00
  • @Jesper: I would be happy to accept that as likely, but the fact that it is taken from a university lecture materials won't let me. Surely some student would notice that this isn't a valid code :-/ – JohnEye Sep 26 '12 at 13:07
  • @JohnEye I understand but the Java Language Specification is the official source, so if that says there's no comma operator, then I tend to believe that the university lecture is wrong. – Jesper Sep 26 '12 at 13:15
  • @JohnEye It doesn't matter where you got it from, or why the authors were wrong, unless it was a normative reference. In this case the only normative reference is the JLS, and it says otherwise. – user207421 Dec 10 '13 at 05:52
  • @JohnEye, the second source you've pointed (on SO) doesn't exist anymore :( –  Jun 28 '16 at 15:23
  • @Ehsan: The answer was probably downvoted out of existence because it was wrong. Using a comma like this is simply not correct and will not work. – JohnEye Jul 08 '16 at 11:04

2 Answers2

13

https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.27

15.27. Expression

<...>

Unlike C and C++, the Java programming language has no comma operator.

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
  • Java has comma separator. [Source : JLS](http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.11) – Quazi Irfan Jan 08 '15 at 21:20
  • 4
    @iamcreasy java doesnt have a comma operator its only a seperator. In your source its given that its a seperator and not an operator. There's a difference. – Ilaya Raja S Dec 13 '15 at 10:31
8

What's wrong?

Some of the tricks which work in C don't work in Java.

Was the comma operator a part of an older specification which later changed?

This never worked in Java AFAIK.

Are the people that wrote those examples using a different Java setup that allows this?

Its a common mistake to assume Java is just like C or C++ because it is similar. A good portion of coding mistakes on SO are due to people trying to write C++ in Java and getting confused when it doesn't do what they expect.

BTW: I have made the same mistake assuming C++ is just like Java as my knowledge of C++ is not current.


However some tricks which working Java but perhaps not C.

You can use all currency symbols or which there are a few which look at most the same.

e.g.

if( ⁀ ‿ ⁀ == ⁀ ⁔ ⁀ || ¢ + ¢== ₡)

You can use character which are invisible and c couple which reverse the order the rest of the line when printed. ;)

This program compiles and runs and prints all the odd characters you can use in Java identifiers

for (char c‮h = 0; c‮h < Character.MAX_VALUE; c‮h++)
    if (Character.isJavaIdentifierPart(c‮h) && !Character.isJavaIdentifierStart(c‮h))
        System.out.printf("%04x <%s>%n", (int) c‮h, "" + c‮h);

which makes its almost too easy.

http://vanillajava.blogspot.co.uk/2012/08/uses-for-special-characters-in-java-code.html

http://vanillajava.blogspot.co.uk/2012/09/hidden-code.html

Chris Huang-Leaver
  • 6,059
  • 6
  • 41
  • 67
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130