• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Pass by value and Pass by reference

October 8, 2011 By j2eereference Leave a Comment

What is Pass by value?

Pass by value means passing a value as an argument to a method.

What is Pass by reference?

In Pass by reference, passing the address itself to a method.

Java always support pass by value. It means we pass copy of variable value to a method, it’s may be either primitive variable or object reference variable, java will always pass copy of bits representing the value of a variable.

Passing primitive variables

When you are passing the primitive variables, you actually pass copy of variable value to a method. For ex. If you are passing int value 5 to a method, you are passing copy of bits representing value 5 to a method. The called method receives the copy of value and does whatever it likes. The value of the original variable cannot be changed within the called method. Let see example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<strong> public</strong> <strong>class</strong> Test
 
{
<strong> public</strong> <strong>static</strong> <strong>void</strong> main (String [] args)
      {
      int a = 6;
      Test t = <strong>new</strong> Test();
      System.<em>out</em>.println("Before passing value of a = " + a);
      t.modify(a);
      System.<em>out</em>.println("After passing value a = " + a);
     }
 
<strong> void</strong> modify(<strong>int</strong> b)
    {
     b=7;
     System.<em>out</em>.println("Value of b = " +b);
    }
 
}

Output :

Before passing value of a = 6

Value of b = 7

After passing value a = 6

Passing Object Reference Variable

The reference variable points to the object which is on the heap. The reference variable actually holds the bits representing the particular object on the heap. In pass by object reference variable you pass copy of reference variable to a called method.

For Ex:

Dog d1 = new Dog();

Dog d2 = d1;

In the above example, you can see both reference variables are pointing to same object on the heap. Let see an example where we are modifying same object.

1
<strong></strong>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<strong>public</strong> <strong>class</strong> ObjectTest
{
 
<strong> public</strong> <strong>static</strong> <strong>void</strong> main (String [] args)
   {
     ObjectTest t = <strong>new</strong> ObjectTest();
     StringBuffer sb = <strong>new</strong> StringBuffer("Hello World");
     System.out.println(sb);
     t.display(sb);
     System.out.println(sb);
}
 
<strong> public</strong> <strong>void</strong> display(StringBuffer str)
   {
      str = str.insert(6, "Java ");
   }
 
}

Output:

Hello World
Hello Java World

Related Posts

  • Busy Spinning in mutithreading
  • Implementing queue in java
  • TreeSet vs ConcurrentSkipListSet
  • How to create immutable class?
  • Implementing Stack in java
  • What is ReentrantLock?
  • What is Semaphore in java
  • Why AtomicInteger class
  • What is CyclicBarrier?
  • CountDownLatch in java

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.