简体   繁体   中英

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. At the bottom of my code below, there is an OR operator, denoted by ||, in the middle of a return statement. 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? 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 .

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() ?). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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