Riskhan
Riskhan

Reputation: 4470

passing reference as parameter in android

I am newbie in java/android. I am a c/c++ developer. May i know how to pass a reference as parameter in android. An illustrative c sample code shown below

void main()
{
  int no1 = 3, no2 = 2, sum = 0;
  findsum( no1, no2, sum );
  printf("sum=%d", sum );
}

void findsum( int no1, int no2, int& sum )
{
  sum = no1 + no2;
}

please suggest me a solution

thanks

Upvotes: 3

Views: 8843

Answers (3)

Bradley Willcott
Bradley Willcott

Reputation: 1

This is how I solved this problem:

// By using a wrapper class.
// This also works with Class objects.
class IntReturn {
    public int val;
}

// For example:
class StringReturn {
    public String val;
}

class Main {
    public static void main (){
        IntReturn iRtn = new IntReturn();
        
        if(tryAdd(2, 3, iRtn)){
            System.out.println("tryAdd(2, 3): " + iRtn.val);
        }
    }

    public static boolean tryAdd(final int a, final int b, final IntReturn iRtn){
        iRtn.val = a + b;

        return true;  // Just something to use return
    }
}

I have a personal library of these types of classes for this purpose. Lambdas are another reason for these classes, as you need to have a 'final' variable to get a value out of a Lambda, other than by return statement.

Update:
I have replaced all of my specific wrapper classes with a single Generic class:

public final class Ref<T>
{
    /**
     * The object being held.
     */
    public T val;

    /**
     * This constructor is <b>private</b> to prevent instantiation by external
     * clients.
     * <p>
     * This class should <i>only</i> be instantiated through one of the factory
     * methods:
     * <ul>
     * <li>{@link  #val()}</li>
     * <li>{@link  #val(Object) val(T val)}</li>
     * </ul>
     */
    private Ref()
    {
        this.val = null;
    }

    /**
     * This factory method instantiates an empty Ref object.
     *
     * @param <T> type of object.
     *
     * @return a new instance of the Ref class.
     */
    public static <T> Ref<T> val()
    {
        return new Ref<>();
    }

    /**
     * This factory method instantiates a Ref object containing {@code val}.
     *
     * @param <T> type of object.
     * @param val the object to be held
     *
     * @return a new instance of the Ref class, initialized with the 'val'
     *         object.
     */
    public static <T> Ref<T> val(T val)
    {
        Ref<T> rtn = new Ref<>();
        rtn.val = val;
        return rtn;
    }

    /**
     * Reset 'val' to {@code null}.
     */
    public void clear()
    {
        this.val = null;
    }

    /**
     * Check to see if this Ref object has not yet been set to a value.
     *
     * @return {@code true} if it hasn't been set to a value, {@code false}
     *         otherwise.
     */
    public boolean isEmpty()
    {
        return this.val == null;
    }

    /**
     * Check to see if this Ref object has been set to a value.
     *
     * @return {@code true} if it has been set to a value, {@code false}
     *         otherwise.
     */
    public boolean isPresent()
    {
        return this.val != null;
    }

    /**
     * Returns a string representation of the object.
     *
     * @implSpec
     * This implementation returns a string consisting of the default conversion
     * to a string, of the object held in {@code val}. This is achieved by
     * calling its {@code toString()} method. If {@code val} is empty, then the
     * empty string is returned: "".
     *
     * @return a string representation of the object.
     */
    @Override
    public String toString()
    {
        return "" + this.val;
    }
}

Here is how it can be used:

 ...
     Ref<Integer> iRtn = Ref.val();

     if(add(2, 3, iRtn)){
         System.out.println("2 + 3 = " + iRtn.val);
     }
 ...

 public boolean add(final int a, final int b, final Ref<Integer> iRtn){
     iRtn.val = a + b;

     return iRtn.val > 0;  // just something to use up return
 }

It may seem as though this would be redundant since I had all of the previously developed wrapper classes. However, now I have only one generic class that can hold anything!

Upvotes: 0

newacct
newacct

Reputation: 122489

There is no pass by reference in Java.

Upvotes: 3

onof
onof

Reputation: 17367

You cannot pass an int as reference in Java. int is a primary type, it can be passed only by value.

If you still need to pass an int variable as reference you can wrap it in a mutable class, for example an int array:

void findsum( int no1, int no2, int[] sum )
{
  sum[0] = no1 + no2;
}

Anyway, I strongly suggest you to refactor your code to be more object oriented, for example:

class SumOperation {
   private int value;

   public SumOperation(int no1, int no2) {
      this.value = no1 + no2;
   }

   public int getReturnValue() { return this.value; }
}

Upvotes: 7

Related Questions