0% found this document useful (0 votes)
17 views4 pages

Assigmanet JAVA

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Assigmanet JAVA

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 4

GALMUDUG STATE UNIVERSITY

1. Assignment Title: Java Methods

2. Students Name: Mohamed Osman,Farhia Muuse

3. Course Name: Java

4. Instructor's Name: Eng. Ibraham Abdinor

5. Due Date: October 24, 2024

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,

2. What is 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.
3. Method Declaration
The method declaration provides information about method attributes, such as visibility, return-type, name, and
arguments. It has six components that are known as method header, as we have shown in the following figure.

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.

Let's see an example of the predefined method.

Demo.java

1. public class Demo


2. {
3. public static void main(String[] args)
4. {
5. // using the max() method of Math class
6. System.out.print("The maximum number is: "
+ Math.max(9,7));
7. }
8. }
Output:

The maximum number is: 9

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.

Prepared By: Mohamed Osman,Farhia Muuse

You might also like