0

I have some "confusion" about void and return. In general, I understand void is used in methods without returning anything, and return is used in methods when I want to return something to the calling code.

But in the following code, I can use both, and my question is, which one to use? And how to determine? Is it about performance?

Normally, I have the same results in both cases.

public class Calc {
    private double number;

    Calc (double n)
    {
        number = n;
    }

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

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

Thank you!

user2026884
  • 47
  • 1
  • 1
  • 6

7 Answers7

4

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.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • thank you. but i am aware of definition for void and return keywords. thats not a problem. My questions are "But in the following code, I can use both, and my question is, which one to use? And how to determine? Is it about performance?" ..... – user2026884 Jul 08 '13 at 13:30
  • there is no change in performance in either case, in order to answer your question we need answer to another question which is - do you want to get the result right after calling `add(x)` method ? `if yes then use add1 otherwise go with add` – Navneet kumar Jun 25 '22 at 02:30
3

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.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1

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.

  • you are saying that variable n is visible outside of the int method (add1)? – user2026884 Jul 08 '13 at 14:10
  • can you give some more info about it or a link where i can read about it? – user2026884 Jul 08 '13 at 14:16
  • certainly. here is the java tutorial about [objects](http://docs.oracle.com/javase/tutorial/java/javaOO/objects.html) and [returning a value from methods](http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html) – Mark Said Camilleri Jul 08 '13 at 14:18
  • i have red [returning a value from methods](http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html) three times and didnt find what you are saying anywhere.... i know my english is not good, but this much... ;) – user2026884 Jul 08 '13 at 14:28
  • sure, but [returning a value from methods](http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html) and [objects](http://docs.oracle.com/javase/tutorial/java/javaOO/objects.html) go hand in hand, as when a value is returned, to use that value you need to create an object. Also, I forgot to mention that the object can transfer the return value to a variable: `(Classname) (name for object) = new (Classname)();` to create an object and `(variable type) (variable name) = (name of object you created).(method)();` to place the value in a variable. – Mark Said Camilleri Jul 08 '13 at 14:57
  • by the way, words in the brackets meant that you have to put what it says in it, so Classname = class where the return is (Calc in this case), methodname = add1 etc..... name of object, you decide when creating it. – Mark Said Camilleri Jul 08 '13 at 14:59
0

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.

Paul J Abernathy
  • 993
  • 2
  • 12
  • 25
0

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);

Ashraf Sada
  • 4,527
  • 2
  • 44
  • 48
0

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)

Botea Florin
  • 543
  • 6
  • 24
-1

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)

ajay.patel
  • 1,957
  • 12
  • 15