jQuery in Action, 3rd edition
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic

can anyone explain this...

saritha cheler

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. }

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.

Kathy Rogers
Ranch Hand

Joined: Aug 04, 2000
Posts: 104
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,
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Closing this thread .. see duplicate post
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...
It's not a secret anymore!