Java - comma operator outside for loop declaration

元气小坏坏 提交于 2019-11-30 19:47:34
Peter Lawrey

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!