17 June 2023

Methods In Java - learngreen.net

  In Java, methods are like mini-programs that do special jobs and can be used when you want them to. 

They gather similar code together into a package of instructions so it can be used again more easily. Below are few important things to remember about Java methods

Method Declaration:- To create a method, you need to give it a name, decide who can use it (public or private), what it will return (like numbers or words), and what input it needs (which may not be necessary).

Method Signature:- The method signature includes the method's name and the things it needs to work correctly. It helps you tell apart ways of doing things that have the same name, but use different information (called method overloading).

Return Type:- The return type tells what kind of information the method will give back when it finishes doing its job. When a method doesn't give any answer, it is called "void".

Parameters:- Methods can take some variables called parameters that help to pass data into the method. These parameters could be zero or more. "Parameters" means some values that are given to a function. They are written inside brackets and separated by commas.

Method Body:- The method body is a set of directions that are followed when the method is used. It's inside squiggly brackets { }.

Method Invocation:- To use a way of doing something, you have to give it a signal to start working. Method invocation means using the name of the method and putting parentheses after it. If the method needs information, you can put it inside the parentheses.

Method Overloading:-  Java lets you make different methods that have the same name but use different information. The computer program decides which action to take based on what we tell it to do when we give it information.

Recursion:- In Java, a method can call itself, called recursion. Recursion can help solve problems by breaking them down into smaller parts.

Method Accessibility:- Methods can be set to be accessible in different ways such as making them available to everyone (public), only to a certain class (private), to related classes (protected), or not specifying any accessibility (default). The access modifiers decide which methods can be seen and used by other classes.

Method Overriding:- Inheritance lets subclasses create their own way of using methods that come from the main class. This means doing things in a different way than what was planned before. The new way of doing things in the smaller group must have the identical title, parts, and outcome as the way of doing things in the larger group.

Methods In Java



Types of methods in Java:-




//1. Method having no parameters and return value
  
      public void message( ) {
		System.out.println("Welcome on my blog");
	}

 /* This method doesn't return any results and 
 * it is declared as 'void'.
 * It prints "Welcome on my blog " when invoked.
 */ 


// 2. Method which has parameters and return value:-
  
    package Interview;
    public class MyClass {
	public int multiplyNumbers(int a,int b) {
	    int multiplication = a*b;
	    return multiplication;
	}
  public static void main(String[] args) {
  MyClass obj = new MyClass();
  int result=obj.multiplyNumbers(8,9);
  System.out.println(result);
    }
 }

// Output-> 72

 /* This code is about a class named MyClass.
  * Inside MyClass, there is a method called multiplyNumbers.
  * This method takes two numbers and multiplies them together.
  * It then return result . The main part of the program starts
  * by making a new MyClass object. Then it uses the multiplyNumbers
  * method with the numbers 8 and 9 as input.The answer is saved in
  * a variable "result" and then shown
  * on the screen using System.outprintln(result)
 */


 
  //3. Method with conditional statement
  
 public boolean isEven(int number) {
    if (number % 2 == 0) {
        return true;
    } else {
        return false;
    }
 }

 /* This method checks if the given number is even or not
  * by using a conditional statement. 
    It returns true if the number is even and false otherwise.
 */


 
 //4. Recursive Method
  
public int factorial(int n) {
    if (n == 0 || n == 1) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
 }

 /* This is an example of a recursive method
 *  that calculates the factorial of a number.
 *  It calls itself with a smaller value until
 *  the base case is reached (when n is 0 or 1).
 */


 
 // 5. Static Method
  
public static void printMessage(String message) {
    System.out.println(message);
 }

 /* This method is declared as static which
 *  means it doesnt require to create object.
 *  It can called directly.

These are different types of methods in Java and they explain how to use them. In Java programs, methods are very important for organizing the code, making it reusable and breaking it down into smaller parts.





No comments:

Post a Comment