Skip to content

Commit c41070e

Browse files
authored
Merge pull request #714 from psychomita/main
Added Java Methods page
2 parents 4fbce9c + d124196 commit c41070e

File tree

2 files changed

+173
-1
lines changed

2 files changed

+173
-1
lines changed

src/navs/documentation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ export const documentationNav = {
2828
pages['continue-statement'],
2929
],
3030
'Java Arrays': [pages['arrays'], pages['multidimensional-arrays']],
31-
'Java OOPS(I)': [pages['class-objects'], pages['static-keyword']],
31+
'Java OOPS(I)': [pages['class-objects'], pages['methods'], pages['static-keyword']],
3232
}

src/pages/docs/methods.mdx

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
---
2+
title: Java Methods
3+
description: In this tutorial, we will learn how to implements methods or functions in Java.
4+
---
5+
6+
## What is a method in Java?
7+
8+
In Java, a method is a collection of statements designed to perform specific tasks and, optionally, return a result to the caller. A Java method can also execute a task without returning any value. Methods enable code reuse by eliminating the need to retype code. Importantly, in Java, every method must belong to a class.
9+
10+
### Types of Methods in Java
11+
12+
- `User-defined Methods`: Custom methods created by the programmer based on specific requirements.
13+
- `Standard Library Methods`: Built-in methods provided by Java, ready for immediate use.
14+
15+
### Declaring a Java Method
16+
17+
The basic syntax for a method declaration is:
18+
19+
```java
20+
returnType methodName() {
21+
// method body
22+
}
23+
```
24+
1. **returnType:** Specifies the type of value returned by the method. If it doesn’t return a value, the return type is void.
25+
2. **methodName:** The identifier used to call the method.
26+
3. **method body:** Code inside `{ }` that defines the task to be performed.
27+
28+
#### Example 1:
29+
30+
```java
31+
int addNumbers() {
32+
// code
33+
}
34+
```
35+
In this example, `addNumbers()` is the method name, and its return type is `int`.
36+
37+
#### For a more detailed declaration:
38+
39+
```java
40+
modifier static returnType methodName(parameter1, parameter2, ...) {
41+
// method body
42+
}
43+
```
44+
1. **modifier:** Defines access type (e.g., public, private).
45+
2. **static:** If included, the method can be called without creating an object.
46+
3. **parameter1, parameter2, ...parameterN:** Values passed to the method.
47+
48+
#### Example 2:
49+
The `sqrt()` method in the Math class is static, so it can be accessed as `Math.sqrt()` without creating an instance of Math.
50+
51+
### Calling a Method in Java
52+
To use a method, call it by name followed by parentheses.
53+
54+
#### Java Method Call Example
55+
56+
```java
57+
class Main {
58+
public int addNumbers(int a, int b) {
59+
return a + b;
60+
}
61+
62+
public static void main(String[] args) {
63+
int num1 = 25, num2 = 15;
64+
Main obj = new Main(); // Create object of Main class
65+
int result = obj.addNumbers(num1, num2);
66+
System.out.println("Sum is: " + result);
67+
}
68+
}
69+
```
70+
71+
#### Output
72+
73+
```bash
74+
Sum is: 40
75+
```
76+
77+
Here, the `addNumbers()` method takes two parameters and returns their sum. Since it's not static, it requires an object to be called.
78+
79+
### Method Return Types
80+
A method may or may not return a value. The return statement is used to return a value.
81+
82+
#### Example 1: Method returns integer value
83+
84+
```java
85+
public static int square(int num) {
86+
return num * num;
87+
}
88+
```
89+
90+
#### Example 2: Method does not return any value
91+
92+
```java
93+
// void keyword is used as function does not return any value
94+
public void printSquare(int num) {
95+
System.out.println(num * num);
96+
}
97+
```
98+
99+
### Method Parameters
100+
Methods can accept parameters, which are values passed to the method.
101+
102+
#### Examples
103+
- **With Parameters:** `int addNumbers(int a, int b)`
104+
- **Without Parameters:** `int addNumbers()`
105+
106+
When calling a method with parameters, you must provide values for each parameter.
107+
108+
#### Example of Method Parameters
109+
```java
110+
class Main {
111+
public void display1() {
112+
System.out.println("Method without parameter");
113+
}
114+
115+
public void display2(int a) {
116+
System.out.println("Method with a single parameter: " + a);
117+
}
118+
119+
public static void main(String[] args) {
120+
Main obj = new Main();
121+
obj.display1(); // No parameters
122+
obj.display2(24); // Single parameter
123+
}
124+
}
125+
```
126+
#### Output
127+
128+
```bash
129+
Method without parameter
130+
Method with a single parameter: 24
131+
```
132+
**Note:** Java requires that argument types match the parameter types.
133+
134+
### Standard Library Methods
135+
Java includes built-in methods available in the Java Class Library (JCL), which comes with the JVM and JRE.
136+
137+
#### Example
138+
```java
139+
public class Main {
140+
public static void main(String[] args) {
141+
System.out.println("Square root of 4 is: " + Math.sqrt(4));
142+
}
143+
}
144+
```
145+
#### Output
146+
```bash
147+
Square root of 4 is: 2.0
148+
```
149+
150+
### Advantages of Using Methods
151+
**Code Reusability:** Define once, use multiple times.
152+
```java
153+
private static int getSquare(int x) {
154+
return x * x;
155+
}
156+
public static void main(String[] args) {
157+
for (int i = 1; i <= 5; i++) {
158+
System.out.println("Square of " + i + " is: " + getSquare(i));
159+
}
160+
}
161+
```
162+
#### Output:
163+
164+
```bash
165+
Square of 1 is: 1
166+
Square of 2 is: 4
167+
Square of 3 is: 9
168+
Square of 4 is: 16
169+
Square of 5 is: 25
170+
```
171+
172+
**Improved Readability:** Grouping code into methods makes it easier to read and debug.

0 commit comments

Comments
 (0)