简体   繁体   English

|| 是怎么做的运算符在 Java 中的 return 语句中工作?

[英]How does the || operator work in a return statement in Java?

I am looking at a Java program in which a text file containing integers is read into a simplified LinkedList, and then all the possible subsets of the list are considered.我正在查看一个 Java 程序,其中将包含整数的文本文件读入简化的 LinkedList,然后考虑列表的所有可能子集。 At the bottom of my code below, there is an OR operator, denoted by ||, in the middle of a return statement.在我下面代码的底部,有一个 OR 运算符,用 || 表示,位于 return 语句的中间。 How does the compiler decide whether to express the left side or the right side of the ||编译器如何决定表达 || 的左边还是右边operator?操作员? I debugged the program and stepped through it line by line, and I noticed that both sides are expressed, recursively.我调试了程序并一行一行地遍历它,我注意到两边都是递归的。 How can both parts of an or statement be expressed? or 语句的两个部分如何表达? I'm new to this, so I find it confusing.我是新手,所以我觉得很困惑。 Any help would be appreciated.任何帮助,将不胜感激。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Target {
/*--------------LINKED LIST------------------*/
    private Node head;
    private Node tail;

    public void add(int obj) {
        if (head == null) {
           head = new Node(obj);
           tail = head;
        } else {
           tail.next = new Node(obj);
           tail = tail.next;
        }
    }
    class Node {
        public Node next;
        public int item;

        public Node(int item) {
            this.item = item;
        }
    }
/*--------------LINKED LIST------------------*/

    public Target() {
        head = null;
    }

    public static void main(String args[]) throws FileNotFoundException {
        Scanner fileIn = new Scanner(new File("in.txt"));
        Target list = new Target();

        while (fileIn.hasNext()) {
            list.add(fileIn.nextInt()); //Read the file into the linkedlist.
        }
        fileIn.close();

        System.out.println(list.recursiveSolution(list, 0));
    }

    public boolean recursiveSolution(Target list, int target) {
        return subsetSumXOR(list.head, 0, 0, target);
    }


    private boolean subsetSumXOR(Node node, long sumOne, long sumTwo, int target) {
        if (node == null) {
            return (sumOne ^ sumTwo) == target;  //base case, when node is null we've reached the end of the list.
        }

        return subsetSumXOR(node.next, sumOne + node.item, sumTwo, target) || subsetSumXOR(node.next, sumOne, sumTwo + node.item, target);
    }

}

If subsetSumXOR(node.next, sumOne + node.item, sumTwo, target) is true, then it returns true .如果subsetSumXOR(node.next, sumOne + node.item, sumTwo, target)为真,则返回true

Otherwise, the expression at the right of ||否则, ||右边的表达式is evaluated and its value is returned.被评估并返回其值。


Learn more about Short Circuit Operators了解有关短路运算符的更多信息

The left side is evaluated first, if true, then the right hand side is not evaluated.首先评估左侧,如果为真,则不评估右侧。 This is call shortcutting (the statement (true||expensiveFunction()) can never evaluate to false, so why bother with the expensiveFunction() ?).这是调用快捷方式(该语句(true||expensiveFunction())永远不会评估为 false,那么为什么要费心使用expensiveFunction()呢?)。 You could see this with an input that does not require any recursion what so ever.您可以通过不需要任何递归的输入来看到这一点。

What you are likely seeing is the left side evaluating to false in which case yes, it will evaluate the right hand side.您可能看到的是左侧评估为false在这种情况下,是的,它将评估右侧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM