0% found this document useful (0 votes)
2 views

Methods

Uploaded by

Sumedh Chaware
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Methods

Uploaded by

Sumedh Chaware
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Methods: A method is a group of statements that together can perform a specific task.

Every
java program has at least one method, which is main() method.
Why we use methods in Java?
Methods are used to divide a large amount code into modules, due to this we can easily
debug and maintain the code. For example, if we want to write a calculator program, at that
time we can write every logic in a separate methods (For addition-sum(), for subtraction-
sub(), for multiplication-product(), for division-division() and for mode-mode()).

Advantages of methods
Code Re-usability
We can able to develop an application in modular format.
Easily to debug the program.
Code optimization: No need to write lot of code.

Types of Methods: There are two type of functions there in Java Language. They are,

Library methods or Predefined methods: Library methods are those which are predefined
in java compiler/libraries.
The implementation part of predefined methods is available in java library files( JAR) that
are .class files are contained precompiled code. println(), nextInt(), next() etc. are
predefined methods.
**Limitations of predefined methods:
All predefined methods are contained limited task only. that is for what purpose method is
designed for same purpose it should be used.

As a programmer we do not have any controls on predefined method implementation part, it


is in machine readable(binary) format.

In implementation whenever a predefined method is not supporting user requirements then


we go for user defined methods.
User defined methods:
Defining a method: Defining of method is nothing but give body of method that means write
logic inside method body.
Syntax:

access-modifier non-access modifier return_type method_name(parameters)


{
//method body
//statements;
}

Return type: A method may return a value. The return_type is the data type of the value

the method returns. Return type parameters and returns statement are optional.

Method name: Method name is the name of method it is decided by programmer or we.

Parameters: This is a value which is pass in method at the time of calling of method, A

parameter is like a placeholder. It is optional.

Method body: Method body is the collection of statements.

Calling a method: When we call any method control goes to method body and execute entire
code. For call any method just write name of method and if any parameters are required
then pass the parameters.

Syntax:

method-name();

or
method-name(arguments);
or
variable = method-name(arguments);
Note: At the time of method calling, method must be terminated with ' ; '.

You might also like