The moose likes Beginning Java and the fly likes problem with passing arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with passing arguments" Watch "problem with passing arguments" New topic

problem with passing arguments

tyler jones
Ranch Hand

Joined: Dec 01, 2000
Posts: 101
In a book I'm using, it has an example question with this code:

They ask what the output will be and I thought it would be "a=1 b=1 bArr[0]=1", but they have the correct answer as "a=0 b=0 bArr[0]=1". Can anyone explain why? I'm really getting confused with passing parameters to methods and how the original value is affected assuming it's a primitive value or an object. If anyone can shed some more light on this subject, it would be a great help! Thanks a lot
Anand Krishna

Joined: Jan 14, 2001
Posts: 5
This is because the primitive data types are passed as a copy to that variable where as objects (including array references) are passed as reference pointers (not exactly as C++ pointers but internally the same).
I hope this helps.
Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
Perhaps the campfire story Pass By Value Please would help you with this

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Rahul Ghai

Joined: Nov 03, 2000
Posts: 21
Hi Jones,
Before we discuss the solution, you have to be clear with the concept of passing by reference and passing by value.
When an argument is passed by value into a method, changes in the argument value by the method do not affect the original data. This is what happens when you pass any primitive data types.
This point has been explained in the following example:
public class PassingExample
static void getValue(int i)

public static void main(String args[])
int val=100;
System.out.println("The value of val is : "+val);
Here the output will be 100 because any change done to data above in the getValue() function is to the copy of the "val" variable given to the getValue() and not to the original value.

But what when we pass any object or an array? In these cases what gets passed into a method is a copy of a reference to an array or object. Remember that in pass by reference the original data is manipulated by the called or caller methods as no copies of the data are passed but only the address to them is given.
Coming back to your program, you are passing the primitive type "a" to inc1(). So there is no manipulation to the variable "a" as the value is getting passed and a different copy of "a" in inc1() is created and modified, not effecting original data. Since we are not doing anything with variable "b" its value will remain the same i.e. 0.
Secondly, when you pass the array to inc2(), what is getting passed is a copy of the reference or the address and not the data as a whole. So the same data is pointed out by the called function.Any change in inc2() will effect the original data.
I hope its clear now.
I agree. Here's the link:
subject: problem with passing arguments
It's not a secret anymore!