-2

I heard that Java integers are pass by value, so why does the following code work in code.runnable.com?

public class HelloWorld {
  public static void main(String[] args) {
    int number = 0;
    number = 2;
    System.out.println(number);
  }
}

The code will print out 2.

1 Answers1

0

This code snippet doesn't pass number anywhere. You're declaring a local variable and then overriding its initial value. This is perfectly legal in Java, and has nothing to do with passing by reference or by value.

Mureinik
  • 297,002
  • 52
  • 306
  • 350