File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with passing arguments

 
tyler jones
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 3340
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps the campfire story Pass By Value Please would help you with this
 
Rahul Ghai
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
{
i=99;
}

public static void main(String args[])
{
int val=100;
getValue(val);
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic