How to return multiple values in java in below case

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • itprof
    New Member
    • Sep 2013
    • 1

    How to return multiple values in java in below case

    Please tell me how to return two values in java

    Code:
    public byte isFirstvalue(String A,
                                  String B, String C, Date date) throws Exception {
            CallableStatement cs = null;
            Connection connection = null;
            ResultSet rsResult = null;
            byte isFirstvalue= -1;
            byte isSecondvalue = -1;
            try {
                connection = getConnection();
                cs = connection.prepareCall("{ call IsProc(?,?,?,?) }");
                cs.setString(1, A);
                cs.setString(2, B);
                cs.setString(3, C);
                cs.setDate(4, date);
    	        rsResult = cs.executeQuery();
    	        if(rsResult.next()){
    	        	isFirstvalue= rsResult.getByte("IsFirstvalue");
    isSecondValue = rsResult.getByte("IsSecondValue ");
    	        }
            }
            catch (Exception ex) {
                String errorMessage = ex.getMessage();
                log.error(errorMessage);
                throw ex;
            }
            finally {
            	closeResultSet(rsResult);
                closeStatement(cs);
                closeConnection(connection);
            }
            return isFirstValue ;
            return isSecondValue 
            
        }
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    You have 3 options:
    1.)
    return an object array:
    Code:
    return new Object[] {new Integer(isFirstValue), new Integer(isSecondValue)};
    2.)
    return an instance of an inner class:
    Code:
    private static class Results{} {
       public final int isFirstValue;
       public final int isSecondValue;
       private Results(int isFirstValue, int isSecondValue) {
          this.isFirstValue = isFirstValue;
          this.isSecondValue = isSecondValue;
       }
    }
    Then return by:
    Code:
    return new Results(isFirstValue , isSecondValue);
    If "results" would be the returned object, you can access the two results with "results.isFirs tValue" and "results.isSeco ndvalue". 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 "originalIntege r = longresult & 0xFFFF" returns the first integer.

    I assume from the name "isFirstVal ue" 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:
    Code:
    int isFirstValueBit = (isFirstValue ? 0 : 1);
    int isSecondValueBit = (isSecondValue ? 0 : 2);
    int resultInt = isFirstValue + isSecondvalue;
    return resultInt;
    ...
    if (resultInt & 1 != 0) then System.out.println("isFirstValue was set");
    if (resultInt & 2 != 0) then System.out.println("isSecondValue was set");

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      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:
      Code:
      import org.apache.commons.lang3.tuple.Pair;
      //...
      Pair<Integer, String> getTwoValues() {
        return Pair.of(1, "two");
      }
      
      void useThePair() {
         Pair<Integer, String> pair = getTwoValues();
         System.out.println("The first value is " + pair.getLeft());
         System.out.println("The second value is " + pair.getRight());
      }
      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.

      Comment

      Working...