Assigmanet JAVA
Assigmanet JAVA
1. Method in Java
In general, 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,
Return Type: Return type is a data type that the method returns. It may have a primitive data type, object,
collection, void, etc. If the method does not return anything, we use void keyword.
Method Name: It is a unique name that is used to define the name of a method. It must be corresponding to the
functionality of the method. Suppose, if we are creating a method for subtraction of two numbers, the method
name must be subtraction(). A method is invoked by its name.
Parameter List: It is the list of parameters separated by a comma and enclosed in the pair of parentheses. It
contains the data type and variable name. If the method has no parameter, left the parentheses blank.
Method Body: It is a part of the method declaration. It contains all the actions to be performed. It is enclosed
within the pair of curly braces.
4. Types of Method
There are two types of methods in Java:
o Predefined Method
o User-defined Method
5. Predefined Method
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.
Each and every predefined method is defined inside a class. Such as print() method is defined in
the java.io.PrintStream class. It prints the statement that we write inside the method. For example, print("Java"),
it prints Java on the console.
Demo.java
In the above example, we have used three predefined methods main(), print(), and max(). We have used these
methods directly without declaration because they are predefined. The print() method is a method
of PrintStream class that prints the result on the console. The max() method is a method of the Math class that
returns the greater of two numbers.
We can also see the method signature of any predefined method by using the link https://docs.oracle.com/.
When we go through the link and see the max() method signature, we find the following:
In the above method signature, we see that the method signature has access specifier public, non-access
modifier static, return type int, method name max(), parameter list (int a, int b). In the above example, instead of
defining the method, we have just invoked the method. This is the advantage of a predefined method. It makes
programming less complicated.