Void and return in Java - when to use

一世执手 提交于 2019-11-29 09:01:43

This method :

public void add(double n)
{
   number += n;
} 

You change the internal state of number but the caller of this method doesn't know the final value of number.

In the below method, you are intimating the caller the final value of number.

public double add1(double n)
{
   return number = number +  n;
}

I would suggest to keep a separate getNumber() getter method.

Do you want to be able to write this?

Calc c = new Calc();
double a = c.add(3);

If yes, then keep your add1 method. A perhaps better way to utilize the return value in your kind of object is the following:

public class Calc {
  ....
  public Calc add(double d) { number += d; return this; } 
}

Now you can write

Calc c = new Calc().add(1).add(2);

which is many times very convenient, reads well, and conserves the vertical screen space.

This idiom is called the fluent API.

In the void method:

public void add(double n)
{
    number += n;
} 

You aren't able to use the n variable across methods. This means that you won't be able to use it for the Calc method.

However, when using a method which actually returns something, like:

public double add1(double n)
{
    return number = number +  n;
}

you get to use that variable as an object thus allowing you to use it within the Calc method and others as many times as you wish, each time might be the same object (not advised if using to calculate using different values) or a new object every time.

As far as I know, there are no visible performances issues.

I personally prefer to return a value. It allows you to give information to the calling code with little to no cost (performance is generally about the same either way). It can either be the result of the calculation or, as Mark mentioned, the object to allow you to chain statements together. Which one might depend on your specific application.

return should not be used with void class type as the program output is returned by default and can be displayed through appropriate methods of the java class, like when you want to display the output of your program:

System.out.println("number = " + number);

you have function definition like this: -public(or private, etc)-lvl access. -static(or blank)to access that method without creating an instance of that object. -and void: you have noticed that any function that return something have that 'something's' data type included in definition (eg: returning int values, or strings). Now, when your function have no return, think at VOID like a placeholder for datatype(int,string, etc)

Think about it this way: if you remove below from your code, do you think your class is of any use:

public double add1(double n)
    {
        return number = number +  n;
    }

As you are doing calculation, but the result of the calculation is never known to the caller of the API:public void add(double n)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!