10000 Updated methods and method-overloading pages · javaistic/javaistic@5538319 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5538319

Browse files
committed
Updated methods and method-overloading pages
1 parent 980af6e commit 5538319

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

src/pages/docs/method-overloading.mdx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ title: Java Method Overloading
33
description: In this tutorial, we will learn how to implements methods overloading or function overloading in Java.
44
---
55

6-
## Java Method Overloading
6+
import { TipInfo } from "@/components/Tip";
7+
8+
## What is Method Overloading in Java?
79
In Java, method overloading allows multiple methods with the same name but different parameter lists to coexist in the same class. This can involve variations in the number or types of parameters, or both. Overloaded methods provide flexibility by offering different ways to call a method, depending on the parameters passed.
810

911
### Example:
@@ -114,4 +116,8 @@ class HelperService {
114116
- Overloading is achieved by varying either the number or type of parameters.
115117
- Changing only the return type does not constitute overloading; there must be a parameter difference.
116118

117-
**Tip:** *Constructor overloading in Java works similarly to method overloading.*
119+
<TipInfo>
120+
121+
**Tip:** *Constructor overloading in Java works similarly to method overloading.*
122+
123+
</TipInfo>

src/pages/docs/methods.mdx

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ title: Java Methods
33
description: In this tutorial, we will learn how to implements methods or functions in Java.
44
---
55

6-
## What is a method in Java?
6+
import { TipInfo } from "@/components/Tip";
7+
8+
## What is a Method in Java?
79

810
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.
911

10-
### Types of Methods in Java
12+
## Types of Methods in Java
1113

1214
- `User-defined Methods`: Custom methods created by the programmer based on specific requirements.
1315
- `Standard Library Methods`: Built-in methods provided by Java, ready for immediate use.
1416

15-
### Declaring a Java Method
17+
## Declaring a Java Method
1618

1719
The basic syntax for a method declaration is:
1820

@@ -25,7 +27,7 @@ returnType methodName() {
2527
2. **methodName:** The identifier used to call the method.
2628
3. **method body:** Code inside `{ }` that defines the task to be performed.
2729

28-
#### Example 1:
30+
### Example 1:
2931

3032
```java
3133
int addNumbers() {
@@ -34,7 +36,7 @@ int addNumbers() {
3436
```
3537
In this example, `addNumbers()` is the method name, and its return type is `int`.
3638

37-
#### For a more detailed declaration:
39+
### For a more detailed declaration:
3840

3941
```java
4042
modifier static returnType methodName(parameter1, parameter2, ...) {
@@ -45,13 +47,13 @@ modifier static returnType methodName(parameter1, parameter2, ...) {
4547
2. **static:** If included, the method can be called without creating an object.
4648
3. **parameter1, parameter2, ...parameterN:** Values passed to the method.
4749

48-
#### Example 2:
50+
### Example 2:
4951
The `sqrt()` method in the Math class is static, so it can be accessed as `Math.sqrt()` without creating an instance of Math.
5052

51-
### Calling a Method in Java
53+
## Calling a Method in Java
5254
To use a method, call it by name followed by parentheses.
5355

54-
#### Java Method Call Example
56+
### Java Method Call Example
5557

5658
```java
5759
class Main {
@@ -68,26 +70,26 @@ class Main {
6870
}
6971
```
7072

71-
#### Output
73+
### Output
7274

7375
```bash
7476
Sum is: 40
7577
```
7678

7779
Here, the `addNumbers()` method takes two parameters and returns their sum. Since it's not static, it requires an object to be called.
7880

79-
### Method Return Types
81+
## Method Return Types
8082
A method may or may not return a value. The return statement is used to return a value.
8183

82-
#### Example 1: Method returns integer value
84+
### Example 1: Method returns integer value
8385

8486
```java
8587
public static int square(int num) {
8688
return num * num;
8789
}
8890
```
8991

90-
#### Example 2: Method does not return any value
92+
### Example 2: Method does not return any value
9193

9294
```java
9395
// void keyword is used as function does not return any value
@@ -96,16 +98,16 @@ public void printSquare(int num) {
9698
}
9799
```
98100

99-
### Method Parameters
101+
## Method Parameters
100102
Methods can accept parameters, which are values passed to the method.
101103

102-
#### Examples
104+
### Examples
103105
- **With Parameters:** `int addNumbers(int a, int b)`
104106
- **Without Parameters:** `int addNumbers()`
105107

106108
When calling a method with parameters, you must provide values for each parameter.
107109

108-
#### Example of Method Parameters
110+
### Example of Method Parameters
109111
```java
110112
class Main {
111113
public void display1() {
@@ -123,32 +125,39 @@ class Main {
123125
}
124126
}
125127
```
126-
#### Output
128+
### Output
127129

128130
```bash
129131
Method without parameter
130132
Method with a single parameter: 24
131133
```
132-
**Note:** Java requires that argument types match the parameter types.
133134

134-
### Standard Library Methods
135-
Java includes built-in methods available in the Java Class Library (JCL), which comes with the JVM and JRE.
135+
<TipInfo>
136+
137+
**Note:** *Java requires that argument types match the parameter types.*
138+
139+
</TipInfo>
136140

137-
#### Example
141+
## Standard Library Methods
142+
Java includes built-in methods available in the Java Class Library (JCL), which comes with the [JVM and JRE](/docs/jvm-jre-jdk).
143+
144+
### Example
138145
```java
139146
public class Main {
140147
public static void main(String[] args) {
141148
System.out.println("Square root of 4 is: " + Math.sqrt(4));
142149
}
143150
}
144151
```
145-
#### Output
152+
### Output
146153
```bash
147154
Square root of 4 is: 2.0
148155
```
149156

150-
### Advantages of Using Methods
151-
**Code Reusability:** Define once, use multiple times.
157+
## Advantages of Using Methods
158+
### 1. Code Reusability
159+
Define once, use multiple times.
160+
152161
```java
153162
private static int getSquare(int x) {
154163
return x * x;
@@ -159,7 +168,7 @@ public static void main(String[] args) {
159168
}
160169
}
161170
```
162-
#### Output:
171+
### Output:
163172

164173
```bash
165174
Square of 1 is: 1
@@ -169,4 +178,5 @@ Square of 4 is: 16
169178
Square of 5 is: 25
170179
```
171180

172-
**Improved Readability:** Grouping code into methods makes it easier to read and debug.
181+
### 2. Improved Readability
182+
Grouping code into methods makes it easier to read and debug.

0 commit comments

Comments
 (0)
0