www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Parameter Passing in Java Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
In Java, all parameters are passed by value, but for arrays and objects of classes only a reference to the actual parameter, which really is a pointer, is passed. Therefore changing an array element or a class field inside the function does change the actual parameter's element or field. Being a programmer, this demands extra care from you. This would become clearer as you go through the following code examples of parameter passing by value and parameter passing by reference in Java:

PARAMETER PASSING BY REFERENCE:
void f( int [ ] A ) { 
   A[0] = 10; 	// change an element of parameter A 
} 

void g() { 
   int [ ] B = new int [3]; 
   B[0] = 5; 
   f(B); 
			// B[0] is now 10, because function f changed the
			// first element of the array 
} 
PARAMETER PASSING BY VALUE:
void f( int A ) { 
   A = 10; 	// change the value of parameter A to 10
} 

void g() { 
   int B =  5; 
   f(B); 
			// B has not changed even though the function f changed the
			// value to 10 
}

Next >>> Lesson No. 17: Exception Handling in Java 1





Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.