8000 Added this keyword page and updated other pages by psychomita · Pull Request #716 · javaistic/javaistic · GitHub
[go: up one dir, main page]

Skip to content

Added this keyword page and updated other pages #716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updated methods and method-overloading pages
  • Loading branch information
psychomita committed Oct 26, 2024
commit 55383190d63ef07828c4e0bea17f55f2de92daac
10 changes: 8 additions & 2 deletions src/pages/docs/method-overloading.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ title: Java Method Overloading
description: In this tutorial, we will learn how to implements methods overloading or function overloading in Java.
---

## Java Method Overloading
import { TipInfo } from "@/components/Tip";

## What is Method Overloading in Java?
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.

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

**Tip:** *Constructor overloading in Java works similarly to method overloading.*
<TipInfo>

**Tip:** *Constructor overloading in Java works similarly to method overloading.*

</TipInfo>
60 changes: 35 additions & 25 deletions src/pages/docs/methods.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ title: Java Methods
description: In this tutorial, we will learn how to implements methods or functions in Java.
---

## What is a method in Java?
import { TipInfo } from "@/components/Tip";

## What is a Method in Java?

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.

### Types of Methods in Java
## Types of Methods in Java

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

### Declaring a Java Method
## Declaring a Java Method

The basic syntax for a method declaration is:

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

#### Example 1:
### Example 1:

```java
int addNumbers() {
Expand All @@ -34,7 +36,7 @@ int addNumbers() {
```
In this example, `addNumbers()` is the method name, and its return type is `int`.

#### For a more detailed declaration:
### For a more detailed declaration:

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

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

### Calling a Method in Java
## Calling a Method in Java
To use a method, call it by name followed by parentheses.

#### Java Method Call Example
### Java Method Call Example

```java
class Main {
Expand All @@ -68,26 +70,26 @@ class Main {
}
```

#### Output
### Output

```bash
Sum is: 40
```

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

### Method Return Types
## Method Return Types
A method may or may not return a value. The return statement is used to return a value.

#### Example 1: Method returns integer value
### Example 1: Method returns integer value

```java
public static int square(int num) {
return num * num;
}
```

#### Example 2: Method does not return any value
### Example 2: Method does not return any value

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

### Method Parameters
## Method Parameters
Methods can accept parameters, which are values passed to the method.

#### Examples
### Examples
- **With Parameters:** `int addNumbers(int a, int b)`
- **Without Parameters:** `int addNumbers()`

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

#### Example of Method Parameters
### Example of Method Parameters
```java
class Main {
public void display1() {
Expand All @@ -123,32 +125,39 @@ class Main {
}
}
```
#### Output
### Output

```bash
Method without parameter
Method with a single parameter: 24
```
**Note:** Java requires that argument types match the parameter types.

### Standard Library Methods
Java includes built-in methods available in the Java Class Library (JCL), which comes with the JVM and JRE.
<TipInfo>

**Note:** *Java requires that argument types match the parameter types.*

</TipInfo>

#### Example
## Standard Library Methods
Java includes built-in methods available in the Java Class Library (JCL), which comes with the [JVM and JRE](/docs/jvm-jre-jdk).

### Example
```java
public class Main {
public static void main(String[] args) {
System.out.println("Square root of 4 is: " + Math.sqrt(4));
}
}
```
#### Output
### Output
```bash
Square root of 4 is: 2.0
```

### Advantages of Using Methods
**Code Reusability:** Define once, use multiple times.
## Advantages of Using Methods
### 1. Code Reusability
Define once, use multiple times.

```java
private static int getSquare(int x) {
return x * x;
Expand All @@ -159,7 +168,7 @@ public static void main(String[] args) {
}
}
```
#### Output:
### Output:

```bash
Square of 1 is: 1
Expand All @@ -169,4 +178,5 @@ Square of 4 is: 16
Square of 5 is: 25
```

**Improved Readability:** Grouping code into methods makes it easier to read and debug.
### 2. Improved Readability
Grouping code into methods makes it easier to read and debug.
0