FrustGoogle Now
FrustGoogle Now

Reputation: 103

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.

Upvotes: 2

Views: 38069

Answers (7)

S. Mayol
S. Mayol

Reputation: 2625

void methods do not return anything - they simply perform an action. While int, double, boolean, String methods return integer, double, String value.

For example,

public void sum() {
    System.out.println("1 + 1 = 2"));  // we don't return anything
}

public int sum() {
    return 1 + 1;  // we return an integer
}

Similarly,

public double sum() {...}  // returns a double

public boolean sum() {...}  // returns a boolean

public String sum() {...}  // returns a String

The first method type void sum() just perform an action call a method println() and the purpose of this method is to print a String value.

Here is another example:

void bark() {
    System.out.println("Woof-Woof");
  }

You will see that print out on your screen: Woof-Woof because the body of bark() method has inside the println() method and this method has a statement "Woof-Woof" type String, so bark() method is performing an action: calling println() method, and println() method is returning a String statement.

System.out.println is composed of three parts:

1) System – is a final class in java.lang package.

2) out – is a static member field of System class and is of type PrintStream.

3) println() – is a method of PrintStream class.

If you want to know more about System.out.println here is a link that explains with some details:

http://javapapers.com/core-java/system-out-println/

I hope this help you.

Upvotes: 0

Rajesh
Rajesh

Reputation: 1

    public class Prime 
    {

        void dispaly()
        {
            for(int i=1;i<100; i++)

            {
                boolean isPrimeNumber = true;

                for(int j=2; j<i; j++)
                {

                    if(i%j==0) 
                    {
                    isPrimeNumber=false;
                    break;
                    }

                }



            if(isPrimeNumber)
            {

                System.out.print(i + " ");

            }


        }


        }

        void dispaly1()
        {
            System.out.print("\n");

            for(int i=1;i<100; i++)

            {


                for(int j=2; j<i; j++)
                {

                    if(i%j==0) 
                    {


                    System.out.print(i + " ");
                    break;

                    }

                }




            }


        }

        public static void main(String[] args)
        {
            Prime p= new Prime();
            System.out.print("The Prime Numbers are : ");
            System.out.print("\n");
            p.dispaly();
            System.out.print("\n");
            System.out.print("The Composite Numbers are : ");
            p.dispaly1();

        }
    }

Upvotes: -2

arshajii
arshajii

Reputation: 129497

void methods do not return anything - they simply perform an action. int methods on the other hand, as the name suggests, return an integer value.

For example,

public void sum() {
    System.out.println("1 + 1 = 2"));  // we don't return anything
}

public int sum() {
    return 1 + 1;  // we return an integer
}

Similarly,

public double sum() {...}  // returns a double

public boolean sum() {...}  // returns a boolean

public String sum() {...}  // returns a String

...  // you get the idea

As a note, it is convention to start method names with a lower-case letter.

Upvotes: 4

Yogendra Singh
Yogendra Singh

Reputation: 34367

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

Upvotes: 5

Amarnath
Amarnath

Reputation: 8865

Naming Convention: Always remember that your method name should start with the small letter. Use camel case for method names.

void: This method does not return anything.

int: This method returns an integer.

Please read a good java book, I suggest you to go for Java - Complete Reference by Herbert Schildt.

Upvotes: -1

Ravindra Bagale
Ravindra Bagale

Reputation: 17655

when u dont want to return any thing from the function then use- void
if u want to return integer then use int

public void sum() {
    System.out.println(5+6);
}

public int sum() {
    return (5 + 6);
}


public void sum() {
        System.out.println(5+6);
    }

this function can be called as

sum();


public int sum() {
        return (5 + 6);
    }

this function can be called as

int a=sum();

Upvotes: -1

Abubakkar
Abubakkar

Reputation: 15644

first one doesn't return anyhting you may use this if you want to say just print the added values.

Second method returns an int you use this kind of method if you want that added value some where else in your program so that you can use it.

Upvotes: 0

Related Questions