& 在 Java 中有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20763857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does & do in Java?
提问by user202051
Im looking over a piece of Java code I didn't write and noticed that it included & in it in a few places. Its a piece of code from a infix to postfix notation converter.
我查看了一段我没有编写的 Java 代码,并注意到它在几个地方包含 & 。它是一段从中缀到后缀符号转换器的代码。
If I put this piece of code in Eclipse it dosn't like these &s and creates errors for them, the error being & cannot be resolved as a variable
.
如果我将这段代码放在 Eclipse 中,它就不喜欢这些 &s 并为它们创建错误,错误是& cannot be resolved as a variable
.
Heres the code
这是代码
public static String[] infixToRPN(String[] inputTokens) {
ArrayList<String> out = new ArrayList<String>();
Stack<String> stack = new Stack<String>();
// For all the input tokens [S1] read the next token [S2]
for (String token : inputTokens) {
if (isOperator(token)) {
// If token is an operator (x) [S3]
while (!stack.empty() && isOperator(stack.peek())) {
// [S4]
if ((isAssociative(token, LEFT_ASSOC) && cmpPrecedence(
token, stack.peek()) <= 0)
|| (isAssociative(token, RIGHT_ASSOC) && cmpPrecedence(
token, stack.peek()) < 0)) {
out.add(stack.pop()); // [S5] [S6]
continue;
}
break;
}
// Push the new operator on the stack [S7]
stack.push(token);
} else if (token.equals("(")) {
stack.push(token); // [S8]
} else if (token.equals(")")) {
// [S9]
while (!stack.empty() && !stack.peek().equals("(")) {
out.add(stack.pop()); // [S10]
}
stack.pop(); // [S11]
} else {
out.add(token); // [S12]
}
}
while (!stack.empty()) {
out.add(stack.pop()); // [S13]
}
String[] output = new String[out.size()];
return out.toArray(output);
}
采纳答案by Martijn Courteaux
You are copy pasting code from a badly encoded website. This &
should be replaced with an ampersand (&
).
您正在从错误编码的网站复制粘贴代码。这&
应该替换为与号 ( &
)。
In HTML, you cannot simply write &
, because that is an escape character. So, in history, they invented the escape sequence for ampersands, which is: &
. That is what the website is showing you unfortunately.
在 HTML 中,您不能简单地编写&
,因为这是一个转义字符。所以,在历史上,他们发明了&符号的转义序列,即:&
。不幸的是,这就是网站向您展示的内容。
So, to answer the question: &&
should be: &&
. That is the logical AND operator.
所以,要回答这个问题:&&
应该是:&&
。那就是逻辑 AND 运算符。
回答by Palpatim
That looks like a mis-encoding of a Java file. I'm only aware of the &&
operator, which is logical AND.
这看起来像是对 Java 文件的错误编码。我只知道&&
运算符,这是逻辑 AND。
回答by BobTheBuilder
&
is the HTML escaped version of &
.
&
是 的 HTML 转义版本&
。
You should replace all &
with &
你应该&
用&
回答by Anton Kovalenko
There is no & in java, it's obviously a result of copy-paste from an HTML page. Replace & with & to fix it.
没有 & 在 java 中,这显然是从 HTML 页面复制粘贴的结果。替换 & 用 & 来修复它。
回答by bn.
This looks like an encoding problem with the source file.
这看起来像是源文件的编码问题。
&
is the ampersand character from the ISO-8859-1character set.
&
是ISO-8859-1字符集中的 & 字符。
回答by Mena
In Java you have:
在 Java 中,你有:
- boolean AND -->
&&
- bitwise AND -->
&
- 布尔值 AND -->
&&
- 按位与 -->
&
Your issue is that your code contains XML entities that haven't been successfully decoded, so &
means &
, just as >
would mean >
.
您的问题是您的代码包含尚未成功解码的 XML 实体,因此&
意味着&
,就像>
意味着>
.