JAVA Methods

You will learn Java methods in the quickest possible way.


Java method is like a code segment, or a group of codes that can be used/called later in your main code.
The structure:

public static boolean isthisequal(String str1, String str2){
    if (str1.equals(str2)){
        return true;
    }
    return false;
}
public -  is an access specifier for java methods and classes. It will indicate that your method can be used/called in any other classes or package in your program.
static - you will learn this later in Java Classes.
boolean is the return type.
isthisequal is the method name.
String str1, String str2 are the parameters. We have created two parameters in this method but you can also create a lot more and also with different data types like int.



To call/use your method:

In your main(...), you will call it by the methods name; For Example:

In the above example we used a return type of void that is why there are no more return statements.

If you remember in our previous tutorial about String methods. For example equals is also a method.


Here is a complete Java methods tutorial program. Learn by example!



No comments:

Post a Comment