Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Passing integer values by reference?

Posted on 1998-08-12
2
Medium Priority
?
204 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 100 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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

824 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