Java Difference Between Method & Function

Difference Between Method & Function in Java

Method: A method is a ready-to-use function. Methods are always associated with a class. Methods can access and modify the data. Below are examples of the class and methods. A method can’t be  defined without the class

For example:

Class Method
Math max
Math min
Arrays equals
String concat
String equals
LocalDate now

Example of Method :

Scenario: create a program to find a max no between these 2 no(40,60) by using Math.Max method

import java.lang.Math;
   public class Main {
     public static void main(String[] args) {
        int result = Math.max(40, 60);
    System.out.println(result); // Outputs: 60
   }
}

Explanation:
public static void main(String[] args)
This line defines the main method, which is the entry point of the program. When you run the program, this method is executed first.
int result = Math.max(40, 60);
This line declares an integer variable named result and sets it to the larger of the two numbers 40 and 60 using the Math.max method.
System.out.println(result);
This line prints the value of result to the console. In this case, it prints 60 because that’s the larger of the two numbers.

Function: A function is a block of reusable code that performs a specific task. Functions can take arguments as input and return values as output.

Example of Function :

Scenario: create a program to find a max number between these 2 no(10,20) without using Math.Max method and use custom  function.

public class Main {
    public static void maximum() {
      int marksofEnglish = 10;
        int marksofScience = 20;
          int result = marksofEnglish > marksofScience ? marksofEnglish : marksofScience;
    System.out.println(result); // Outputs: 20
 }
      public static void main(String[] args) {
    maximum();
   }
}

Explanation:
public static void maximum()
This defines a method named maximum. The public keyword means this method can be called from outside the class. The static keyword means this function belongs to the class itself, not to any specific object of the class.
int marksofEnglish = 10;
This declares an integer variable named marksofEnglish and sets its value to 10.
int marksofScience = 20;
This declares another integer variable named marksofScience and sets its value to 20.
int result = marksofEnglish > marksofScience ? marksofEnglish : marksofScience;
This line uses a conditional (ternary) operator to find the larger of the two numbers. It checks if marksofEnglish is greater than marksofScience. If true, result is set to marksofEnglish. If false, result is set to marksofScience.
System.out.println(result);
This line prints the value of the result to the console. It will print 20.

Class
There are two types of classes:
Standard classes like Math, String, LocalDate, etc., as described in the above method table.
Custom classes, like the Operation class shown below.

public class Operation {
   public static void main(String[] args) {
    // Your code here
 }
}

Course Video

YouTube Reference :