• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in understanding local variable and array  RSS feed

 
Jahangir Alam
Greenhorn
Posts: 15
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please consider the following codes:


Output: The value of a= 0;

I understand that the value is printed here is of the local a of the main method-- not the value of incremented a of method modify.

But.....

output: The value of a[0]= 1.

Here the incremented a[0] is printed that is performed in method modify. I expected the value of a[0]= 0 as I thought the default value of array a[0] is 0 and we printed a[0] in the main method.

Please explain it.
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indded defualt value of int array is 0 but in your modfiy method aren't you incrementing the first member of array?

Manish
 
Jahangir Alam
Greenhorn
Posts: 15
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manish ghildiyal wrote:Indded defualt value of int array is 0 but in your modfiy method aren't you incrementing the first member of array?

Manish


I know the incremented value of a[0] is printed. But look at my first coding. There a=0 is printed. The incremented value of a is not being printed as that incremented a is the local a of modify method but we printed the a of main method.

So what is the difference?
 
Vivek Kr Agrawal
Ranch Hand
Posts: 51
Eclipse IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jahangir Alam,
No doubt you made a very good point. The Java Spec says that everything in Java is pass-by-value. There is no such thing as "pass-by-reference" in Java.
But we need to understand that it doesn't exactly work in that way especially in case of objects. We can assume that java passes the reference of the object or objects as references passed by value.
For Example considering your code:


Lets see what others have to say about this.
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

....as everything in java is pass-by-value, so here it means that you when you pass argument
then it is copied. But as 'a' is reference, so what we get by copying also refers to very same object referred by 'a'.So
if you do any oepration inside method then it impacts the object referred by original reference 'a'.

Manish
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
..additionally

in your first case you are passing a primitive and not a reference...so when copying of
argument happens then you get two different entities which are in no way connected.So
modifying one doesn't impact other in any way.

Manish
 
Arjun Narsipur
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manish ghildiyal wrote:..additionally

in your first case you are passing a primitive and not a reference...so when copying of
argument happens then you get two different entities which are in no way connected.So
modifying one doesn't impact other in any way.

Manish


So Manish

If I understand correctly are you saying that in the following code






the value of a++ is different from value of a? In which Case we have a value for a and another for a++ stored in two different locations? But doesn't a++ mean increment the value of "a" by 1 and store it in "a"?

Thanks for clarifying.

 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arjun,

What I mean to say is that variable a which you pass as argument (to modify method)
in main method is copied when modfiy method is invoked...so variable which is actually modified inside modify method is not the variable a of main method, its just
a copy of it...so inside modify method you are not incrementing variable a of main method but rather a copy of it which is local to modiy method.
So variable a of main method reamains unincremented.

Manish
 
Arjun Narsipur
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manish ghildiyal wrote:Hi Arjun,

What I mean to say is that variable a which you pass as argument (to modify method)
in main method is copied when modfiy method is invoked...so variable which is actually modified inside modify method is not the variable a of main method, its just
a copy of it...so inside modify method you are not incrementing variable a of main method but rather a copy of it which is local to modiy method.
So variable a of main method reamains unincremented.

Manish


I think I got it. Is it now correct to generalise this and say that if i pass a primitive (and not a reference) to any method, the method actually works on a copy (BY VALUE!) of that variable and not on the original?

Regards
Arjun
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I got it. Is it now correct to generalise this and say that if i pass a primitive (and not a reference) to any method, the method actually works on a copy (BY VALUE!) of that variable and not on the original?



....in java its pass-by-value only, whether primitives or reference types.
Behaviour 'seems' to differ because reference points to an object in the memory. So if I have a method
which takes reference as argument then it means when reference is passed to it then passed reference
is copied. So now we have two references(one which is passed as argument and one which is created by
copying passed one) which have different locations in memory but they are pointing
to same object. So if you try to call methods on any one of these two references then method is called on
same object, hence modifying very same object.
Its like you have a ball and you have two sticks which you can use to move the ball. No matter which of
the two sticks you use same ball is moved.

Manish
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another aspect of what is going on.

The method hack gets the value (the String reference) copied from the variable s of the method main into its own, different one (albeit of the same name).
If it methods reassigns it, the own variable gets reassigned: the original variable retains the value.



$ java a
foo
bar
foo
 
Jahangir Alam
Greenhorn
Posts: 15
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all for your very helpful posts! It's now completely clear to me.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have always thought of variables in java as like being pieces of paper with stuff written on them. When you pass a variable into a method, you make a copy of the piece of paper, and hand it to someone else.

Now, if I pass in a primitive, say with a value of 5, then I (let's say I am the calling method) have a copy with the number 5, and I give you (the method that got called) a new piece of paper with the number 5 on it. If you erase your number '5' and write the number '6' on your piece of paper, it has NO impact on my piece of paper with the number 5 on it. So when I come back and look at what I have written, I still see 5.

Now....let's assume we are passing an object (and an array IS and object). What the calling method really has is not the actual object, but effectively the ADDRESS of the object. I use that address to find the object when I need to do something to/with it.

So...when a method is called, it is handed a piece of paper with the address. If that method then says "go to that address and paint the bedroom blue", no problem. But when the calling method them comes back and says "what color is the bedroom at this address", it WILL see the change.

 
Arjun Narsipur
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all. Its clear to me me except for one nagging doubt which I have, i thought about it a lot but still can't figure it out.
Lets say i pass a primitive integer variable named s (holding value 5) to a method. Now the method gets a copy of the variable and and let's say it increments it to 6 (by using s++) . We now have two variables (the original one and one with the method) both named s but one with value 5 and the other with value 6. is this allowed?
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We now have two variables (the original one and one with the method) both named s but one with value 5 and the other with value 6. is this allowed?


They are variables in different scopes.

Manish
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!