[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Return 2 int values

Posted on 2007-10-02
Medium Priority
Last Modified: 2012-05-05
How to return the two integer values in Java terms

int getnumber(int a ,int b)
       number1 = a;
       number2 = b;
       //return new int[]{number1,number2};
       //return number1 , number2;
Question by:gauravflame
LVL 86

Assisted Solution

CEHJ earned 400 total points
ID: 19997024
int[] getnumber(int a ,int b) {
    return new int[] { 1, 2};

Author Comment

ID: 19997033
int[] getnumber(int a ,int b) {

it doesn't mean we are returning the two int arrays , not two integers
LVL 13

Accepted Solution

Bart Cremers earned 600 total points
ID: 19997038
Use an int[] or create a value object to return

ValueObject getNumber(int a, int b) {

    return new ValueObject(a, b);

class ValueObject {
    int a;
    int b;

    ValueObject(int a, int b) {
        this.a = a;
        this.b = b;

    int getA() {
        return a;
Technology Partners: 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!

LVL 86

Expert Comment

ID: 19997042
>>it doesn't mean we are returning the two int arrays , not two integers

It means you're returning two ints in an array
LVL 13

Expert Comment

by:Bart Cremers
ID: 19997045
int[] means returning a single int array containing an unspecified number of ints.
LVL 92

Expert Comment

ID: 19997089
a bean as Bart_Cr suggests is typically the way to go.
using an array is somewhat error prone
LVL 86

Expert Comment

ID: 19997114
>>using an array is somewhat error prone

In what way?

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month19 days, 15 hours left to enroll

872 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