What is an argument in java programming

Difference between arguments and parameters in Java [duplicate]

I was going through some interview questions. I wasn’t able to come up with a solid answer to this question: Difference between arguments and parameters in Java?
How are they different?

You only have to worry about arguments if you’re married. Otherwise just use the term parameter for both.

The main point is that the terminology in this area is not clear-cut, so this makes for Yet Another Stupid Interview Question.

From Oracle’s tutorial (docs.oracle.com/javase/tutorial/java/javaOO/arguments.html): Note: Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration’s parameters in type and order.

5 Answers 5

Generally a parameter is what appears in the definition of the method. An argument is the instance passed to the method during runtime.

@PeterLawrey The way these terms are used in the linked WP page, 42 can be considered an instance of the type int .

The term parameter refers to any declaration within the parentheses following the method/function name in a method/function declaration or definition; the term argument refers to any expression within the parentheses of a method/function call. i.e.

Please have a look at the below example for better understanding:

package com.stackoverflow.works; public class ArithmeticOperations < public static int add(int x, int y) < //x, y are parameters here return x + y; >public static void main(String[] args) < int x = 10; int y = 20; int sum = add(x, y); //x, y are arguments here System.out.println("SUM IS: " +sum); >> 

System.out.println(344); vs int v=344; System.out.println(v); which is argument/parameter? Can you please help me?

They are not. They’re exactly the same.

However, some people say that parameters are placeholders in method signatures:

public void doMethod(String s, int i)

String s and int i are sometimes said to be parameters. The arguments are the actual values/references:

myClassReference.doMethod("someString", 25); 

«someString» and 25 are sometimes said to be the arguments.

There are different points of view. One is they are the same. But in practice, we need to differentiate formal parameters (declarations in the method’s header) and actual parameters (values passed at the point of invocation). While phrases «formal parameter» and «actual parameter» are common, «formal argument» and «actual argument» are not used. This is because «argument» is used mainly to denote «actual parameter». As a result, some people insist that «parameter» can denote only «formal parameter».

Источник

Passing Information to a Method or a Constructor

The declaration for a method or a constructor declares the number and the type of the arguments for that method or constructor. For example, the following is a method that computes the monthly payments for a home loan, based on the amount of the loan, the interest rate, the length of the loan (the number of periods), and the future value of the loan:

public double computePayment( double loanAmt, double rate, double futureValue, int numPeriods) < double interest = rate / 100.0; double partial1 = Math.pow((1 + interest), - numPeriods); double denominator = (1 - partial1) / interest; double answer = (-loanAmt / denominator) - ((futureValue * partial1) / denominator); return answer; >

This method has four parameters: the loan amount, the interest rate, the future value and the number of periods. The first three are double-precision floating point numbers, and the fourth is an integer. The parameters are used in the method body and at runtime will take on the values of the arguments that are passed in.

Читайте также:  Kotlin округление до целых

Note: Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration’s parameters in type and order.

Parameter Types

You can use any data type for a parameter of a method or a constructor. This includes primitive data types, such as doubles, floats, and integers, as you saw in the computePayment method, and reference data types, such as objects and arrays.

Here’s an example of a method that accepts an array as an argument. In this example, the method creates a new Polygon object and initializes it from an array of Point objects (assume that Point is a class that represents an x, y coordinate):

public Polygon polygonFrom(Point[] corners) < // method body goes here >

Note: If you want to pass a method into a method, then use a lambda expression or a method reference.

Arbitrary Number of Arguments

You can use a construct called varargs to pass an arbitrary number of values to a method. You use varargs when you don’t know how many of a particular type of argument will be passed to the method. It’s a shortcut to creating an array manually (the previous method could have used varargs rather than an array).

To use varargs, you follow the type of the last parameter by an ellipsis (three dots, . ), then a space, and the parameter name. The method can then be called with any number of that parameter, including none.

public Polygon polygonFrom(Point. corners) < int numberOfSides = corners.length; double squareOfSide1, lengthOfSide1; squareOfSide1 = (corners[1].x - corners[0].x) * (corners[1].x - corners[0].x) + (corners[1].y - corners[0].y) * (corners[1].y - corners[0].y); lengthOfSide1 = Math.sqrt(squareOfSide1); // more method body code follows that creates and returns a // polygon connecting the Points >

You can see that, inside the method, corners is treated like an array. The method can be called either with an array or with a sequence of arguments. The code in the method body will treat the parameter as an array in either case.

You will most commonly see varargs with the printing methods; for example, this printf method:

public PrintStream printf(String format, Object. args)

allows you to print an arbitrary number of objects. It can be called like this:

System.out.printf("%s: %d, %s%n", name, idnum, address);
System.out.printf("%s: %d, %s, %s, %s%n", name, idnum, address, phone, email);

or with yet a different number of arguments.

Parameter Names

When you declare a parameter to a method or a constructor, you provide a name for that parameter. This name is used within the method body to refer to the passed-in argument.

The name of a parameter must be unique in its scope. It cannot be the same as the name of another parameter for the same method or constructor, and it cannot be the name of a local variable within the method or constructor.

