Pass by Value and Reference in Java Programming

In this tutorial, you will learn about pass by value an reference in java programming.

You will learn what is pass by value and reference, how to pass arguments to methods by value and by reference, what is the difference between them, what is the advantage of using one over another in detail with example.

Source code for this video tutorial


package method;

public class Tutorial {

 public static void main(String[] args) {
 // int number = 25;
 // System.out.println("Before calling display method number= "+number);
 // display(number);
 // System.out.println("after calling display method number= "+number);
  int[] value = {125,635};
  System.out.println("before displayArray methodfirst element of value array = "+value[0]);
  displayArray(value);
  System.out.println("after displayArray methodfirst element of value array = "+value[0]);
 }
 
 public static void display(int num){  
  System.out.println("Inside display method num = "+num);
  num = 100;
  System.out.println("Inside display method num = "+num);
 }
 
 public static void displayArray(int[] values){
  System.out.println("Inside displayArray method first element is  = "+values[0]);
  values[0] = 100;
  System.out.println("Inside displayArray method first element is  = "+values[0]);
 }
}