Solved

Passing integer values by reference?

Posted on 1998-08-12
2
154 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
Comment Utility
(... 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
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 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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now