A parameter can have the same name as one of the class’s fields. If this is the case, the parameter is said to shadow the field. Shadowing fields can make your code difficult to read and is conventionally used only within constructors and methods that set a particular field. For example, consider the following Circle class and its setOrigin method:

Читайте также:  Java find in list by property

The Circle class has three fields: x , y , and radius . The setOrigin method has two parameters, each of which has the same name as one of the fields. Each method parameter shadows the field that shares its name. So using the simple names x or y within the body of the method refers to the parameter, not to the field. To access the field, you must use a qualified name. This will be discussed later in this lesson in the section titled «Using the this Keyword.»

Passing Primitive Data Type Arguments

Primitive arguments, such as an int or a double , are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost. Here is an example:

public class PassPrimitiveByValue < public static void main(String[] args) < int x = 3; // invoke passMethod() with // x as argument passMethod(x); // print x to see if its // value has changed System.out.println("After invoking passMethod, x codeblock"> 
After invoking passMethod, x = 3

Passing Reference Data Type Arguments

Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.

For example, consider a method in an arbitrary class that moves Circle objects:

public void moveCircle(Circle circle, int deltaX, int deltaY) < // code to move origin of circle to x+deltaX, y+deltaY circle.setX(circle.getX() + deltaX); circle.setY(circle.getY() + deltaY); // code to assign a new reference to circle circle = new Circle(0, 0); >

Let the method be invoked with these arguments:

Inside the method, circle initially refers to myCircle . The method changes the x and y coordinates of the object that circle references (that is, myCircle ) by 23 and 56, respectively. These changes will persist when the method returns. Then circle is assigned a reference to a new Circle object with x = y = 0 . This reassignment has no permanence, however, because the reference was passed in by value and cannot change. Within the method, the object pointed to by circle has changed, but, when the method returns, myCircle still references the same Circle object as before the method was called.

Источник

what is an argument in java

An argument in java is the value passed to the variables defined in the function when the function is called. Whenever the function is called during the execution of the program, arguments are passed to the function.

An argument when passed with a function, it takes the place of those variables which are used during the function definition and the function is executed with these values.

The line below shows a function being called. Here in this example, a function sum is called. The values 20, 30 are the arguments passed to the function sum.

After the execution of this code, the result from the sum function is stored in the variable total.

The example below illustrates how arguments can be used in a function.

public static int sum(int a, int b) < return a + b; // a is replaced by and 20 and b by 30 >public static void main(String[] args) < int result = sum(20, 30); // 20 and 30 is passed to sum System.out.println("Result language-java">Result = 50

The data type of the argument values passed must be the same as the parameter type defined in the function.

The arguments that are passed in the function call are called actual arguments or actual parameters. The data type of the actual parameter need not be mentioned in the function call.

The arguments can be passed in 2 ways, they are:

Pass by value

In Pass by value technique, the actual parameter's value is copied to memory and the copied values are passed to the function when the function is called.

So whatever changes made to the arguments inside the function do not reflect on the actual parameters. Because the changes are made to the copies and not to the original values.

The example program below illustrates pass by value technique

public class value < public static void increment(int x, int y) < x++; y++; >public static void main(String[] args) < int a = 10; int b = 20; value obj = new value(); // creating object System.out.println("Before changing a: " + a + " , b: " + b); // values before calling function obj.increment(a, b); // passing arguments to function increment System.out.println("After changing a: " + a + " , b: " + b); // values after calling function >>
Before changing a: 10 , b: 20 After changing a: 10 , b: 20

Pass by reference

In pass by reference technique, the reference(address) of the actual parameters are passed to the function during the function call.

Henceforth whatever changes made to the arguments inside the function are reflected on the actual parameters. The reason being when the value is changed using the reference, it changes the value of the actual variables.

The program below illustrates the pass by reference technique

public class reference < static int x = 11; static int y = 12; public static void passbyref(reference obj) < obj.x = 35; obj.y = 45; >public static void main(String[] args) < System.out.println("Original value of x and y : " + x + " " + y); reference object = new reference(); passbyref(object); // passing reference of arguments to function passbyref System.out.println("Changed value of x and y : " + x + " " + y); >>
Original value of x and y: 11 12 Changed value of x and y: 35 45

Command-line arguments

A command-line argument is an argument that is passed along with the run command of the program. The argument passed from the console is received by the java program as input and can be used in the program.

For example, let's consider the Example program. The syntax to compile and run the program in command prompt by passing arguments is given below.

// Compile by javac Example.java //Run by java Example 30

They are stored as strings in the String array passed to the main function. Any number of arguments can be accepted from the console because the argument type is an array.

To access the command-line arguments inside the program, one can traverse the args array using a loop or directly use the index value of the array. The args array is the parameter passed to the main method.

Let us consider that the command line argument is 30. The program below illustrates how to take arguments from the console and access them.

The command-line argument is: 30

The program below illustrates how to input multiple command-line arguments and access them. Let the command line arguments be Raj, 20, 80050, c.

Command line argument0 is: Raj Command line argument1 is: 20 Command line argument2 is: 80050 Command line argument3 is: c
Recommended Readings

Источник

Оцените статью