By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,651 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,651 IT Pros & Developers. It's quick & easy.

How to return multiple values in java in below case

P: 1
Please tell me how to return two values in java

Expand|Select|Wrap|Line Numbers
  1. public byte isFirstvalue(String A,
  2.                               String B, String C, Date date) throws Exception {
  3.         CallableStatement cs = null;
  4.         Connection connection = null;
  5.         ResultSet rsResult = null;
  6.         byte isFirstvalue= -1;
  7.         byte isSecondvalue = -1;
  8.         try {
  9.             connection = getConnection();
  10.             cs = connection.prepareCall("{ call IsProc(?,?,?,?) }");
  11.             cs.setString(1, A);
  12.             cs.setString(2, B);
  13.             cs.setString(3, C);
  14.             cs.setDate(4, date);
  15.             rsResult = cs.executeQuery();
  16.             if(rsResult.next()){
  17.                 isFirstvalue= rsResult.getByte("IsFirstvalue");
  18. isSecondValue = rsResult.getByte("IsSecondValue ");
  19.             }
  20.         }
  21.         catch (Exception ex) {
  22.             String errorMessage = ex.getMessage();
  23.             log.error(errorMessage);
  24.             throw ex;
  25.         }
  26.         finally {
  27.             closeResultSet(rsResult);
  28.             closeStatement(cs);
  29.             closeConnection(connection);
  30.         }
  31.         return isFirstValue ;
  32.         return isSecondValue 
  33.  
  34.     }
Sep 11 '13 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 785
You have 3 options:
1.)
return an object array:
Expand|Select|Wrap|Line Numbers
  1. return new Object[] {new Integer(isFirstValue), new Integer(isSecondValue)};
2.)
return an instance of an inner class:
Expand|Select|Wrap|Line Numbers
  1. private static class Results{} {
  2.    public final int isFirstValue;
  3.    public final int isSecondValue;
  4.    private Results(int isFirstValue, int isSecondValue) {
  5.       this.isFirstValue = isFirstValue;
  6.       this.isSecondValue = isSecondValue;
  7.    }
  8. }
Then return by:
Expand|Select|Wrap|Line Numbers
  1. return new Results(isFirstValue , isSecondValue);
If "results" would be the returned object, you can access the two results with "results.isFirstValue" and "results.isSecondvalue". I agree that it's a lot of boilerplate code to write, but the advantage is: more clear than accessing by index, for example "object[1]"; it can be inlined by compiler; no in/outboxing.

3.)
For fastest speed, put both integers (32 bits) together in a long (64 bits) and return that long. The lower 32 bits of the long represents the first integer and the higher 32 bits of the long represents the second integer. Use shifting and bit functions to extract the original integers from the long. (For example "originalInteger = longresult & 0xFFFF" returns the first integer.

I assume from the name "isFirstValue" that you really want to store a boolean and not an integer. If that is the case, then you can put both values in an integer, using the first bit for the first boolean and the second bit for the second boolean. then you can retrieve them later by a simple bit-test.
Example:
Expand|Select|Wrap|Line Numbers
  1. int isFirstValueBit = (isFirstValue ? 0 : 1);
  2. int isSecondValueBit = (isSecondValue ? 0 : 2);
  3. int resultInt = isFirstValue + isSecondvalue;
  4. return resultInt;
  5. ...
  6. if (resultInt & 1 != 0) then System.out.println("isFirstValue was set");
  7. if (resultInt & 2 != 0) then System.out.println("isSecondValue was set");
Sep 12 '13 #2

Nepomuk
Expert 2.5K+
P: 3,112
Actually, if you want to use Apache Commons, you can use something similar to the second solution mentioned by chaarmann without the boilerplate code: The Pair class. Here's an example:
Expand|Select|Wrap|Line Numbers
  1. import org.apache.commons.lang3.tuple.Pair;
  2. //...
  3. Pair<Integer, String> getTwoValues() {
  4.   return Pair.of(1, "two");
  5. }
  6.  
  7. void useThePair() {
  8.    Pair<Integer, String> pair = getTwoValues();
  9.    System.out.println("The first value is " + pair.getLeft());
  10.    System.out.println("The second value is " + pair.getRight());
  11. }
That should be typesafe compared to the array of Objects, shorter than the boilerplate code solution and easier to read than the 64-bit integer. Of course, depending on your problem it might still not be the best solution.
Sep 12 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.