Returning an array in java is actually simple: just return the reference to the array. In C++, this will not work. In Java, in this context, a reference is a variable name. The array construction literal can also be returned. When the array is returned, it should be received by an array variable.
This tutorial illustrates how to return a variable array reference and how to return an array construction literal reference.
Returning a Variable Reference
Remember that a method in a class has to return the array to return an array to return. An example of such a method is:
return ar;
}
This method has just one parameter, which is an array declaration. Its return type is an array declaration, which is obligatory. This method has just one statement to keep things simple. The one statement just returns the array.
Simple Program to return Array Variable Reference
The following program shows a simple program to return an array variable reference:
public char[] mthd(char[] ar) {
return ar;
}
}
public class TheClass {
public static void main(String[] args) {
char[] arr = new char[] {'R', 'S', 'T', 'U', 'V'};
AClass obj = new AClass();
char[] arra = obj.mthd(arr);
for (int i=0; i<arra.length; i++) {
System.out.print(arra[i]); System.out.print(' ');
}
System.out.println();
}
}
The output is:
There are two classes in the program: the main class called, TheClass and a secondary class called AClass. There are two principal code segments in the program. AClass is one of the code segments, and TheClass is the other code segment.
AClass begins with the reserved word, class. Then there is the class name, AClass. Inside the braces for the AClass, is the public method with the name mthd. If the method is not public, it will not be seen in the main method of the main class. mthd() returns the argument for the parameter it receives. The method has only one parameter.
After the AClass definition in the code is the main class definition. The name of the main class is TheClass. It is public, and it has to be public. When the public modifier for a class definition is omitted, the class is private. Similarly, when the modifier for a method definition is omitted, the method is private. The main class here has only the main method, which should be there.
The main method has to be public; it must be static; and it must return void.
The first statement in the main method declares the array, arr, with initialization of the array in the declaration.
The second statement creates an object for the AClass, called obj. The statement after uses the instantiated object, obj to call the method, mthd(), passing as an argument to the method, arr. If the method definition in the class, AClass, was not public, this statement would not execute. mthd() of obj returns the reference of the array.
The return reference is received by the declared array variable, arra, in “char[] arra”. After that is the for-loop, which prints out the received array content.
In this way, an array has been returned by a method.
Returning a Constructor Array Literal
Remember, that in order to return an array, a method in a class has to return the array. An example of such a method, is:
return new char[] {'R', 'S', 'T', 'U', 'V'};
}
This method has no parameter. Its return type is an array declaration, which is obligatory. The return statement returns a construction array literal reference, which is “new char[] {‘R’, ‘S’, ‘T’, ‘U’, ‘V’}”. This method has just one statement, just to keep things simple.
Simple Program to return Constructor Array Literal Reference
The following program shows a simple program to return a constructor array literal reference:
public char[] mthd() {
return new char[] {'R', 'S', 'T', 'U', 'V'};
}
}
public class TheClass {
public static void main(String[] args) {
AClass obj = new AClass();
char[] arra = obj.mthd();
for (int i=0; i<arra.length; i++) {
System.out.print(arra[i]); System.out.print(' ');
}
System.out.println();
}
}
The output is:
There are two classes in the program: the main class called, TheClass and a secondary class called AClass. There are two principal code segments in the program. AClass is one of the code segments, and TheClass is the other code segment.
AClass begins with the reserved word, class. Then there is the class-name, AClass. Inside the braces for the AClass, is the public method with the name mthd. If the method is not public, it will not be seen in the main method of the main class. mthd() returns the constructor array literal reference. The method optionally has no parameter.
After the AClass definition in the code is the main class definition. The name of the main class is TheClass. It is public, and it has to be public. When the public modifier for a class definition is omitted, the class is private. Similarly, when the modifier for a method definition is omitted, the method is private. The main class here has only the main method, which should be there.
The main method has to be public; it must be static; and it must return void.
The first statement in the main method creates an object for the AClass, called obj. The statement after uses the instantiated object, obj to call the method, mthd(), passing no argument, since the invoked method definition has no parameter. If the method definition in the class, AClass, was not public, this statement would not execute. mthd() of obj returns a constructor array literal reference.
The return reference is received by the declared array variable, arra, in “char[] arra”. After that is the for-loop, which prints out the received array content.
In this way, a constructor array literal reference has been returned by a method.
Conclusion
To return an array in java is actually simple: just return the reference to the array. In C++, this will not work. In Java, a reference is a variable name or the array construction literal reference in this context. When the array is returned, it should be received by an array variable.