Rick Smith
Rick Smith

Reputation: 103

What is the purpose of void?

public class test
{
    public static void main(String[] args)
    {
        int x = 5;
        int y = 10;
        multiply(x,y);
    }
    public static void multiply(int x, int y)
    {
        int z = x*y;
        System.out.println(z);
    }
}

I am new to programming and I am confused on a few things.

  1. Why is it correct to use void? I thought void is used in order to specify that nothing will be returned but, the multiply method returns z.

  2. Do all programs require that you have exactly "public static void main(String[] args)"? What exactly is the purpose of the main method and what do the parameters "String[] args" mean? Would the program work if the main method was removed?

Thank You!

Upvotes: 3

Views: 4972

Answers (8)

ProfBits
ProfBits

Reputation: 219

Why is it correct to use void? I thought void is used in order to specify that nothing will be returned but, the multiply method returns z.

Your multiply method is correct to have void since it is returning nothing, it is just printing to the console.
Returning something means gives out a result to the programm for further computation.

For example your methode with return of the result would look like this:

public static int multiply(int x, int y)
{
    int z = x*y;            //multipling x and y
    System.out.println(z);  //printing the restult to the console
    return z;               //returning the result to the programm
}

this "new" method can be used like this for example:

public static void main(String[] args)
{
    int x = 5;
    int y = 10;
    int result = multiply(x,y);  //storing the returnen value of multiply in result 
    int a = result + 2;          //adding 2 to the result and storing it in a
    System.out.println(a);       //printing a to the console
}

Output:
50
52

Do all programs require that you have exactly "public static void main(String[] args)"? What exactly is the purpose of the main method and what do the parameters "String[] args" mean? Would the program work if the main method was removed?

This mehtod seves a the etry point for your programm. This meas the first thing that is executet of your programm is this mehtod, removing it would make the programm unrunneable.
String[] args stands for the commandline arguments you can give to you programm befor starting over the OS (OS = Windows for example) The exact purpose of all of the words is very well explained in the other answers here.

Upvotes: 0

Nick Burt
Nick Burt

Reputation: 45

Like others said, the multiply method does NOT return anything. The other answers explained why that is.

However it would also be helpful to mention that when you use void that method can not return anything. In contrast, if you set your method to return anything (not to void) you are required to return that type of value.

For example:

public static void main(String[] args){

    int a;
    a = returnInt();

}//End Method

public static int returnInt(){

    int z = 5;
    return z;

}//End Method

The main method does not return anything, which is why we use void. The returnInt method returns an integer. The integer that the method returns is z. In the main method where a = returnInt(); that sets the value of a to the value returned from returnInt(), in this case, a would equal 5.

Tried to keep it simple, hope it makes sense.

Upvotes: 1

AI Generated Response
AI Generated Response

Reputation: 8845

First, the multiply method does not return anything; it prints the product, but does not return any value.

public static void multiply(int x, int y)
    {
        int z = x*y;
        System.out.println(z); //may look like a return, but actually is a side-effect of the function.
    } //there is no return inside this block

Secondly, public static void main provides an entry point into your program. Without it, you cannot run your program. Refer to the Java documentation for more information on the usage of public static void main.

The String[] args here means that it captures the command line arguments and stores it as an array of strings (refer to the same link posted above, in the same section). This array is called args inside your main method (or whatever else you call it. Oracle cites argv as an alternate name)

System.out.print tells the program to print something to the console, while return is the result of the method. For example, if you added print all over your method to debug (a common practice), you are printing things while the program runs, but this does not affect what the program returns, or the result of the program.

Imagine a math problem - every step of the way you are "print"ing your work out onto the paper, but the result - the "answer" - is what you ultimately return.

Upvotes: 7

Mfali11
Mfali11

Reputation: 353

Void class is an uninstantiable class that hold a reference to the Class object representing the primitive Java type void. and The Main method is the method in which execution to any java program begins. A main method declaration looks like this

public static void main(String args[]){ }

The method is public because it be accessible to the JVM to begin execution of the program.

It is Static because it be available for execution without an object instance. you may know that you need an object instance to invoke any method. So you cannot begin execution of a class without its object if the main method was not static.

It returns only a void because, once the main method execution is over, the program terminates. So there can be no data that can be returned by the Main method

The last parameter is String args[]. This is used to signify that the user may opt to enter parameters to the java program at command line. We can use both String[] args or String args[]. The Java compiler would accept both forms.

Upvotes: 0

Shobit
Shobit

Reputation: 794

  1. When a method does not return anything, you specify its return type as "void". Your multiply method is not returning anything. Its last line is a print statement, which simply prints the value of its arguments on the standard output. If the method ended with the line "return z", then you would not be able to compile the program with the "void" return type. You would need to change the method signature to public static int multiply(int x, int y).

  2. All Java programs do require the public static void main(String[] args) if they are to be executable. It is the starting point of any runnable Java program. Here's what it means:

a. public - the main method is callable from any class. main should always be public because it is the method called by the operating system.

b. static - the main method should be static, which means the operating system need not form an object of the class it belongs to. It can call it without making an object.

c. void - the main method does not return anything (although it may throw an Exception which is caught by the operating system)

d. String[] args - when you run the program, you can pass arguments from the command line. For example, if your program is called Run, you can execute the command java Run 3 4. In that case, the arguments would be passed to the program Run in the form of an array of Strings. You would have "3" in args[0] and "4" in args[1].

That said, you could have a Java program without a main, which will not be runnable.

I hope that helps.

Upvotes: 2

Brian Lacy
Brian Lacy

Reputation: 19118

The multiply() method in your example does not return the value of z to the calling method, rather it outputs the value of z (e.g., prints it to the screen).

As you said, the void type keyword means that the method will not return a value. Methods like this are intended to "just do something". In the case of main(), the method will not return a value, because there is no calling method to return it to -- that's where your program begins.

OK, technically, that last comment is not accurate; it actually is possible to have your main return a value to the operating system or process that launched the program, but it isn't always necessary to do so -- especially for simpler console-based programs like those you'll write when you're just getting started! :)

Upvotes: 0

Aniket Inge
Aniket Inge

Reputation: 25733

Why is it correct to use void? I thought void is used in order to specify that nothing will be returned but, the multiply method returns z.

No

multiply method does not return z. However, you are correct, void is in fact used to specify that nothing will be returned.

Do all programs require that you have exactly "public static void main(String[] args)"? What exactly is the purpose of the main method and what do the parameters "String[] args" mean? Would the program work if the main method was removed?

yes, all programs must have a main function that looks like public static void main(String[] args).

Upvotes: 1

Maciej Cygan
Maciej Cygan

Reputation: 5471

public means that the method is visible and can be called from other objects of other types. Other alternatives are private, protected, package and package-private. See here for more details.

static means that the method is associated with the class, not a specific instance (object) of that class. This means that you can call a static method without creating an object of the class.

void means that the method has no return value. If the method returned an int you would write int instead of void.

The combination of all three of these is most commonly seen on the main method which most tutorials will include.

credits to Mark Bayres

Upvotes: 0

Related Questions