user2026884
user2026884

Reputation: 37

Void and return in Java - when to use

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!

Upvotes: -1

Views: 20596

Answers (7)

Botea Florin
Botea Florin

Reputation: 633

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)

Upvotes: 0

Ashraf Sada
Ashraf Sada

Reputation: 4905

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

Upvotes: 0

Paul J Abernathy
Paul J Abernathy

Reputation: 1003

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.

Upvotes: 0

Mark Said Camilleri
Mark Said Camilleri

Reputation: 388

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.

Upvotes: 1

Marko Topolnik
Marko Topolnik

Reputation: 200296

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.

Upvotes: 3

ajay.patel
ajay.patel

Reputation: 1967

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)

Upvotes: -1

AllTooSir
AllTooSir

Reputation: 49432

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.

Upvotes: 4

Related Questions