Skip to main content

Java Methods – Understanding Functions in Java

 

Introduction to Java Methods

Methods in Java are reusable blocks of code designed to perform specific tasks. They enhance code reusability, maintainability, and readability. Java provides predefined (built-in) and user-defined methods.


1. Declaring a Method in Java

A method consists of:

  • Access Modifier (public, private, etc.)
  • Return Type (void, int, String, etc.)
  • Method Name (should be meaningful)
  • Parameters (optional)
  • Method Body (code inside the method)

Syntax:

accessModifier returnType methodName(parameters) {
// Method body return value; // Optional }

Example:

public class MethodExample {
public static void greet() { System.out.println("Hello, welcome to Java!"); } public static void main(String[] args) { greet(); // Calling the method } }

2. Types of Methods in Java

2.1 Predefined (Built-in) Methods

Java provides predefined methods like Math.sqrt(), length() for strings, and System.out.println().

Example:

public class BuiltInMethodExample {
public static void main(String[] args) { int num = 16; System.out.println("Square root: " + Math.sqrt(num)); } }

2.2 User-Defined Methods

You can create custom methods based on your needs.

Example:

public class UserMethodExample {
public static int square(int num) { return num * num; } public static void main(String[] args) { System.out.println("Square of 5: " + square(5)); } }

3. Method Parameters and Return Types

3.1 Method with Parameters

Passing arguments to a method allows it to work dynamically.

Example:

public class ParameterExample {
public static void greet(String name) { System.out.println("Hello, " + name + "!"); } public static void main(String[] args) { greet("Alice"); greet("Bob"); } }

3.2 Method Returning a Value

A method can return data using the return keyword.

Example:

public class ReturnExample {
public static int add(int a, int b) { return a + b; } public static void main(String[] args) { int sum = add(10, 20); System.out.println("Sum: " + sum); } }

4. Method Overloading

Java allows multiple methods with the same name but different parameters. This is called method overloading.

Example:

public class OverloadExample {
public static int multiply(int a, int b) { return a * b; } public static double multiply(double a, double b) { return a * b; } public static void main(String[] args) { System.out.println("Int Multiply: " + multiply(5, 4)); System.out.println("Double Multiply: " + multiply(5.5, 4.2)); } }

5. The main() Method in Java

Every Java program must have a main() method, which acts as the entry point.

Syntax:

public static void main(String[] args) {
// Code execution starts here }

Example:

public class MainMethodExample {
public static void main(String[] args) { System.out.println("Java Program Execution Begins!"); } }

Conclusion

Methods help in writing clean, reusable, and efficient Java programs. In the next post, we will discuss Recursion in Java – How It Works with Examples.

Comments