0% found this document useful (0 votes)
24 views6 pages

Java Methods: Types and Usage Guide

Uploaded by

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

Java Methods: Types and Usage Guide

Uploaded by

subramanisheela1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

📌 Method

A method is a small program or a block of code written


inside a class. It performs a specific task when it is
called.

📌 Types of Methods

In Java, there are two types of methods:

1. Pre-defined Methods: These are already created


in Java and can be used directly.
Examples: [Link](), [Link]()
2. User-defined Methods: These are written by the
programmer to perform a particular task.

📌 Need for User-Defined Methods

 They help in breaking big programs into


smaller, manageable parts.
 Make the program easier to write, read, and
understand.
 The same method can be used in a program many
times without rewriting the same code.
 It makes the program shorter and neater.

📌 Parts of a User-Defined Method

Every user-defined method has three main parts:

1. Method Declaration
2. Method Calling
3. Method Definition
📌 1️⃣ Method Declaration

It tells the compiler about the name of the method,


what type of value it will return, and the
parameters (if any) it will receive.

Syntax:

Java returnType methodName (parameter list);

Example:

java
CopyEdit
int sum (int a, int b);

Explanation:

 int is the return type (this method will return an


integer).
 sum is the method’s name.
 (int a, int b) is the list of parameters the method
will take when called.

📌 2️⃣ Method Calling

A method can be called (used) by writing its name


followed by round brackets (parentheses) containing
the required arguments.

Syntax:

java
CopyEdit
methodName (arguments);

Example:
java
CopyEdit
sum(5, 10);

📌 3️⃣ Method Definition

It is the part where the actual code or statements of


the method are written.
This part tells what the method will do when it is
called.

Syntax:

java
CopyEdit
returnType methodName (parameter list)
{
statements;
return value;
}

Example:

java
CopyEdit
int sum (int a, int b)
{
int c = a + b;
return c;
}

📌 Types of User-Defined Methods

User-defined methods are classified based on:

 Whether they take arguments or not.


 Whether they return a value or not.
There are four types:

Type Description Example


1. With Takes values as
int sum(int
arguments and arguments and gives
a, int b)
with return value back a value.
2. With
Takes values as void
arguments and
arguments but does display(int
without return
not give back a value. a)
value
3. Without
Takes no arguments int
arguments and
but gives back a value. getValue()
with return value
4. Without
Neither takes
arguments and void
arguments nor gives
without return show()
back a value.
value

📌 Return Type

The return type tells what kind of value a method will


give back after it is executed.
It can be:

 int for integers


 double for decimal numbers
 char for characters
 void if no value is returned

📌 Void Keyword

If a method does not return any value, we write void in


place of the return type.

Example:
java
CopyEdit
void display()
{
[Link]("Hello");
}

📌 Actual and Formal Parameters

When a method takes values while being called, those


values are called actual parameters (also known as
arguments).

When a method receives values inside its definition,


those values are called formal parameters (also
known as parameters).

Type Explanation Example


Actual Values passed at the time of sum(5, 10) →
Parameter method calling. 5, 10
Variables that receive the
Formal int sum(int a,
values inside the method
Parameter int b) → a, b
definition.

📌 Calling a Method

A method can be called in two ways:

 If it belongs to the same class: Directly write the


method name with arguments.
 If it belongs to another class: Create an object of
that class and then call the method using
[Link](arguments);

📌 Method Overloading
Method overloading means creating two or more
methods with the same name but different
parameter lists (the number, type, or order of
parameters must be different).

Example:

java
CopyEdit
void display(int a)
void display(double a)
void display(String a)

Purpose of Method Overloading:

 It improves program readability.


 It allows the use of the same method name for
different types of tasks.

You might also like