[Go to site: main page, start]

0% found this document useful (0 votes)
4 views2 pages

Understanding Java Methods and Syntax

The document explains methods in Java, which are blocks of code that execute when called, allowing for code reuse. It categorizes methods into predefined (built-in) and user-defined (custom) types, detailing their syntax and providing examples. Additionally, it includes code snippets demonstrating methods with and without parameters.

Uploaded by

rdayakar65
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Understanding Java Methods and Syntax

The document explains methods in Java, which are blocks of code that execute when called, allowing for code reuse. It categorizes methods into predefined (built-in) and user-defined (custom) types, detailing their syntax and providing examples. Additionally, it includes code snippets demonstrating methods with and without parameters.

Uploaded by

rdayakar65
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Functions

A method is a block of code which only runs when it is called.


Why use methods?
To reuse code: define the code once, and use it many times.
Types of Methods
In Java, methods can be categorized in two main ways:
1. Predefined vs. User-defined:
Predefined methods: These methods are already defined in the Java Class
Library and can be used directly without any declaration.
Examples include [Link]() for printing to the console and
[Link]() for finding the maximum of two numbers.
User-defined methods: These are methods that you write yourself to perform
specific tasks within your program.
Syntax:
returnType methodName() {
// method body
}

returnType - It specifies what type of value a method returns.


methodName - It is an identifier that is used to refer to the particular method
in a program.
method body - It includes the programming statements that are used to perform
some [Link] is enclosed inside the curly braces { }.
Code:
class Main {
// create a method
public int addNumbers(int a, int b) {
int sum = a + b;
// return value
return sum;
}
public static void main(String[] args) {
int num1 = 25;
int num2 = 15;
// create an object of Main
Main obj = new Main();
// calling method
int result = [Link](num1, num2);
[Link]("Sum is: " + result);
}
}
Method with Parameter and without Parameter
class Main {

// method with no parameter


public void display1() {
[Link]("Method without parameter");
}

// method with single parameter


public void display2(int a) {
[Link]("Method with a single parameter: " + a);
}

public static void main(String[] args) {

// create an object of Main


Main obj = new Main();
// calling method with no parameter
obj.display1();

// calling method with the single parameter


obj.display2(24);
}
}

You might also like