本文介绍了Java - 逗号运算符,用于循环声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(int i = 1,j = 15; j>< p> 10; i ++,j--){
//做一些整洁的
}





  int j = 2,k = 4; 
int x;
//带逗号运算符的赋值语句
x = j + 1,k;

来源:





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

来源:



这对于代码迷惑竞赛来说是一个很好的窍门,或者让我的同事迷惑不解,但是这两个例子都不会编译(Java 1.6,Eclipse Juno),错误是赋值的左边必须是变量。我试着看编译器设置,看是否可以禁止不良代码,但没有运气。



有什么错?逗号运算符是以后更改的旧规范的一部分吗?是谁写的这些例子使用不同的Java设置,允许这个人吗?
解决方案

一些在C中工作的技巧在Java中不起作用。

在Java AFAIK中。

假设Java就像C或C ++一样是一个常见的错误,因为它是相似的。在SO上编码错误的很大一部分是由于人们试图用Java编写C ++,当它不能达到他们期望的时候感到困惑。

BTW:犯了同样的错误,假设C ++就像Java一样,因为我的C ++知识并不是最新的。






Java,但可能不是C。



您可以使用所有货币符号,或者有几个看起来相同的货币符号。



例如

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

您可以使用不可见和c对的字符,这些字符在打印时会颠倒其余行的顺序。 ;)



这个程序编译并运行并打印所有可以在Java标识符中使用的奇数字符



<$ p $ (charch= 0;ch< Character.MAX_VALUE;ch++)
if(Character.isJavaIdentifierPart(ch)& &!Character.isJavaIdentifierStart(ch))
System.out.printf(%04x<%s>%n,(int)ch,+ch );

这使得它变得简单。






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?

解决方案

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

This never worked in Java AFAIK.

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

这篇关于Java - 逗号运算符,用于循环声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 00:34