Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Pass by Reference Method  RSS feed

 
Anjanesh Lekshminarayanan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Is it possible to create a method to have one parameter pass by reference instead of by value ?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there are no reference parameters in Java. If you really want to, you can use a one-element array as a way to pass a changeable primitive -- i.e.,

public String foo(String a, int[] b) { b[0] = 7; }
 
Anjanesh Lekshminarayanan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case why isnt this working.

Output is

I understand Err is a copy of the reference b, but Im changing the reference's content which should propagate in main().

EDIT : It works.
[ December 09, 2007: Message edited by: Anjanesh Lekshminarayanan ]
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I try it, it prints

which is what you'd expect since the end index in the replace method is exlusive.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!