Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Understanding Parameters and Arguments | Working with Parameters
Mastering Methods in Java

bookUnderstanding Parameters and Arguments

Parameters vs. Arguments in Java

Parameters are the variable names you define in a method's declaration. They act as placeholders for the values your method will use when it runs.

Arguments are the actual values you provide when you call a method. These values are assigned to the method's parameters.

Example:

public void greet(String name) { // 'name' is a parameter
    System.out.println("Hello, " + name);
}

greet("Alice"); // 'Alice' is an argument
  • Parameters: Defined in the method declaration as part of the method signature;
  • Arguments: Supplied when you call the method;
  • Parameters receive the values of the arguments during method execution.
Main.java

Main.java

copy
12345678910111213141516
package com.example; public class Main { // This method has two parameters: 'int a' and 'int b' public static int addNumbers(int a, int b) { // 'a' and 'b' are parameters inside the method definition return a + b; } public static void main(String[] args) { // Here, 5 and 7 are arguments passed to the method int result = addNumbers(5, 7); // '5' is the first argument, '7' is the second argument System.out.println("Sum: " + result); } }

The method greet takes a single parameter name and prints a greeting using that argument, and the method add takes two parameters a and b and returns their sum.
In main, greet is called with "Alice" and add is called with 5 and 7, showing how arguments are passed to methods, and multiple parameters can be included in a method by separating them with commas.

question mark

What is the difference between a parameter and an argument in Java methods?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 1

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you explain the difference between parameters and arguments with more examples?

What happens if I pass the wrong number of arguments to a method in Java?

Can a method have no parameters in Java?

bookUnderstanding Parameters and Arguments

Stryg for at vise menuen

Parameters vs. Arguments in Java

Parameters are the variable names you define in a method's declaration. They act as placeholders for the values your method will use when it runs.

Arguments are the actual values you provide when you call a method. These values are assigned to the method's parameters.

Example:

public void greet(String name) { // 'name' is a parameter
    System.out.println("Hello, " + name);
}

greet("Alice"); // 'Alice' is an argument
  • Parameters: Defined in the method declaration as part of the method signature;
  • Arguments: Supplied when you call the method;
  • Parameters receive the values of the arguments during method execution.
Main.java

Main.java

copy
12345678910111213141516
package com.example; public class Main { // This method has two parameters: 'int a' and 'int b' public static int addNumbers(int a, int b) { // 'a' and 'b' are parameters inside the method definition return a + b; } public static void main(String[] args) { // Here, 5 and 7 are arguments passed to the method int result = addNumbers(5, 7); // '5' is the first argument, '7' is the second argument System.out.println("Sum: " + result); } }

The method greet takes a single parameter name and prints a greeting using that argument, and the method add takes two parameters a and b and returns their sum.
In main, greet is called with "Alice" and add is called with 5 and 7, showing how arguments are passed to methods, and multiple parameters can be included in a method by separating them with commas.

question mark

What is the difference between a parameter and an argument in Java methods?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 1
some-alt