Java Notes

Topic : Methods Part #1

Java Notes

Table of Content:

  1. What is the java method?

  2. Use of Java methods?

  3. Method Vr Function

  4. Argument Vr parameter

  5. Types of method

What is Java Method:

Java methods are like little tasks or actions that your Java program can do. You can think of them as mini-programs within your bigger program. When you want your program to do a specific thing, you can tell it to "run" a method. Methods can take in information (called "parameters"), and can also give back information (called "returning a value"). They are defined within a class and can be called by other parts of the program using the object of the class.

In Java, a method is a block of code that performs a specific task and may or may not return a value. It is used to encapsulate a group of statements that can be called multiple times in a program. Methods can take parameters and can also return a value or multiple values using arrays or objects. They are defined within a class and can be called by other parts of the program using the object of the class. Methods can also be overridden in subclasses to provide a different implementation.

Syntax to write methods:

modifiers returnType methodName(dataType parameter) {
    // method body
}
  • modifiers: Specifies the access level and other properties of the method, such as public, private, static, etc. These modifiers determine who can access the method and how it can be accessed. For example, a method with the public the modifier can be accessed from anywhere within the program, whereas a method with the private a modifier can only be accessed within the class where it's defined.

  • returnType: Specifies the data type of the value that the method returns. This can be any valid Java data type, such as int, double, String, boolean, etc. If the method does not return a value, the return type should be void.

  • methodName: Specifies the name of the method. The name should be a valid Java identifier and should follow the naming conventions for Java.

  • parameters: Specifies the type and name of the parameters that are passed to the method when it is called. The parameter list is enclosed in parentheses and multiple parameters are separated by commas. If the method does not take any parameters, the parameter list should be empty.

  • method body: Specifies the statements that are executed when the method is called. The method body is enclosed in curly braces {} and can include any valid Java statements.

Function Vr Methods:

In Java, a method is a block of code that performs a specific task and can be called from other parts of the program. It can accept arguments and return a value.

A function, on the other hand, is a subprogram that can be called from other parts of the program and can return a value, but typically refers to functions in mathematical contexts or other programming languages.

In Java, the term "function" is not commonly used, the correct terminology is "method".

sometimes because we write functions in java under the CLASS that's why we called them methods

Example of Java method:

Write Java method to print " Hello Word"

//Example 1:
//creating void method to print hello world!

public class HelloWorld{
    public static void main(String[] args) {
        System.out.println("This is  main method");

        //calling function 

        helloworld();

    }

    // creating void method for just print HW
    public static void helloworld(){
        System.out.println("Hello world ");
    }

}

Methods type:

  1. Void Return type method

  2. Not-Void return (value return according to the data type) return Methods.

Parameter Vr Argument:

A parameter is a variable that is declared in the method signature and receives a value when the method is called.

An argument is the actual value passed to the method when it is called. It is assigned to the corresponding parameter in the method.

For example, consider the following Java method:

public class pract{


     //Main method
    public static void main(String[] args) {
        int x= 2; 
        int y = 3;
        System.out.println(multiply(x, y));

    }

    //multiply method 
    public static int multiply(int a, int b ){
        return a*b;
    }



}

From the above code,

Parameter:

  • In the multiply method (int a, int b ) are the parameters which are passed in the multiply method.

  • Parameters are always linked with the defined method.

  • A parameter is variably used to define a particular value during the function definition whenever we define a function we introduce our compiler with some variables that are being used in the running function.

  • This parameter variable is not required to same as the argument variable name (x, y )

Argument:

  • multiply(x, y) is the argument which gets value from users or we can assign them in the main method.

  • Arguments are always linked with the main method.

  • The argument is the value passed to the function when the function is called.

    ex. in the above code multiply(x, y) is an argument.

  • whenever any function is called during the execution of the program there are some values passed with the function. This value is called an argument.

  • An argument when passed with a function, replaces with a variable which where used during the function definition that is parameters.

Void return Methods:

When we make the return type of the method void, the method must have a system.out.print(return output) and In the main method we have to just call the function with the parameter. (no need to store or print statement )

In the below program the sum function with a void return type is given :



public class Main{
    public static void main(String[] args) {
        int a= 10;
        int b=10;

        Sum(a, b);
     //function calling 
    }

    public static void Sum(int a, int b){
        System.out.println(a+b);
        return;
    }
}

Non-Void Return methods:

But when we return the result value after the operation from the method this value might be used in further programs so we need to store this value for this we need to obey the following points:

In the Method:
We need to put the appropriate return type of method and return with operation or store operation in any resulting variable and return that result variable.

In the main Method:

In the main method, we get a value return from the method after the operation so this must be saved in some variable for further use and print that variable if we want or we can directly print that argument.

/** so sometime we need the result of some methods for 
 next operation thats why we need to store that result in 
some variable thats we can use anywhere
 so for this we have to make return type is int .
 **/

 import java.util.*;
 public class Main{

     public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);

         System.out.println("Enter val of a");
         int a= sc.nextInt();

         System.out.println("Enter val of b");
         int b = sc.nextInt();

         //calling method

         int finalResult= sum(a,b);
         System.out.println(finalResult);
         //or
         //System.out.println(sum(a,b));
     } 

         //method define
         public static int sum(int a , int b ){
         int result = a+b;
         return result;
     }



 }


<The remaining part of java methods will be continued in the next part of this java notes series BLOG>