Methods
Methods
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.
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
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 ' ; '.