Methods in Java

 Methods in Java

  • A method is a collection of statements that perform some specific task and return the result to the caller
  • . A method can perform some specific task without returning anything.
  •  Methods allow us to reuse the code without retyping the code. 
  • In Java, every method must be part of some class which is different from languages like C, C++, and Python.
  • Methods are time savers and help us to reuse the code without retyping the code.
Types of Method:-  
  

There are two types of methods in Java:

  • Predefined Method

  • User-defined Method

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 predefined 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.

Example:-

class Demo   

{  

public static void main(String[] args)   

{  


System.out.print("The maximum number is: " + Math.max(9,7));  

}  

}  

User-defined Method:-

The method written by the user or programmer is known as a user-defined method. These methods are modified according to the requirement.


public static void findEvenOdd(int num)  

{  


if(num%2==0)   

System.out.println(num+" is even");   

else   

System.out.println(num+" is odd");  

}  

Method Declaration:-

  • Modifier-: Defines access type of the method i.e. from where it can be accessed in your application. In Java, there 4 type of the access specifiers.
  • public: accessible in all class in your application.
  • protected: accessible within the class in which it is defined and in its subclass(es)
  • private: accessible only within the class in which it is defined.
  • default (declared/defined without using any modifier) : accessible within same class and package within which its class is defined.
  • The return type : The data type of the value returned by the method or void if does not return a value.
  • Method Name : the rules for field names apply to method names as well, but the convention is a little different.
  • Parameter list : Comma separated list of the input parameters are defined, preceded with their data type, within the enclosed parentheses. If there are no parameters, you must use empty parentheses ().
  • Exception list : The exceptions you expect by the method can throw, you can specify these exception(s).
  • Method body : it is enclosed between braces. The code you need to be executed to perform your intended operations.




Calling a method:-

The method needs to be called for using its functionality. There can be three situations when a method is called:

A method returns to the code that invoked it when:

  • It completes all the statements in the method
  • It reaches a return statement
  • Throws an exception

Example of method:_

import java.io.*;

  

class Addition {

      

    int sum = 0;

      

    public int addTwo(int a, int b){

          

               sum = a + b;

          

                return sum; 

    }

      

}

  

class demo{

    public static void main (String[] args) {

      

                Addition add = new Addition();

          

               int s = add.addTwo(1,2);


        System.out.println("Sum of two integer values :"+ s);

          

    }

}    


output:-    Sum of two integer values :3

                 



Comments

Popular posts from this blog

Inheritance in Java

Type Conversions

Life cycle of a Thread and creating thread class