HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

Java Method Parameters

Parameters and Arguments

Information can be passed to methods as parameter. Parameters act as variables inside the method.

Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.

The following example has a method that takes a String called fname as parameter. When the method is called, we pass along a first name, which is used inside the method to print the full name:

Example
    
 public class Main {
  static void myMethod(String fname) {
    System.out.println(fname + " Refsnes");
  }

  public static void main(String[] args) {
    myMethod("Ram")
    myMethod("Mohan");
    myMethod("Sohan");
  }
}
// Ram Refsnes
// Mohan Refsnes
// Sohan Refsnes
              

When a parameter is passed to the method, it is called an argument. So, from the example above: fname is a parameter, while Liam, Jenny and Anja are arguments.

Multiple Parameters

You can have as many parameters as you like:

Example
    
 public class Main {
  static void myMethod(String fname, int age) {
System.out.println(fname + " is " + age);
  }

  public static void main(String[] args) {
   myMethod( "Ram", 7);
    myMethod( "Mohan", 8);
    myMethod( "Sohan", 25);
  }
}

// Ram is 7
// Mohan is 8
// Sohan is 25
              


Return Values

The void keyword, used in the examples above, indicates that the method should not return a value. If you want the method to return a value, you can use a primitive data type (such as int, char, etc.) instead of void, and use the return keyword inside the method:

Example
    
public class Main {
  static int myMethod(int a) {
    return 6 + a;
  }

  public static void  main(String[] args) {
    System.out.println( myMethod(4));
  }
}
// Outputs 10 (6 + 4)
              

This example returns the sum of a method's two parameters:

Example
    
 Main {
  static intmyMethod(int a, int b) {
    return a + b;
  }

  public static void main(String[] args) {
    System.out.println(myMethod(6, 4));
  }
}
// Outputs 10 (6 + 4)
              

You can also store the result in a variable (recommended, as it is easier to read and maintain):

Example
    
public class Main {
  static int myMethod(int a, int b) {
    return a + b;
  }

  public static void main(String[] args) {
    int c = myMethod(6, 4);
    System.out.println(c);
  }
}
// Outputs 10 (6 + 4)
              

A Method with If...Else

It is common to use if...else statements inside methods:

Example
    
public class Main {

  // Create a checkAge() method with an integer variable called age
  static void checkAge(int age) {

    // If age is less than 18, print "access denied"
    if (age < 18) {
      System.out.println("Access denied - You are not old!");

    // If age is greater than, or equal to, 18, print "access granted"
    } else {
      System.out.println("Access granted - You are old!");
    }

  }

 public static void main(String[] args) {
    checkAge(25); // Call the checkAge method and pass along an age of 25
  }
}

// Outputs "Access granted - You are old!"