Understanding Pass By Value and Pass By Reference in Java Development

 


The author of this post has written many articles about Java web development. In this article, he will share some important facts about Pass by Value and Pass by Reference. You can learn a lot more about these methods- just understand the details shared by the author in this post.

          In basic, there are two methods that a computer language can pass an argument to a subroutine. 1) Pass-By-Value and 2) Pass-By-Reference

          In Java, Objects are passed by reference, and primaries are passed by value.

          All of us can simply agree that primaries are passed by value; there’s no such thing in Java as a pointer to a primary.

          Java uses the pass-by-value. There is no pass-by-reference in Java. This Java tutorial is to walk you via the difference between pass by value and pass by reference, then search how Java uses pass by value with samples. Some say that in Java primaries are passed by value and objects are passed by reference. It is not right.

          But, Objects are not passed by reference. A true statement would be Object references are passed by value.

          Java does manage objects by reference, and all object variables are references. But, Java doesn’t pass method arguments by reference; it passes them by value.

Pass by Value

          Real parameter expressions that are passed to a method are estimate and a value is derived. Then this value is stored in a location and then it becomes the formal parameter to the call on method. This structure is called pass-by-value. Not the real reference but Passing a copy of the value.

          In Java, when you pass a primary type to a method, it is passed by value. So, what happens to the parameter that receives the argument has no effect outside the method.

EXAMPLE OF PASS BY VALUE

public class Example1 {

 

            public static void main(String args[]) {

                        int number = 3;

                        nextPrint(number);

                        System.out.println(“Print number Inside main(): ” + number);

            }

 

            public static void nextPrint(int number) {

                        number++;

                        System.out.println(“Print number Inside nextPrint(): ” + number);

            }

}

Output :

Print number Inside nextPrint(): 4

Print number Inside main(): 3

Above example, Primitives are passed as pass by values to method parameters and

Java Pass by reference both main() method and nextPrint() method should have printed the same value.

Pass by Reference

1.         By name or reference to the real parameter is passed to the method, so that it’s called pass by reference. Passing the address of the object, so that you may examine the real object.

2.         Java is always Pass by Value and not pass by reference.

3.         When you pass an object to a method, the condition changes badly, because objects are passed by successful call-by-reference.

4.         When you generate a variable of a class type, you are only creating a reference to an object.

5.         When you pass this reference to a method, the parameter that gets it will refer to the same object as that referred to by the argument. These successful means that object are passed to methods by use of call-by-reference.

EXAMPLE OF PASS BY REFERENCE.

public class Example2 {

           public static void main(String args[]) {

                   Car car1 = new Car(“Rolls-Royce”);

                   Car car2 = new Car(“BMW”);

                   System.out.println(“Brand of Car Inside main() before: 1) “+ car1.brand + ” 2) ” + car2.brand);

                   brandPrint(car1, car2);

                   System.out.println(“Brand of Car Inside main() after: 1) “+ car1.brand + ” 2) ” + car2.brand);

                   swap(car1, car2);

                   System.out.println(“Brand of Car after swap: 1) “+ car1.brand + ” 2)” + car2.brand);

               }

 

               public static void brandPrint(Car car1, Car car2){

                   car1.brand = “Bugatti”;

                   car2.brand = “Ferrari”;

               }

 

               public static void swap(Car c1, Car c2){

                       Car temp = new Car(“”);

                       temp = c1;

                       c1 = c2;

                       c2 = temp;

               }

 

               private static class Car{

                   private String brand;

 

                   public Car(String brand){

                       this.brand = brand;

                   }

 

               }

}

Output:


Brand of Car Inside main() before: Rolls-Royce

Brand of Car Inside brandPrint(): Bugatti

Brand of Car Inside main() after: Bugatti

1.         Above example, change made in method parameter is reflected globally. The brands of cars are changed in all places, which means one object is used in both methods. Swap() method test may be used for Pass by reference.

2.         You will find that method swap() within from the main() method is passed two objects car1 and car2 those are received by swap() in c1and c2 references respectively. Next inner swap() method c1 and c2 are exchanged. By doing so c1 holds the object earlier referred to by c2 and c2 does the opposite. In this procedure of exchanging formal parameters c1 and c2 there would be no impact on car1 and car2, which states that in Java arguments are passed by value, not by reference.

3.         That’s how Java handles pass-by-value and pass-by-reference mechanisms of passing parameters to methods.

4.         Objects are not swapped because Java uses the pass-by values.

5.         Key point to mention is that “The reference is copied as a value” to a new variable and it is given as a formal parameter to the called method.

6.         If you pass an object as a method parameter in Java it passes “value of reference” or object reference or handles to Object in Java. Here reference expression is fully different from the reference expression used in C and C+ which directly points to the memory address of the variable and is subject to pointer arithmetic. In Java object can only be accessed by its reference as you can’t get the memory address where the object is stored or more precisely there is no method to get the value of an object by passing the memory address.


The author of this post is a java web development professional. He has good exposure to java app development and has built many apps for distinct businesses. If you have any question related to Pass by Value and Pass by reference, then comment on this post. The experts will answer your query and will share proper thoughts about the subject. 

Related Articles:

Webclipse for Java Developers to Create Modern Apps with JavaScript and Java EE Development 

Java Tools Support Windows 10 And Visual Studio Now!


SHARE

Ethan Millar

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment