What is a Java Method
What is a Java Method
1. A Java method is a block of Java code, it gives a block of code a name, and the code is “stored” under this name
2. The Java method ( Java code) will not run all by itself. It will only run when it is called.
3. Java Methods are used to run the same code many times, without having to code it many times, or retyping the code over and over and over again.
Define the code once, and use it many times!
4. You can pass data, known as parameters, into a Java method. The parameters are input values that the Java method needs in order to fulfil its task
Example of a Java method:
void myMethod(double length * width) { // length and width are parameters
double
area = length * width;
System.out.println(“The
area is “ + area);
}
Example of a Java method call:
myMethod(2,4);
// this line send 2 and 4 as length and width to the method
// the Java method will calculate 2 * 4 and print: The area is 8
Comments
Post a Comment