A method is a way to perform some task. Similarly, the method in Java is a collection of instructions that performs a specific task.
It provides the reusability of code. We can also easily modify code using methods. In this section, we will learn what is a method in Java, types of methods, method declaration, and how to call a method in Java.
A method is a block of code or collection of statements or a set of code grouped together to perform a certain task or operation.
It is used to achieve the reusability of code. We write a method once and use it many times. We do not require to write code again and again.
It also provides the easy modification and readability of code, just by adding or removing a chunk of code. The method is executed only when we call or invoke it.
Every method has a method signature. It is a part of the method declaration. It includes the method name and parameter list.
In Java, predefined methods are the method that is already defined in the Java class libraries is known as predefined methods.
It is also known as the standard library method or built-in method. We can directly use these methods just by calling them in the program at any point.
Some pre-defined methods are length(), equals(), compareTo(), sqrt(), etc. When we call any of the predefined methods in our program, a series of codes related to the
corresponding method runs in the background that is already stored in the library.