The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Incrementor operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Incrementor operator" Watch "Incrementor operator" New topic
Author

Incrementor operator

janne s
Ranch Hand

Joined: Sep 21, 2004
Posts: 81
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

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Karthik Mohan
Greenhorn

Joined: Sep 29, 2004
Posts: 23
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

Joined: Sep 21, 2004
Posts: 81
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Incrementor operator