Java Arguments vs Parameters [Syntax & Examples]

 

Introduction to Java Arguments vs Parameters

Most beginner programmers confuse the term argument and parameter. And they mix both of them but in reality, they are two different terms. In this tutorial, we will learn about java arguments vs parameters. We will start by highlighting some of the main differences between java arguments and parameters. Then we will see how we can implement the same concept in the java programming language and will see when we use java arguments and parameters and their role in the Java programming language.

At the same time, we will take various examples and declare different data typed parameters and call using arguments. Moreover, we will also take a look at the optional java parameters and will also learn how to call them. All in all, this tutorial is going to be one of the best and detailed tutorials about java arguments vs parameters.

Advertisement

 

Understanding Java arguments vs parameters

Java parameter is a variable used to define a particular value during a function definition. Whenever we define a function we introduce our compiler with some variables that are being used in the running of that function. These variables are often termed Parameters. You can read more about java parameters from the article on Java parameters.

While the Java argument is a value passed to a function when the function is called. Whenever any function is called during the execution of the program there are some values passed with the function. These values are called arguments. You can read more about java arguments from the article on Java arguments. The diagram below shows the difference between java arguments and parameters.

Java arguments vs parameters

Notice that the Parameter is variable in the declaration of the function and the Argument is the actual value of this variable that gets passed to function.

 

General difference between java arguments vs parameters

There are few general differences between java arguments and parameters. Here we will highlight some of those differences and later in other sections, we will implement them through the java program and will see the difference. See the list below which shows some of the differences between java arguments and parameters.

  • The Java argument is a variable whose value is passed into a function and is used whenever a function is called. The parameter is the value that is defined in a functional block. So, it is basically used whenever we need to pass a value to a particular functional block.
  • Arguments are used to send the values to the function whereas parameters are used to receive or hold the values of arguments.
  • During a function call, each argument is associated with a parameter and vice versa is also true.
  • Arguments are called the actual parameter whereas parameters are called formal parameters.

 

Syntax of declaring Java parameters

Now we already have enough knowledge about java parameters. Let us see how we can declare different data typed parameters in a java programming language. First, we will see the general syntax and then will learn how we can create different data typed parameters. See the general syntax below:

methodName(Datatype p1, Datatype p2, ..., Datatype pn){
    // method statements
}

Notice that the parameters are declared while defining the function and the datatype of each parameter is also initialized. Two things are important while declaring the java parameters, one is the data type and the second is the name of the parameter. Now, see the syntax of creating Integer data typed parameters in Java.

Advertisement
methodName(Integer p1, Integer p2, ..., Integer pn){
    // method statements
}

In a similar way, the syntax of String typed parameters will look like the following:

methodName(String p1, String p2, ..., String pn){
    // method statements
}

We can also declare different datatype parameters in one function as well. See the example below.

methodName(String p1, Integer p2, ..., float pn){
     // method statements 
}

Notice that we can declare different data types in one function as well as shown in the above syntax.

 

Syntax of calling Java method with arguments

Now let us see how we can call a method by providing java arguments. The data type or the value will totally depend on the data type of parameter that we have declared while defining the function. The general syntax of calling a function with arguments looks like this:

methodName(value1, value2, ..., value3);

The value should have the same data type as declared in parameters. For example, if we have declared all parameters to be integer types, then we will call the function by providing all the int values. As shown below:

methodName(IntegerValue1, IntegerValue2, ..., IntegerValue3);

In the above case, all the values should be integer types as our parameters are integer typed. If we will provide the mismatched data typed argument in while calling a method, then we will get an error. That is why it is important to provide the data typed value that matched with the parameters of the method.

 

Example-1 Integer value and Java arguments vs parameters

Now let us implement all the knowledge that we get from the above sections and create a java method that accepts all the integer typed arguments. See the example below:

