FrustGoogle Now FrustGoogle Now - 11 days ago 5
Java Question

Difference between void and (int, string, etc.)

Whats the difference between

public void Sum()


and

public int Sum()


I tried searching on Google/YouTube and I've learned that
public int Sum()
needs a
return
statement. I'm using
return
a lot, but I really dont know what is the use of it on methods.

Answer

public void sum() doesn't return any value e.g. below:

  public class Calculate{
     private int sum = 0;

     public void performSum(){
         sum(3,4);
         System.out.println("sum="+this.sum);
     }

     public void sum(int value, int value2){
           sum = value1+ value2;
           return; //optional
     }
  }

public int sum() must return a value of type int e,g, below:

  public class Calculate{

     public void performSum(){
         int sum = sum(3,4);
         System.out.println("sum="+sum);
     }

     public int sum(int value, int value2){
          return value1+ value2;
     }
  }