Output Parameters in Java

后端 未结 8 839
夕颜
夕颜 2020-12-21 05:36

With a third party API I observed the following.

Instead of using,

public static string getString(){
   return \"Hello World\";
}

i

相关标签:
8条回答
  • 2020-12-21 06:17

    in my opinion, this is useful when you have more than one result in a function.

    0 讨论(0)
  • 2020-12-21 06:18

    Actually, it is impossible to have out parameters in java but you can make a work around making the method take on a de-reference for the immutable String and primitives by either writing a generic class where the immutable is the generic with the value and setter and getter or by using an array where element 0 (1 in length) is the value provided it is instantiate first because there are situations where you need to return more than one value where having to write a class just to return them where the class is only used there is just a waste of text and not really re-usable.

    Now being a C/C++ and also .Net (mono or MS), it urges me that java does not support at least a de-reference for primitives; so, I resort to the array instead.

    Here is an example. Let's say you need to create a function (method) to check whether the index is valid in the array but you also want to return the remainding length after the index is validated. Let's call it in c as 'bool validate_index(int index, int arr_len, int&rem)'. A way to do this in java would be 'Boolean validate_index(int index, int arr_len, int[] rem1)'. rem1 just means the array hold 1 element.

    public static Boolean validate_index(int index, int arr_len, int[] rem1)
    {
        if (index < 0 || arr_len <= 0) return false;
    
        Boolean retVal = (index >= 0 && index < arr_len);
    
        if (retVal && rem1 != null) rem1[0] = (arr_len - (index + 1));
    
        return retVal;
    
    }
    

    Now if we use this we can get both the Boolean return and the remainder.

     public static void main(String[] args)
     {
        int[] ints = int[]{1, 2, 3, 4, 5, 6};
        int[] aRem = int[]{-1};
        //because we can only scapegoat the de-ref we need to instantiate it first.
        Boolean result = validate_index(3, ints.length, aRem);
    
        System.out.println("Validation = " + result.toString());
        System.out.println("Remainding elements equals " + aRem[0].toString());
    
     }
    

    puts: Validation = True puts: Remainding elements equals 2

    Array elements always either point to the object on the stack or the address of the object on the heap. So using it as a de-references is absolutely possible even for arrays by making it a double array instantiating it as myArrayPointer = new Class[1][] then passing it in because sometimes you don't know what the length of the array will until the call going through an algorithm like 'Boolean tryToGetArray(SomeObject o, T[][] ppArray)' which would be the same as in c/c++ as 'template bool tryToGetArray (SomeObject* p, T** ppArray)' or C# 'bool tryToGetArray(SomeObject o, ref T[] array)'. It works and it works well as long as the [][] or [] is instantiate in memory first with at least one element.

    0 讨论(0)
提交回复
热议问题