Solved

Passing integer values by reference?

Posted on 1998-08-12
2
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

623 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