1

I have a method like this:

public boolean isDocumentsDeletedFormDB(String strDocumentsID) {
}

In this method, I wanted to return one Integer value as well. As there is already a return type as Boolean, how can I return Integer value as well.

Passing Wrapper Integer as a agreement is also not solving my problem.

Can any one suggest a solution with out using Collection in a return type.

3
  • 2
    According to standard programming practices, you'll have to redesign your program to avoid the necessity to return two values
    – manas
    Oct 1, 2012 at 6:06
  • What the Integer is supposed to do anyway? perhaps it would lead to some better ideas. Oct 1, 2012 at 6:18
  • I wanted to return how many file(s) are successfully deleted as a Integer value.
    – Nirdesh
    Oct 1, 2012 at 6:53

8 Answers 8

4

Firstly I must say that there is a problem with your method; a method must only do one thing and yours does two things (computes two values that you need to return). The proper solution would be to create another method which has the purpose of returning one of these values while the other is to be returned by the original method.

Anyway, if you really need to do this you have some options:

  1. Create a wrapper class that contains both the values as private fields and return from your method a value of this new class.
  2. Extract one of the returned object as a field variable which is going to be read by other needing methods (if it is only needed by private methods).
  3. If one of those returned types is an error status, use an exception.
1

The standard way to return multiple elements is to return an object encapsulating those elements.

For example:

public static class DeletedFromDBReturn {
  public final int integerValue;
  public final bool booleanValue;

  private DeletedFromDBReturn(int i, bool b) {
    integerValue = i; booleanValue = b;
  }
}

DeletedFromDBReturn sDocumentsDeletedFormDB(String strDocumentsID) {
  // ...
  return new DeletedFromDBReturn(intVal, boolVal);
}
0

You cannot return multiple values from a function in Java. You must either use a collection or a class or struct to contain the 2 values.

0

You will have to either define an object which has a Boolean and Integerfield and return that object, or else, if you can use external libraries, you can take a look at the Pair<L,R> Class offered by Apache Commons.

0

You could write a generic Pair<First,Second> class and return a Pair<Boolean,Integer>. However IMHO you should think about changing the method name as well since everybody expects a method named isSomething() to just return boolean.

0

You might want to use a simple String then, which can return both the types, but in a String form. In your calling code, then just check if it is a ("true" or "false") or not and parse it accordingly.

0

Create a Plain Old Java Object (POJO) class and return an instance of that. Your POJO class can be something like this:

public class MyPOJO{
    private boolean booleanValue;
    private Integer integerValue;


    public void setBooleanValue(boolean booleanValue){
        this.booleanValue = booleanValue
    }

    public void setIntegerValue(Integer integerValue){
        this.integerValue = integerValue
    }

    public boolean getBooleanValue(){
        return this.booleanValue;
    }

    public Integer getIntegerValue(){
        return this.integerValue;
    }
}

You would need to change your method signature and do something like this:

public MyPOJO isDocumentsDeletedFormDB(String strDocumentsID){
    /*
    * Your logic
    */

    MyPOJO pojo = new MyPOJO();
    pojo.setIntegerValue(/* Your integer value */);
    pojo.setBooleanValue(/* Your boolean value */);

    return pojo;
}
0

What you can do is change return type to int. Return -1 in case of false else return Valid integer.

From the method name I could guess that you use boolean return type to check whether document is deleted or not. And Integer you want to return will be number of documents deleted.

 public int isDocumentsDeletedFormDB(String strDocumentsID) {}

so you can check in function which uses this function as

   if(isDocumentsDeletedFormDB(str)==-1)
    //Do that you do when it is false.
4
  • 1
    That is old C practice and no good programming style in Java. Oct 1, 2012 at 6:08
  • but we don't know whats the purpose of the two return values
    – manas
    Oct 1, 2012 at 6:08
  • From the method name I could guess as whether document is deleted or not. Oct 1, 2012 at 6:10
  • I think this is the best soluation which i need.
    – Nirdesh
    Oct 1, 2012 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.