• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Method parameters doubt?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Consider the code below,


The above code prints 1, 3

And the code below,



prints 3, 1

Can you guys please explain this?

Thanks in advance.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:
Hi Guys,

Consider the code below,


The above code prints 1, 3

And the code below,



prints 3, 1

Can you guys please explain this?

Thanks in advance.



Notice in the first code that there is nothing within the method that has any effect on the array references sent to it.

In the second code, the elements of the arrays are actually swapped.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the answer, arrays are passes by reference values.

Thanks keith.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, all parameters are sent by value.

When you use an array reference (or any other reference) as a parameter to a method, what gets sent to the method is a copy of the reference.

So that means when a method begins executing, the formal parameters of the method point to the same objects that the actual parameters do.

However, if you change where the formal parameter points, that does not change anything about where the actual parameter points.
[ December 06, 2006: Message edited by: Keith Lynn ]
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Frenz..

Am also confused that java is pass by reference but its not...it is by PASS BY VALUE

Explanation:

when we pass parameters to a method...we are actually passing the "COPY OF REFERENCE VALUE i.e in bits"
so whatever the variable we are just passing the size(in bits) of that type.

Harish..
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic