aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes can anyone explain this... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic
Author

can anyone explain this...

saritha cheler
Greenhorn

Joined: Nov 15, 2000
Posts: 13
1. public class PrimitiveReferences{
2. public static void main(String args[]){
3. int[] muValue = {1};
4. modifyIt(myValue);
5. System.out.println("myValue contians" + myValue[0]);
6. }
7. public static void modifyIt(int[] value){
8. value[0]++;
9. }
10.}

At line 5 it prints 2. How can it be possible when the incrementation of the value is till that method. It should print 1 at line 5. Can anybody explain this y it prints 2 at line 5.

thanks<BR>saritha.
Kathy Rogers
Ranch Hand

Joined: Aug 04, 2000
Posts: 103
Saritha,
At line 3, you set myArray[0] = 1. Then, at line 4, you call the function modifyIt(). Because you're passing it an array, you actually modify the array itself. So, modifyIt adds 1 to myArray[0] which now equals 2. It doesn't matter here whether you use value[0]++ or ++value[0}. Then control returns to the main function. Line 5 is now executed - and the value 5 is printed out. And the program is finished.
Hope this helps,
Kathy
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Closing this thread .. see duplicate post
http://www.javaranch.com/ubb/Forum24/HTML/007481.html
------------------
Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can anyone explain this...