Java Methods
What are Methods?
In the Article, we will Discuss about the Methods of java.
In Java, a method is a block of code that performs a specific task and can be called from other parts of a program. A method can have parameters (inputs) and can also return a value.
Java Methods:
In Java, a method is a block of code that performs a specific task and can be called from other parts of a program. A method can have parameters (inputs) and can also return a value.
Methods allow programmers to encapsulate functionality into reusable components, making it easier to write, maintain, and debug programs. By breaking down a program into smaller, more focused methods, the overall code becomes more organized and easier to understand.
Defining a Method in Java:
In Java, methods are defined inside a class and are declared using the following syntax:
Syntax:
modifier returnType methodName(parameterList) { // method body }
Explanation:
In the above Syntax, the modifier specifies the access level of the method,
- The returnType specifies the type of data that the method returns (or void if the method doesn’t return anything),
- The methodName specifies the name of the method.
- The parameterList is a comma-separated list of parameters that the method takes as input.
The syntax of a method in Java typically consists of several parts, including:
Syntax:
public int calculateSum(int num1, int num2) { // method body }
Syntax:
public void printMessage(String message) { System.out.println(message); }
Syntax:
public int calculateSum(int num1, int num2) { int sum = num1 + num2; return sum; }
Syntax:
int result = calculateSum(5, 10); printMessage("Hello, world!");
Java Example Program that defines and calls a simple method:
import java.util.*; public class Main{ public static void main(String[] args) { // call the printMessage method printMessage("Hello, world!"); // call the calculateSum method int num1 = 5; int num2 = 10; int result = calculateSum(num1, num2); System.out.println("The sum of " + num1 + " and " + num2 + " is " + result); } // method that prints a message to the console public static void printMessage(String message) { System.out.println(message); } // method that calculates the sum of two integers and returns the result public static int calculateSum(int num1, int num2) { int sum = num1 + num2; return sum; } }
Output:
Hello, world! The sum of 5 and 10 is 15
Explanation:
In the above program, defines two methods: printMessage and calculateSum. The printMessage method accepts a String parameter message and simply prints it to the console. The calculateSum method accepts two int parameters num1 and num2, calculates their sum, and returns the result.
In the main method, we call both methods: we pass the string “Hello, world!” to printMessage, and we pass the integers 5 and 10 to calculateSum, storing the result in a variable called result. We then use System.out.println to print a message that displays the values of num1, num2, and result.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment