Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Method parameters doubt?

 
Ranch Hand
Posts: 10192
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 2410
  • Mark post as helpful
  • send pies
  • 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 Harry
Ranch Hand
Posts: 10192
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the answer, arrays are passes by reference values.

Thanks keith.
 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • 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
  • 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..
 
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic