Java Tutorial

Java Method Parameters

Parameters receive the values you pass at the call site. Primitives are copied; object references are shared — so changes to the object are visible to the caller.

Pass arguments into a method

The names in the method header are parameters. The values you write in the call are arguments. Inside the method, parameters behave like local variables that start with those argument values.

Example

public class Main {
  static void greet(String name, int times) {
    for (int i = 0; i < times; i++) {
      System.out.println("Hi, " + name);
    }
  }

  public static void main(String[] args) {
    greet("Ada", 2);
  }
}

Primitives are copied

When you pass an int, the method gets its own copy. Reassigning the parameter does not change the caller's variable. Java always passes by value — for primitives, the value is the number itself.

Example

public class Main {
  static void bump(int n) {
    n = n + 1;
    System.out.println("inside: " + n);
  }

  public static void main(String[] args) {
    int x = 5;
    bump(x);
    System.out.println("outside: " + x);   // still 5
  }
}

Object references are shared

For objects, the value that is copied is the reference. The method and the caller point at the same object, so mutating fields inside the method is visible outside. Reassigning the parameter to a new object does not rebind the caller's variable.

Example

class Counter {
  int n;
}

public class Main {
  static void bump(Counter c) {
    c.n = c.n + 1;
  }

  public static void main(String[] args) {
    Counter c = new Counter();
    c.n = 5;
    bump(c);
    System.out.println(c.n);   // 6 — same object
  }
}

Java has no default parameter values. If you want optional arguments, write overloads — separate methods with the same name and different parameter lists — covered in the next lesson.

Try It Yourself

Exercise: Write static int scale(int value, int factor) that returnsvalue * factor. Call it from main and print the result. Confirm the originalvalue variable is unchanged.

Show solution
public class Main {
  static int scale(int value, int factor) {
    return value * factor;
  }

  public static void main(String[] args) {
    int value = 4;
    int result = scale(value, 3);
    System.out.println(result);   // 12
    System.out.println(value);    // 4
  }
}

The method returns a new number; the caller's value stays 4 because primitives are copied.

Key Takeaways

  • Parameters receive the arguments you pass at the call site.
  • Primitives are copied; reassigning a parameter does not change the caller.
  • Object references are copied, but they still point at the same object — field changes show through.
  • Java has no default arguments; use overloading for alternate parameter lists.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.

Physics

Pass mass and height

Parameters carry the numbers into the method. mgh needs both m and h; the signature makes that contract visible.

E = mgh

Example

public class Main {
  static double pe(double m, double h) {
    return m * 9.81 * h;
  }

  public static void main(String[] args) {
    System.out.println(pe(2.0, 1.5) + " J");
  }
}

FAQ: Java Method Parameters

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java method parameters examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java method parameters in this Java Java lesson (Java Method Parameters).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.