Solved

Passing integer values by reference?

Posted on 1998-08-12
2
190 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

751 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