The return statement does not have to pass a value back at all. It can just be used to return control to the calling program. Once a return statement has been executed, the code which follows it is not executed. You can see what I mean in the example below:
andrew@UBUNTU:~/Java$ cat Number_Check.java
public class Number_Check
{
public void check_number(int a)
{
if (a < 10)
{
System.out.println(a + " < 10");
return;
}
// The next line is ignored if the number supplied
// is less than 10:
System.out.println(a + " >= 10");
}
}
andrew@UBUNTU:~/Java$ javac Number_Check.java
andrew@UBUNTU:~/Java$
andrew@UBUNTU:~/Java$ cat Number_Check_Test.java
class Number_Check_Test
{
public static void main(String args[])
{
Number_Check x = new Number_Check();
x.check_number(9);
x.check_number(10);
x.check_number(11);
}
}
andrew@UBUNTU:~/Java$ javac Number_Check_Test.java
andrew@UBUNTU:~/Java$ java Number_Check_Test
9 < 10
10 >= 10
11 >= 10
andrew@UBUNTU:~/Java$
However, if the compiler sees code which will NEVER execute, it returns a compilation error:
andrew@UBUNTU:~/Java$ cat Another_Number_Check.java
public class Another_Number_Check
{
public void check_number(int a)
{
if (a < 10)
{
System.out.println(a + " < 10");
return;
}
else
{
System.out.println(a + " >= 10");
return;
}
System.out.println("This line is ignored");
}
}
andrew@UBUNTU:~/Java$ javac Another_Number_Check.java
Another_Number_Check.java:15: unreachable statement
System.out.println("This line is ignored");
^
1 error
andrew@UBUNTU:~/Java$
Showing posts with label return. Show all posts
Showing posts with label return. Show all posts
Saturday, 31 January 2015
Thursday, 29 January 2015
How to Return Values From a Java Method
This example is based on an earlier post but I have changed it to show how a method can return a value. The first part creates a class called Square:
andrew@UBUNTU:~/Java$ cat Square.java
public class Square
{
double width;
public void display_area()
{
// The next line calls the calculate_area method.
// The value returned is assigned to area:
double area = calculate_area();
System.out.println("Area = " + area);
}
// The word double in the next line tells you
// that this method returns a double value:
private double calculate_area()
{
double area = width * width;
// The next line returns the value:
return area;
}
}
andrew@UBUNTU:~/Java$ javac Square.java
andrew@UBUNTU:~/Java$
The second part creates a member of the Square class and calls the methods which calculate and display its area:
andrew@UBUNTU:~/Java$ cat SquareExample.java
class SquareExample
{
public static void main(String args[])
{
Square my_square = new Square();
my_square.width = 5;
my_square.display_area();
}
}
andrew@UBUNTU:~/Java$ javac SquareExample.java
andrew@UBUNTU:~/Java$ java SquareExample
Area = 25.0
andrew@UBUNTU:~/Java$
andrew@UBUNTU:~/Java$ cat Square.java
public class Square
{
double width;
public void display_area()
{
// The next line calls the calculate_area method.
// The value returned is assigned to area:
double area = calculate_area();
System.out.println("Area = " + area);
}
// The word double in the next line tells you
// that this method returns a double value:
private double calculate_area()
{
double area = width * width;
// The next line returns the value:
return area;
}
}
andrew@UBUNTU:~/Java$ javac Square.java
andrew@UBUNTU:~/Java$
The second part creates a member of the Square class and calls the methods which calculate and display its area:
andrew@UBUNTU:~/Java$ cat SquareExample.java
class SquareExample
{
public static void main(String args[])
{
Square my_square = new Square();
my_square.width = 5;
my_square.display_area();
}
}
andrew@UBUNTU:~/Java$ javac SquareExample.java
andrew@UBUNTU:~/Java$ java SquareExample
Area = 25.0
andrew@UBUNTU:~/Java$
Location:
West Sussex, UK
Subscribe to:
Posts (Atom)