& 在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:10:15  来源:igfitidea点击:

What does &amp do in Java?

java

提问by user202051

Im looking over a piece of Java code I didn't write and noticed that it included &amp 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 &amps and creates errors for them, the error being &amp cannot be resolved as a variable.

如果我将这段代码放在 Eclipse 中,它就不喜欢这些 &s 并为它们创建错误,错误是&amp 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() &amp;&amp; isOperator(stack.peek())) {
                // [S4]
                if ((isAssociative(token, LEFT_ASSOC) &amp;&amp; cmpPrecedence(
                        token, stack.peek()) <= 0)
                        || (isAssociative(token, RIGHT_ASSOC) &amp;&amp; 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() &amp;&amp; !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 &amp;should be replaced with an ampersand (&).

您正在从错误编码的网站复制粘贴代码。这&amp;应该替换为与号 ( &)。

In HTML, you cannot simply write &, because that is an escape character. So, in history, they invented the escape sequence for ampersands, which is: &amp;. That is what the website is showing you unfortunately.

在 HTML 中,您不能简单地编写&,因为这是一个转义字符。所以,在历史上,他们发明了&符号的转义序列,即:&amp;。不幸的是,这就是网站向您展示的内容。

So, to answer the question: &amp;&amp;should be: &&. That is the logical AND operator.

所以,要回答这个问题:&amp;&amp;应该是:&&。那就是逻辑 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

&amp;is the HTML escaped version of &.

&amp;是 的 HTML 转义版本&

You should replace all &amp;with &

你应该&amp;&

回答by Anton Kovalenko

There is no &amp; in java, it's obviously a result of copy-paste from an HTML page. Replace &amp; with & to fix it.

没有 & 在 java 中,这显然是从 HTML 页面复制粘贴的结果。替换 & 用 & 来修复它。

回答by bn.

This looks like an encoding problem with the source file.

这看起来像是源文件的编码问题。

&amp;is the ampersand character from the ISO-8859-1character set.

&amp;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 &amp;means &, just as &gt;would mean >.

您的问题是您的代码包含尚未成功解码的 XML 实体,因此&amp;意味着&,就像&gt;意味着>.