Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does &amp do in Java?

Tags:

java

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.

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.

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);
}
like image 769
user202051 Avatar asked Mar 28 '26 18:03

user202051


2 Answers

You are copy pasting code from a badly encoded website. This &amp; 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: &amp;. That is what the website is showing you unfortunately.

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

like image 97
Martijn Courteaux Avatar answered Mar 30 '26 06:03

Martijn Courteaux


This looks like an encoding problem with the source file.

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

like image 45
bn. Avatar answered Mar 30 '26 07:03

bn.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!