5
  1. What is the effect of not catching the value of the method that returns a value?

  2. Does it develop complications like memory issues if the return value was not caught.

Example code snippets:

//reference types
public Object[] thismethodreturnsvalue(){
   return new Object[]{new Object(),new Object(),new Object()};
}

//primitive types
public int thismethodreturnsint(){
   return -1;
}

public static void main(String a[]){
   thismethodreturnsvalue();
   thismethodreturnsint();
}

6 Answers 6

3

1. What is the effect of not catching the value of the method that returns a value?

No effect really.

The return value will be computed, and if it was created on the heap (as in your first example), it will be eligible for garbage collection right away.

In your second example, the return value will end up on the stack and will simply be discarded after the method has returned.

2. Does it develop complications like memory issues if the return value was not caught.

No, not really. As I said above, it will be garbage collected.

Here is a bytecode dump of what happens at the call site:

public class Test {

    public static int testMethod() {
        return 5;
    }

    public static void main(String[] args) {
        testMethod();
    }
}

... corresponding bytecode:

public static int testMethod();
  Code:
   0:   iconst_5
   1:   ireturn


public static void main(java.lang.String[]);
  Code:
   0:   invokestatic    #2;
   3:   pop                 // return value popped immediately.
   4:   return
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the vivid answers. Gladly appreciated.
2
  1. There is no effect. If you just execute a method for the side effects and not the return value, all that will happen is the side effects get done.

  2. Nope. The return value gets marked for garbage collection immediately.

Comments

1

Nothing bad happens just because the return value is dropped. But it can, in some cases, indicate misuse or misunderstanding of the API (e.g. String.replace()).

Comments

0

The return value will be allocated when you call but since it will have no references to it, it will be eligible for garbage collection

Comments

0

It is pretty weird, if a method returns a value, yet the caller ignores it.

Broadly speaking, methods have two utilities, one for querying system state, one for changing system state.

Some argue that a method should only do one of the two things, but not both. A query method must not change system state, and must return a value. An update method must not return a value. To find out the new system state after calling an update method, call a query method.

In this paradigm, it is certainly wrong to ignore a return value - if not for getting the return value, the (query) method call is totally bogus.

That paradigm is not what we adopt though. We do have methods mixing two utilities together. There are also methods that return values for some other odd reasons, like those "fluent methods". Therefore it is sometimes legitimate to ignore method return values.

Comments

-1

There is no consequence to this in the JVM. It can be unclear if you're not using the return value, as you're just calling the method then for its side effects.

If you're doing this regularly its questionable - for example, in your example your method is returning what looks like an error code - if you're going to ignore the error code you should at least document that.

1 Comment

I disagree. How often do you check the value returned from Map.put() or Set.add() ? There are many API calls that return some additional informative data that may or may not be valuable for your use case. Trying to document every instance where you ignore a return value would be quite burdensome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.