Java - for 循环声明之外的逗号运算符 [英] Java - comma operator outside for loop declaration
问题描述
我知道我可以像这样使用逗号运算符
I know I can use the comma operator like this
for (int i = 1, j = 15; j>10; i++, j--) {
// do something neat
}
但有些文章似乎建议逗号运算符可以在for循环声明之外使用,例如
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 ;
来源:http://www.cs.umd.edu/~clin/MoreJava/ControlFlow/comma.html
或
int x = (expression) ? (i++,2) : 3;
来源:https://stackoverflow.com/a/12047433/1084813
这对于代码混淆竞赛或混淆我的同事来说是一个巧妙的技巧,但两个示例都无法编译(Java 1.6,Eclipse Juno),错误是赋值的左侧必须是一个多变的".我尝试查看编译器设置,看看是否可以禁止以防止不良代码,但没有运气.
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.
怎么了?逗号运算符是否是后来更改的旧规范的一部分?编写这些示例的人是否使用允许这样做的不同 Java 设置?
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?
推荐答案
怎么了?
某些在 C 中有效的技巧在 Java 中无效.
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?
这在 Java AFAIK 中从未奏效.
This never worked in Java AFAIK.
编写这些示例的人是否使用允许这样做的不同 Java 设置?
Are the people that wrote those examples using a different Java setup that allows this?
认为 Java 就像 C 或 C++ 是一个常见的错误,因为它们很相似.SO 上的很大一部分编码错误是由于人们试图用 Java 编写 C++ 并且在它没有达到他们的预期时感到困惑.
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.
顺便说一句:我犯了同样的错误,认为 C++ 就像 Java,因为我对 C++ 的了解不是最新的.
BTW: I have made the same mistake assuming C++ is just like Java as my knowledge of C++ is not current.
但是有些技巧可以在 Java 中运行,但可能不能在 C 中运行.
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.
例如
if( ⁀ ‿ ⁀ == ⁀ ⁔ ⁀ || ¢ + ¢== ₡)
您可以使用不可见的字符和 c 对,它们在打印时颠倒行的其余部分的顺序.;)
You can use character which are invisible and c couple which reverse the order the rest of the line when printed. ;)
这个程序编译、运行并打印所有可以在 Java 标识符中使用的奇数字符
This program compiles and runs and prints all the odd characters you can use in Java identifiers
for (char ch = 0; ch < Character.MAX_VALUE; ch++)
if (Character.isJavaIdentifierPart(ch) && !Character.isJavaIdentifierStart(ch))
System.out.printf("%04x <%s>%n", (int) ch, "" + ch);
这让它几乎太容易了.
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
这篇关于Java - for 循环声明之外的逗号运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!