In the past two tutorials, we have learnt about datatypes and
operators. Today, I will introduce methods. Simply, methods are just like
formulae. From elementary school mathematics, we know that the formula for
calculating the area of a rectangle is length x width. We know that the formula
for calculating the area of a circle is πr2. The formula for
calculating the perimeter of a square is 4 x side.
The above formulae were defined by people like you and me.
Maybe if you were alive back then, you might have discovered the formulae
yourself. However, this does not mean that all the formulae in the universe
have been defined. You can define your own formulae from the simplest to the
most complicated things. A method or formula is just a way to do things. And in
Java, you can create your own formulae or methods for almost anything
imaginable.
However, let me teach you something new, called return types.
Return Types
When you calculate the volume of a sphere, what type of data
do you input into the formula?
Numerical data.
When you want to calculate the length of the hypotenuse, what
type of data do you input into the
formula? Numerical data.
When you want to count the number of characters in a word,
what type of data do you input into
your formula? Textual data.
Notice that I use the word input in all the above three examples. Why, because the type of
data that you input is the kind of data you expect to get. It’s like you are
telling Java, take these numbers, do the following operations on them and then return to me with the answer.
So when we input textual data, what type of data should we
expect to return? Textual data, of course. And when we input numerical data,
what type of data should we expect the method to return? Numerical data.
Return Types in Java
In Java, you can have methods that return integer (short,
int, long), float, double, String, boolean, void, and generic values. The new
terms in this sentence are void and generic types. I will talk about generic
types in the next tutorial. Void just means that the method returns nothing. It
may do some operations, but in the end, it returns nothing. I will show you
examples of void functions in this tutorial.
Integer methods return integers. Floats and doubles return
numerical values with decimal values. String functions return strings. Boolean
functions return either true or false. Void functions return nothing. You may
use void methods to do things such as print something on the screen, but it
doesn’t return any value.
Parameters
A parameter is what the data that the method takes from the
user or a certain process and uses it to return a value. For example, the
formula for the area of a rectangle are length and width i.e l x w = A. So to
find the area, you have to supply your method with the length and width
parameters and the method will give you an answer.
Your method may or may not have parameters, and this is
dependent on what you want your function to do.
Structure of a Method
1) When writing a method, you first indicate its return type.
This way, we tell Java what type of value we want the method to produce. Is it
an integer, a boolean value, a double or value, or is it void?
2) The second step is giving your method a name. The name
of the function is really up to you. You are however supposed to use a method
name that suggests what it is going to do. This will help you when you are
reading your code, because you will understand which function does what. Also, your method should always start with a lowercase letter.
3) The third step when writing your method writing the
parentheses or brackets (). Within these parentheses is where you add your
parameters. Depending on your method, it is not mandatory that it has
parameters. Moreover, it can have one, two, or whatever number of parameters
necessary for your needs.
4) The fourth step when writing your method is adding the
opening and closing curly braces {}. Within these curly braces is where you
write the processes that your method will use to come up with your answer. The
statements within the curly braces are what your function will use to compute
your result.
Having covered the major parts of a Java method, let me show
you actual examples:
Examples of Java Methods
1) Methods with parameters
1a) Methods with one parameter
String yourName (String name) {
return “Your
name is” + name;
}
int yourAgeInTenYears (int age) {
return age +
10;
}
1b) Methods with more than one parameter
void yourNameAndAge (String name, int age) {
System.out.println(“Your
name is ” + name + “ and your age is ” + age);
}
double volumeOfCuboid (double length, double width, double
height) {
return
length * width * height;
}
2) Methods with no parameters
String greetings () {
return “Hi
there you!”;
}
As you can see, Java allows you to do infinite things. You
can create a method for almost anything.
Using Methods in an Actual Program
You have to define a method before you can use it. Now that
we have covered the defining part, let us look at a real example.
public class HelloWorld {
//Declare a
method:
static void
yourNameAndAge (String name, int age) {
System.out.println(“Your
name is ” + name + “ and your age is ” + age);
}
public
static void main (String[] args) {
//Calling a method
yourNameAndAge(“Brian”,
87);
}
}Figure 1: A program showing the declaration of a method that prints out a person’s name and age on the console.
In the above code, you may notice that I have added another
term called static. I have explained the use of the term static in another tutorial.
Let’s look at another method:
double volumeOfCuboid (double length, double width, double
height) {
return
length * width * height;
}
Figure 2: A program showing the declaration of a method that calculates the volume of a cuboid with dimensions 10 by 10 by 10 and prints the results on the console.
So there you have it folks… Methods in Java, and they are not
as hard as you thought. You can play around with the examples and even create
your own methods. Make mistakes and learn from them. This is how you learn.
If you have any questions or comments, please post them in
the comments section and I will address them.
No comments:
Post a Comment