// Java main class
public class Main{
	// java main method
    public static void main (String args[]){
        // printing
        System.out.print("The total sum is: ");
        // java arguments
        System.out.println(myMethod(10, 10, 10));

    }
    // creating java method
    public static Integer myMethod(Integer num1, Integer num2, Integer num3){
        // return total sum of arguments
        return num1+ num2+ num3;
    }

Output:

Advertisement
The total sum is: 30

Notice that the total sum is 30 because it added all the three arguments that were integer typed. If we will provide mismatched data typed in the argument, it will return an error as shown below:

// Java main class
public class Main{
	// java main method
    public static void main (String args[]){
        // printing
        System.out.print("The total sum is: ");
        // java arguments
        System.out.println(myMethod(10, "Linux", 10));

    }
    // java arguments vs parameters
    // creating java method
    public static Integer myMethod(Integer num1, Integer num2, Integer num3){
        // return total sum of arguments
        return num1+ num2+ num3;
    }
}

Output:

java argument error

 

Example-2 String values and Java arguments vs parameters

In similar, if we have declared the data type of parameters to be string, then we have to provide the string values as arguments while calling the function. As shown below:

// Java main class
public class Main{
	// java main method
    public static void main (String args[]){
        // java arguments
        System.out.println(myMethod("Welcome ", "to ", "golinuxcloud"));
    }
     // java arguments vs parameters
    // creating java method
    public static String myMethod(String value1, String value2, String value3){
        // return total sum of arguments
        return value1+ value2+value3;
    }
}

Output:

Welcome to golinuxcloud

Notice that the code, successfully executed, when we provide all the arguments values as strings because we declared them to be of string type while declaring java parameters. One more thing to notice is that, in the above example, we have declared only three string typed parameters in our function, if we will provide arguments more than or less than three then again we will get an error as shown below:

// Java main class
public class Main{
	// java main method
    public static void main (String args[]){
        // java arguments
        System.out.println(myMethod("Welcome ", "to ", "golinux", "cloud"));
    }
     // java arguments vs parameters
    // creating java method
    public static String myMethod(String value1, String value2, String value3){
        // return total sum of arguments
        return value1+ value2+value3;
    }
}

Output:

java more arguments error

Notice that in our function we declare three parameters and while calling the function, we provide four arguments which gives an error as shown above. So it is important to have an equal number of and same data typed parameters and arguments, otherwise, the program will not execute and give errors.

 

Java optional parameters and arguments

In the above section, we have seen that if we do not provide the same number of arguments as we have declared in the function definition, then the program gives an error. But sometimes, we don't know exactly the number of arguments to be provided by the user. In such cases, the java optional parameters help us which makes the parameters optional, which that means, even if we will not provide the values while calling the function, it will not return any error as the parameters are now optional.

You can learn more about the optional parameters from the article on java optional parameters. In this section, we will just take an example and see how we can declare optional parameters and how we can call them with a different number of arguments.

 

Example of Java optional parameters and arguments

Now let us see the example of java optional parameters. There are different ways to create java optional parameters but we will not use all of them. Here we will use the method overloading method to create optional parameters. See the example below:

// Java main class
public class Main{
	// java main method
    public static void main (String args[]){
        // calling method without arguments
        System.out.println(myMethod());
        // calling method with one argument
        System.out.println(myMethod(10));
        // calling method with two arguments
        System.out.println(myMethod(10, 10));
        // calling the method with three arguments
        System.out.println(myMethod(10, 10, 10));

    }
    // java method which takes no arguments
    public static Integer myMethod(){
        // return null
        return null;
    }
    // java method which takes one argument
    public static Integer myMethod(Integer value1){
        // return total sum of arguments
        return value1;
    }
    // java method which takes two arguments
    public static Integer myMethod(Integer value1, Integer value2){
        // return total sum of arguments
        return value1+ value2;
    }
    // java method which takes three arguments
    public static Integer myMethod(Integer value1, Integer value2, Integer value3){
        // return total sum of arguments
        return value1+ value2+value3;
    }
}

Output:

null
10
20
30

Notice that in the above example, we were able to call the same method with multiple arguments because we used the java method overloading method to create java optional parameters.

 

Summary

The parameter is also known as place holder which means it belongs to the function naming and be used in the function body. While arguments are known as actual values which means an actual value that is passed by the function call. In this tutorial, we learned about java arguments and parameters. We discussed the basic differences and learn about the syntax of creating different data typed parameters and how to call them. Also, we covered some of the examples where we created a java method that declared different data typed parameters, and then we call the method by providing the arguments.

Moreover, we also discussed how we can create optional parameters in java and how to call them using multiple arguments. In a nutshell, this tutorial covers the differences between java arguments and parameters by giving different examples.

 

Further Reading

Java arguments
Java parameters
Java optional parameters

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment