When I write like this:
public class test {
void mainx()
{
int fyeah[] = {2, 3, 4};
smth(fyeah);
System.out.println("x"+fyeah[0]);
}
void smth(int[] fyeah)
{
fyeah[0] = 22;
}
}
It prints x22;
When I write like this:
public class test {
void mainx()
{
int fyeah = 5;
smth(fyeah);
System.out.println("x"+fyeah);
}
void smth(int fyeah)
{
fyeah = 22;
}
}
It doesn't print x22, but prints x5.
Why, in the second version function, doesn't the value change? Does it change values only for array elements?
Seeing it a bit less technically, I would say that
is changing the content of (the object pointed to by) fyeah, while
is changing (the variable) fyeah itself.
Another example:
is changing (the content of) the person while
is changing the variable person - the original instance of Person is still unchanged (and, since Java is pass-by-value, the variable in the calling code is not changed by this method)
Think it in terms of memory: Lets analyse your first program -
In
mainx
,fyeah
is an array ofint
s , so its a reference ( or a pointer if I may ). This reference points to a location in heap memory where the actual array ofint
s is stored. Lets say at address 100. Located contiguously from here are three ints ( lets say beginning at address 100 , 104 and 108 respectively are 2 ,3 and 4 ).Now you call your method
smth
and pass the reference. Within the method , there is another reference ( of anint
array type ) namedfyeah
. Thisfyeah
is quite distinct fromfyeah
reference in themainx
method. Now , when you callsmth
and pass thefyeah
frommainx
, thefyeah
within thesmth
method is initialized to point to the same location ( ie memory address 100 ) When you access the 0 element offyeah
and assign it a value of 22 , it reaches out to the memory location of 100 and writes this value 22 there. When you come back in yourmainx
method , thefyeah
reference is still referring to memory address 100. But the value present at that location is now 22. So you get that value when you access the first element fromfyeah
inmainx
.Now , your second program. Your
mainx
method declares anint
( not an array , but a simpleint
) and set it to 5. Thisfyeah
variable is created on stack not on the heap. The value of 5 is stored on the stack. Now you callsmth
and pass this variable. Within the methodsmth
, you again declare anint
variable ,fyeah
by name ( as a formal method argument ). Again this is distinct from thefyeah
of themainx
method and thisfyeah
is also created on stack.This variable will be initialized to a value of 5, copied over from thefyeah
you passed tosmth
as argument. Note now that there are two distinct copies onfyeah
variables , both on stack , and both a value of 5. Now you assign thefyeah
insmth
a value of 22. This will not affect thefyeah
of themainx
method,so when you return tomainx
and accessfyeah
, you see 5.Ok. An int in java ( and really all languages that have strict data typing ) is a primitive data type. Its just a single variable of that data type. In java this means its passed by value to the method. So when you pass in the argument, a copy of the passed variable is created. Any operations that take place in the method act on that copy not the passed variable.
Actually in java EVERYTHING is passed by value but getting into the details of how that actually is true with what I am going to say next seems inadvisable.
With the array...its a collection of variables of the primitive data type int. So an entire copy of the array isn't actually made just a copy of the reference to the memory that stores the array. so yes the value of the int IN the array is changed from operations in the method.
In short methods don't change the external value of primitives (int,float,double,long,char) with the operations in the method, you have to return the resulting value of those operations to the caller if you wish to obtain it. Operations do change the value with most objects as well as with arrays of primitives. Hope that helps. Really unsure of how low level to get. Maybe someone else can clearly explain exactly why its by value. I "get" it but find it hard to help other people understand.
The
int
is a value type so 5 is passed directly intosmth
which can only modify the local copy. An array on the other hand is a reference type so the elements of that array can be modified.The
fyeah
variable in your first example contains a reference to an array (not an array), while thefyeah
integer in your second example contains an integer.Since Java passes everything by value the following will happen:
In the array case: A copy of the array reference will be sent, and the original array will be changed.
In the int case: A copy of the integer will be changed, and the original integer will not be changed.
It's because your int is a primitive and the method
smth
creates a local copy which is why it doesn't print the way you want. Objects are passed by value as well, but a value to the pointer in memory. So when it is changed, the pointer stays throughout both methods and you see the change. Read More Here