Methods are an essential concept in Java programming. They allow us to encapsulate a sequence of code statements into a single block, making our code more organized, reusable, and easier to understand. In this article, we will explore methods in Java, learn how to define and use them, and discuss different types of methods.
A method in Java is a collection of statements that perform a specific task. It is similar to a function in mathematics and other programming languages. Methods are used to break down complex problems into smaller, manageable tasks, making our code modular and easier to maintain.
Before diving into method examples, let's understand the syntax and components of a method in Java. Here is the general structure of a method:
accessModifier returnType methodName(parameterList) {
// method body
// code statements
// return statement (if returnType is not void)
}
Let's break down each component:
Now that we understand the components of a method, let's move on to some examples to see them in action.
Example 1: Simple Method
Let's start with a simple method that greets the user by printing a welcome message. The method does not accept any parameters and does not return any value (void return type).
public class HelloWorld {
public static void main(String[] args) {
greet(); // Call the greet() method
}
public static void greet() {
System.out.println("Welcome to Java Programming!");
}
}
Output
Welcome to Java Programming!
Explanation
Example 2: Method with Parameters
Methods can also accept input parameters. Let's create a method that calculates the sum of two numbers and returns the result.
public class Calculator {
public static void main(String[] args) {
int result = add(5, 7); // Call the add() method with arguments 5 and 7
System.out.println("Sum: " + result);
}
public static int add(int num1, int num2) {
int sum = num1 + num2;
return sum; // Return the sum
}
}
Output
Sum: 12
Explanation
Example 3: Method Overloading
Java allows us to define multiple methods with the same name but different parameters. This is called method overloading. Let's see an example:
public class Rectangle {
public static void main(String[] args) {
int area1 = calculateArea(5); // Call the calculateArea() method with one argument
int area2 = calculateArea(4, 7); // Call the calculateArea() method with two arguments
System.out.println("Area 1: " + area1);
System.out.println("Area 2: " + area2);
}
public static int calculateArea(int sideLength) {
return sideLength * sideLength;
}
public static int calculateArea(int length, int width) {
return length * width;
}
}
Output
Area 1: 25
Area 2: 28
Explanation:
1. Write a Java method to check if a number is even or odd.
public class NumberChecker {
public static void main(String[] args) {
int number = 7;
if (isEven(number)) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
}
public static boolean isEven(int number) {
return number % 2 == 0;
}
}
Output
7 is odd.
2. Write a Java method to find the maximum of three numbers.
public class MaximumFinder {
public static void main(String[] args) {
int a = 5, b = 9, c = 3;
int max = findMax(a, b, c);
System.out.println("Maximum: " + max);
}
public static int findMax(int num1, int num2, int num3) {
int max = num1;
if (num2 > max) {
max = num2;
}
if (num3 > max) {
max = num3;
}
return max;
}
}
Output
Maximum: 9
60 videos|37 docs|12 tests
|
|
Explore Courses for Software Development exam
|