File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java passing variables by reference?

 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a way to do this in java, like the following c++ code
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13047
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not the way you coded it. If you want to manipulate a bunch of int primitives and return the values, pass an array of int (a reference to an array object.)
Bill
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you are using a single method to process three ints, then we can assume those three ints are related in some way. If that is the case then they should be in a class.


This class should have a method called duplicate (double would be a better name) that doubles the value of each of the ints


Then your main method looks like this

If the three ints are not related in any way, then it is probably a bad design to have a single method processing them.
[ August 15, 2006: Message edited by: Joanne Neal ]
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you can't do that. Everything in Java is passed by value.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While William's solution using an array would work, I find it to be a rather awkward workaround.

A goal of OO design is to put the data, and the operations on the data together in a class. Joanne's solution is one of the possible ways to do that.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic