Java Method Returning a Value
Java Object Oriented Programming Core – Part 7
Forward: In this part of the series, I discuss the return data type of a Java method.
By: Chrysanthus Date Published: 3 Sep 2012
Introduction
Simple Method
Consider the following example:
class MyClass
{
void aMeth()
{
System.out.println("I am alive!");
}
}
class Return1
{
public static void main(String[] args)
{
MyClass myObj = new MyClass();
myObj.aMeth();
}
}
The method of interest here is the one with the name, aMeth(). This method declaration commences with the reserved word, void. This reserved word is an example of a modifier. It means that the method will return nothing. Do not confuse between a return method and a print statement. What you have in the method is a print statement and not a return statement. Because of "void", the method does not have a return statement. In the past, this kind of method declaration in some languages was called, a procedure (a method that does not return anything).
An int Return Example
Consider the following program (code):
class MyClass
{
int aMeth(int no)
{
//statements
return no;
}
}
class Return2
{
public static void main(String[] args)
{
MyClass myObj = new MyClass();
int num = myObj.aMeth(81);
System.out.println(num);
}
}
The declaration of the method of interest begins with, int. This is another example of a modifier. The position of int in the declaration of the method means that the method has to return an int data type (e.g. 81). In the method declaration, there is a retrun statement that returns the value of the local variable, no. Down in main, this return value is assigned to the local variable, num. Read and try the above code.
You do not only have to return a value in the form of a variable. You can return a literal. In the following program, the first method returns a double literal; the second method returns a string Literal. Read and try the code:
class MyClass
{
double aMeth1(double no)
{
//statements
return no;
}
String aMeth2(String stri)
{
//statements
return stri;
}
}
class Return3
{
public static void main(String[] args)
{
MyClass myObj = new MyClass();
double num = myObj.aMeth1(55.33);
String str = myObj.aMeth2("The U.S.A is the greatest nation, but it is slowly being challanged by China!");
System.out.println(num);
System.out.println(str);
}
}
Expression as return Argument
You can have an expression as the return argument (value). The resulting value or reference of the expression is what is returned. In the method declaration, it is the resulting value of the expression that is written in front, as the return data type. Read and try the following code that illustrates this:
class TheClass
{
int propInt = 5;
}
class MyClass
{
int aMeth()
{
TheClass hisInt = new TheClass();
return hisInt.propInt;
}
}
class Return4
{
public static void main(String[] args)
{
MyClass myObj = new MyClass();
int num = myObj.aMeth();
System.out.println(num);
}
}
The expression of interest here is, "hisInt.propInt", where hisInt is the variable name of an instantiated object and propInt is a property variable of the instantiated object (or class). The method of interest is aMeth(). Since the expression boils down to an int, the return type of this method declaration is, int.
The return value of any instantiated object or string or array is a reference and not a copy of the content of the instantiated object or string or array, respectively. The return type that precedes the method of interest, is the classname (or STRING or array element type name followed by []). Down in main, the returned reference (value) has to be assigned to a variable of the same classname (same reference type). Read and try the following program that illustrates this for an instantiated object:
class TheClass
{
int propInt = 11;
}
class MyClass
{
TheClass aMeth()
{
TheClass hisObj = new TheClass();
return hisObj;
}
}
class Return5
{
public static void main(String[] args)
{
MyClass myObj = new MyClass();
TheClass anObj = myObj.aMeth();
System.out.println(anObj.propInt);
}
}
The method of interest here is aMeth(). It is preceded in its declaration by a classname, as return type. The argument of the return statement is an instantiated object variable name, that has the reference (intrinsically). You do not need to know the code (or number) that is actually the reference (it is typically a memory address number). Down in main, the return reference has been assigned to a variable of the same class name.
Read and try the following example for the case of an array:
class MyClass
{
int[] aMeth()
{
int[] arr1 = new int[2];
arr1[0] = 111;
arr1[1] = 222;
return arr1;
}
}
class Return6
{
public static void main(String[] args)
{
MyClass myObj = new MyClass();
int[] arr2 = myObj.aMeth();
System.out.println(arr2[0]);
System.out.println(arr2[1]);
}
}
The method of interest is aMeth(). Its return type at the beginning of its declaration is the type of elements the return array would have, immediately followed by []. The array to be returned is declared and populated in the method body. The argument of the return statement is the name of the array without the square brackets. This name is a variable whose value is a reference. So, it is the reference that is returned and can be returned, in this situation. It is not a copy of the content of the array that is returned, it is the reference. Down in main, the returned reference is assigned to a new variable of the same array type. This is a new variable (identifier) and not a new array content.
The above is what I have installed for you. We take a break here and continue in the next part of the series.
Chrys