Return 2 int values

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;
   }
   
gauravflameAsked:
Who is Participating?
 
Bart CremersConnect With a Mentor Java ArchitectCommented:
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;
    }
    ...
}
0
 
CEHJConnect With a Mentor Commented:
int[] getnumber(int a ,int b) {
    return new int[] { 1, 2};
}
0
 
gauravflameAuthor Commented:
int[] getnumber(int a ,int b) {

int[]
it doesn't mean we are returning the two int arrays , not two integers
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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

It means you're returning two ints in an array
0
 
Bart CremersJava ArchitectCommented:
int[] means returning a single int array containing an unspecified number of ints.
0
 
objectsCommented:
a bean as Bart_Cr suggests is typically the way to go.
using an array is somewhat error prone
0
 
CEHJCommented:
>>using an array is somewhat error prone

In what way?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.