Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Incrementor operator

 
janne s
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

can anyone explain me why incremented i value is not printed where as array value is incremented???

public class TestClass
{
public static void main(String args[ ] )
{
int i = 1;
int[] iArr = {1};

incr(i) ;
incr(iArr) ;

System.out.println( "i = " + i + " iArr[0] = " + iArr [ 0 ] ) ;
}
public static void incr(int n ) { n++ ; }
public static void incr(int[ ] n ) { n [ 0 ]++ ; }
}


output: i=1 iArr[0]=2
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jayasree,

Welcome to Javaranch

We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.

With regard to your question, try reading this.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether passed as references to objects or as primitive values, method arguments are copies. And once passed, these copies are local to the method.

The method incr(int n) is given a copy of the value i, which is assigned to a local variable n. The operation is performed on the local variable n, leaving the outside variable i untouched.

In contrast, the method incr(int[] n) is given a copy of a reference to an object (an int Array). But it doesn't matter whether we're using the original reference or a copy of that reference; because either way, we're still pointing to the same object. Thus, the method is able to modify fields in the outside object (or, in this case, elements in the Array).

[ September 29, 2004: Message edited by: marc weber ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of it this way - variables are like peices of paper with data on them. when you pass them into a method, you photocopy the page and the photocopy is used in the method.

now, with primitives, the important info is on the piece of paper. if you give me a copy, i can erase the old data and put in new data - your info doesn't change.

but with OBJECTS (and your iArr is an Array object), the piece of paper tells me where the real document is filed. you photocopy the page and give it to me. Your line "n[0]++ ;" says "go to the real document, find the 0th element, and update it."

i now go and change the document, and throw away my reference to it since i'm done.

now you, with your reference paper, want to print something. you go to the real place, open the file, and you're gonna see my changes.
 
Karthik Mohan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jayasree:

public class TestClass
{
public static void main(String args[ ] )
{
int i = 1;
int[] iArr = {1};

incr(i) ;
incr(iArr) ;

System.out.println( "i = " + i + " iArr[0] = " + iArr [ 0 ] ) ;
}
public static void incr(int n ) { n++ ; }
public static void incr(int[ ] n ) { n [ 0 ]++ ; }
}


output: i=1 iArr[0]=2


Arrays are passed by reference to methods and variables are passed by value. So the variable "i" is passed by value and the modifications done inside the function "incr" does not affect the value of "i" in main. In the case of arrays, as they are passed by reference, the changes made in incr are reflected in main.

hope this helps

[wooh ... before i composed my reply, there were 2 replies posted ..!!!]
[ September 29, 2004: Message edited by: Karthik Mohanasundaram ]
 
janne s
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

This was my first post. i'm happy with replies...

I understood the concept clealy.

Thankyou to all of you...marc weber,fred rosenberger, Karthik Mohanasundaram

Have a good day!
[ September 30, 2004: Message edited by: jaya merugu ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic