script_before_java
script_before_java

Reputation: 87

How is the return statement working in the following java method?

is it possible by any means in the following method that the print statement get executed after the if statement returns true in the for loop?

public boolean contains(Object o) {
    if(o == null){
        throw new IllegalArgumentException();
    }

    for(int i = 0; i < size(); i++){
        if(o.equals(getNodeAt(i).data)){
            System.out.println("contains passed here: "+o+" "+getNodeAt(i)+" "+i);
            return true;
        }
        System.out.println(getNodeAt(1));
    }
    System.out.println("cointain failed here "+o);
    return false;
}

Upvotes: 1

Views: 83

Answers (5)

ChiefTwoPencils
ChiefTwoPencils

Reputation: 13930

Of course; call the method again. More effectively, efficiently, and specifically with an Object such that o.equals(getNodeAt(i).data is false. The truth is...

"[B]y any means" is a pretty loose constraint; you say...

is it possible by any means in the following method that the print statement get[s] executed after the if statement returns true in the for loop?

I'm saying that YES, that's possible by any means when the means are recalling the method. In fact, it's perpetually true as long as you're using whatever container.


Proof:

Assume that it is impossible by any means in the following method that the second return statement gets executed after the if statement returns true in the for loop.

static String proof(Object o) {
        for(int i = 0; i < 1; ++i) {
            if (o == null) {
                return "I'm returning from the for loop!!!";
            }
        }
        return "I'm now called after the for's return statement (by any means)!! - QED";
}

But given...

public static void main(String...args) {
        System.out.println(proof(null));
        System.out.println(proof(new String("Hello Proof!")));
}// end main method

the ouput is...

I'm returning from the for loop!!!
I'm now called after the for's return statement!! - QED

Therefore our assumption is wrong and it is possible by some means for the second return statement to get executed after the if statement returns true in the for loop.

;)


A "better" way to phrase that so it's clear what you're asking would be, perhaps, - "Is it possible for the code in a method body to continue to execute after a return statement?"

That answer is no and can be tested in any good IDE as follows.

static String proof(Object o) {
        for(;;)
            if(true)
                return "Donkey Butts";
        return "Poops";
}

This basically says forever it is true that I will return "Donkey Butts". In any IDE I'd waste my time using you will get an error for "unreachable statement". The IDE can determine this truth from your code which implicitly is telling you that any time the loop is active and the if is true the code below cannot execute.

Upvotes: 1

lodo
lodo

Reputation: 2383

Nothing inside a method can be executed after the return statement.

But when you deal with output operations, things can happen quite differently from what you might expect. In fact, writes to an output file/device are often buffered, i.e. written to an internal array. When the array is full, it is sent to the file/device. This happens for efficiency reasons, because writing a few big chunks of data is faster than writing lots of small ones.

This means that these operations sometimes seem to happen long after the place where they appear in the code.

Upvotes: 1

shazin
shazin

Reputation: 21883

Yes, if you enclose in a try and finally.

public boolean contains(Object o) {
    if(o == null){
        throw new IllegalArgumentException();
    }

    for(int i = 0; i < size(); i++){
        try {
            if(o.equals(getNodeAt(i).data)){
                System.out.println("contains passed here: "+o+" "+getNodeAt(i)+" "+i);
                return true;
            }
        } finally {
            System.out.println(getNodeAt(1));
        }
    }
    System.out.println("cointain failed here "+o);
    return false;
}

Upvotes: 1

Andy Turner
Andy Turner

Reputation: 140309

No, but it is possible that System.out isn't flushed until after the return statement.

Upvotes: 1

Michał Szydłowski
Michał Szydłowski

Reputation: 3409

No, it is definitely not possible.

Upvotes: 1

Related Questions