- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Call by Value and Call by Reference
Java Parameter Passing
In general, there are the following two ways that a computer language can pass an argument to a subroutine :
- call-by-value - The call-by-value approach copies the value of an argument into the formal parameter of the subroutine. Therefore, changes made to parameter of the function or subroutine have no effect on the argument.
- call-by-reference - In call-by-reference approach, a reference to an argument (not the value of the argument) is passed to the parameter. Inside the subroutine, this reference is used to access the actual argument determined in the call. This means that changes made to the parameter will affect the argument used to call the subroutine.
Java call by value
As you will see, Even though Java uses call-by-value to pass all the arguments, the precise effect differs between whether a primitive type or a reference type is passed.
Whenever you pass primitive type to a method, it is passed by value. Thus, a copy of the argument is made, and what happens to parameter that receives the argument has no effect outside the method. For example, consider the following example program :
Java Call by Value Example
Here is an example program, helps you to understand, how to use call by value method to pass parameter to the method in Java:
/* Java Program Example - Java Argument Passing * Primitive types are passed by value */ class Test { void meth(int i, int j) { i = i * 2; j = j / 2; System.out.println("\nThe value of a and b in method after performing action :"); System.out.println("a = " + i + "\t b = " + j); } } public class JavaProgram { public static void main(String args[]) { Test obj = new Test(); int a = 150, b = 200; System.out.println("The value of a and b before call :"); System.out.println("a = " + a + "\t b = " + b); obj.meth(a, b); System.out.println("\nThe value of a and b after call : "); System.out.println("a = " + a + "\t b = " + b); } }
When the above Java program is compile and executed, it will produce the following output:
As you can see, the operations that occur in the method named meth() have no effect on the values of a and b used in the call, and their values here did not change to 300 and 100.
Java Call by Reference
When you pass an object to a method, then the situation changes dramatically, because objects are passed by what is effectively call-by-reference.
Always keep in mind that when you create a variable of a class type, you are only creating a reference to an object. Therefore, when you pass this reference to a method, the parameter that receives it will refer to the same object as that referred to by the argument. This actually means that objects act as if they are passed to the methods by the use of call-by-reference. Changes to the object within the method do affect the object used as an argument. For example, consider the following program :
Java Call by Reference Example
Below is an example program given, uses call by reference method, to pass argument/parameter to the method in Java:
/* Java Program Example - Java Argument Passing * Objects are passed through their reference */ class Test { int a, b; Test(int i, int j) { a = i; b = j; } /* pass an object */ void meth(Test o) { o.a = o.a * 2; o.b = o.b / 2; } } public class JavaProgram { public static void main(String args[]) { Test obj = new Test(150, 200); int a = 150, b = 200; System.out.println("The value of obj.a and obj.b before call :"); System.out.println("obj.a = " + obj.a + "\t obj.b = " + obj.b); obj.meth(obj); System.out.println("\nThe value of obj.a and obj.b after call : "); System.out.println("obj.a = " + obj.a + "\t obj.b = " + obj.b); } }
When the above Java program is compile and executed, it will produce the following output:
As you can see, in this case, the actions inside the meth() have affected the object used as an argument.
« Previous Tutorial Next Tutorial »