Solved

Passing integer values by reference?

Posted on 1998-08-12
2
184 Views
Last Modified: 2010-04-16
Okay, okay, I know you can't really pass anything by reference in Java -- or, more precisely, that every time you pass an Object of any kind you're passing a reference. Well, that's all fine and well, except I want to pass two integers into a method that changes them and sends back the changes -- and as far as I can tell, the Integer object isn't mutable and there isn't any setValue() that I can use. I could, of course, wrap the two integers in a new Object type, but I find them cumbersome and ridiculously ungraceful, especially since the only thing's I'll be doing with this object is setting and getting one of two ints.

Any other less kludgy workarounds would be much appreciated.
0
Comment
Question by:HoJu
2 Comments
 

Author Comment

by:HoJu
ID: 1232471
(... um, change that sentence above to "I could, of course, wrap the two integers in a new Object type, but I find _the idea_ cumbersome and ridiculously ungraceful ..." I actually have made my own classes before. Honest.)
0
 
LVL 4

Accepted Solution

by:
evijay earned 50 total points
ID: 1232472
pass an array. Arrays are passed by reference



public class testit {

void myfunc(int[] results)
{
   results[0] = 10;
   results[1] = 20;
}

public static void main(String args[])
{
    int[] res = new int[2];
    myfunc(res);
    System.out.println(res[0] + " " + res[1]);
}
}
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 69
login form jsp example 2 39
Java syntax, or is it Selenium 6 30
American Express @Work site and Java 4 33
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question