Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Pass by Value in Java | Working with Parameters
Mastering Methods in Java

bookPass by Value in Java

Understanding Pass by Value in Java

In Java, all method arguments are passed by value. This means when you pass a variable to a method, Java copies the value of that variable and sends the copy to the method. The method then works with this copy, not the original variable from your code.

  • When you pass a primitive type (such as int, double, or boolean), the method receives a copy of the actual value. Any changes made to the parameter inside the method do not affect the original variable outside the method;
  • When you pass an object reference (like an array or a custom class), Java still passes the value of the reference (the memory address). The method receives a copy of this reference, so both the original and the method parameter point to the same object. You can change the object's internal state inside the method, but if you assign a new object to the parameter, the original reference outside the method is not affected.

This behavior is important for predicting how your variables change—or stay the same—after calling methods in Java.

Main.java

Main.java

copy
123456789101112131415
package com.example; public class Main { public static void main(String[] args) { int number = 10; System.out.println("Before method call: number = " + number); changeValue(number); System.out.println("After method call: number = " + number); } public static void changeValue(int num) { num = 20; System.out.println("Inside method: num = " + num); } }

In this code, the changeValue method receives a copy of the variable number because Java uses pass by value.
Changing num inside the method does not affect number in main, so its value remains 10 after the method call.

question mark

What is printed when running this code?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 2

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you give an example showing how this works with primitive types?

How does this behavior differ when passing objects instead of primitives?

Can you explain why Java doesn't use pass by reference?

bookPass by Value in Java

Svep för att visa menyn

Understanding Pass by Value in Java

In Java, all method arguments are passed by value. This means when you pass a variable to a method, Java copies the value of that variable and sends the copy to the method. The method then works with this copy, not the original variable from your code.

  • When you pass a primitive type (such as int, double, or boolean), the method receives a copy of the actual value. Any changes made to the parameter inside the method do not affect the original variable outside the method;
  • When you pass an object reference (like an array or a custom class), Java still passes the value of the reference (the memory address). The method receives a copy of this reference, so both the original and the method parameter point to the same object. You can change the object's internal state inside the method, but if you assign a new object to the parameter, the original reference outside the method is not affected.

This behavior is important for predicting how your variables change—or stay the same—after calling methods in Java.

Main.java

Main.java

copy
123456789101112131415
package com.example; public class Main { public static void main(String[] args) { int number = 10; System.out.println("Before method call: number = " + number); changeValue(number); System.out.println("After method call: number = " + number); } public static void changeValue(int num) { num = 20; System.out.println("Inside method: num = " + num); } }

In this code, the changeValue method receives a copy of the variable number because Java uses pass by value.
Changing num inside the method does not affect number in main, so its value remains 10 after the method call.

question mark

What is printed when running this code?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 2
some-alt