Skip to content

Commit 13f4bcb

Browse files
committed
methods-and-functions
1 parent be68a3a commit 13f4bcb

File tree

3 files changed

+254
-4
lines changed

3 files changed

+254
-4
lines changed

docs/java/methods-and-functions/method-declaration-and-syntax.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,38 @@ sidebar_label: Method Declaration and Syntax
55
sidebar_position: 1
66
tags: [java, methods, functions, programming, java methods, java functions]
77
description: In this tutorial, we will learn about methods and functions in Java. We will learn about what methods are, how to declare and define methods, and how to call methods in Java.
8-
---
8+
---
9+
10+
11+
# Method Declaration and Syntax in Java
12+
13+
## Introduction
14+
15+
Methods in Java are blocks of code that perform a specific task and can be called upon whenever needed. They help in organizing code, improving reusability, and making programs more modular and maintainable.
16+
17+
## Method Declaration
18+
19+
A method declaration in Java defines a method's name, return type, and parameters. Here's the basic syntax for declaring a method:
20+
21+
### Syntax
22+
23+
```java
24+
accessModifier returnType methodName(parameters) {
25+
// method body
26+
}
27+
```
28+
29+
### Example
30+
31+
```java
32+
public int add(int a, int b) {
33+
return a + b;
34+
}
35+
```
36+
37+
### Explanation
38+
39+
- **Access Modifier**: Defines the visibility of the method. Common access modifiers include `public`, `private`, `protected`, and the default (package-private).
40+
- **Return Type**: Specifies the type of value the method returns. If the method does not return any value, use `void`.
41+
- **Method Name**: The name of the method. It should be a valid identifier and follow camelCase naming convention.
42+
- **Parameters**: A comma-separated list of input parameters. Each parameter consists of a data type and a variable name.

docs/java/methods-and-functions/method-overloading-and-recursion.md

Lines changed: 137 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,141 @@ id: method-overloading-and-recursion
33
title: Method Overloading and Recursion
44
sidebar_label: Method Overloading and Recursion
55
sidebar_position: 3
6-
tags: [java, methods, functions, programming, java methods, java functions, method overloading, recursion]
6+
tags:
7+
[
8+
java,
9+
methods,
10+
functions,
11+
programming,
12+
java methods,
13+
java functions,
14+
method overloading,
15+
recursion,
16+
]
717
description: In this tutorial, we will learn about method overloading and recursion in Java. We will learn about what method overloading is, how to overload methods in Java, and how to use recursion to solve problems in Java.
8-
---
18+
---
19+
20+
# Method Overloading and Recursion in Java
21+
22+
## Introduction
23+
24+
Methods are an essential part of Java programming. Understanding advanced concepts like method overloading and recursion can help you write more efficient and flexible code.
25+
26+
## Method Overloading
27+
28+
Method overloading allows multiple methods with the same name but different parameters to coexist within a class. It is a way to define multiple methods that do similar things but with different input parameters.
29+
30+
### Syntax
31+
32+
```java
33+
returnType methodName(parameterList1) {
34+
// method body
35+
}
36+
37+
returnType methodName(parameterList2) {
38+
// method body
39+
}
40+
```
41+
42+
### Example
43+
44+
```java
45+
public class Main {
46+
public static void main(String[] args) {
47+
Main obj = new Main();
48+
49+
System.out.println(obj.add(5, 3)); // Calls add(int, int)
50+
System.out.println(obj.add(2.5, 3.7)); // Calls add(double, double)
51+
System.out.println(obj.add("Hello", "World")); // Calls add(String, String)
52+
}
53+
54+
public int add(int a, int b) {
55+
return a + b;
56+
}
57+
58+
public double add(double a, double b) {
59+
return a + b;
60+
}
61+
62+
public String add(String a, String b) {
63+
return a + " " + b;
64+
}
65+
}
66+
```
67+
68+
### Points to Remember
69+
70+
- Overloaded methods must have different parameter lists (number or type of parameters).
71+
- Overloaded methods can have different return types, but the return type alone is not sufficient to distinguish overloaded methods.
72+
- Overloaded methods can have different access modifiers and throw different exceptions.
73+
74+
## Recursion
75+
76+
Recursion is a programming technique where a method calls itself to solve a problem. It is useful for problems that can be broken down into smaller, similar subproblems.
77+
78+
### Syntax
79+
80+
```java
81+
returnType methodName(parameters) {
82+
if (baseCondition) {
83+
// base case to stop recursion
84+
return baseResult;
85+
} else {
86+
// recursive case
87+
return methodName(modifiedParameters);
88+
}
89+
}
90+
```
91+
92+
### Example: Factorial Calculation
93+
94+
```java
95+
public class Main {
96+
public static void main(String[] args) {
97+
Main obj = new Main();
98+
int number = 5;
99+
int result = obj.factorial(number);
100+
System.out.println("Factorial of " + number + " is " + result);
101+
}
102+
103+
public int factorial(int n) {
104+
if (n <= 1) {
105+
return 1;
106+
} else {
107+
return n * factorial(n - 1);
108+
}
109+
}
110+
}
111+
```
112+
113+
### Example: Fibonacci Series
114+
115+
```java
116+
public class Main {
117+
public static void main(String[] args) {
118+
Main obj = new Main();
119+
int number = 10;
120+
for (int i = 0; i < number; i++) {
121+
System.out.print(obj.fibonacci(i) + " ");
122+
}
123+
}
124+
125+
public int fibonacci(int n) {
126+
if (n <= 1) {
127+
return n;
128+
} else {
129+
return fibonacci(n - 1) + fibonacci(n - 2);
130+
}
131+
}
132+
}
133+
```
134+
135+
### Points to Remember
136+
137+
- A recursive method must have a base case to terminate the recursion and prevent infinite loops.
138+
- Recursive solutions are often more elegant but may be less efficient due to repeated calculations and function call overhead.
139+
- Consider the stack depth and memory usage when using recursion, as deep recursion can lead to `StackOverflowError`.
140+
141+
## Conclusion
142+
143+
Method overloading and recursion are powerful concepts in Java that enhance the flexibility and functionality of your code. Method overloading allows you to define multiple methods with the same name but different parameters, improving code readability and reusability. Recursion enables you to solve complex problems by breaking them down into simpler subproblems, though it requires careful handling to ensure termination and efficiency.

docs/java/methods-and-functions/method-parameters-and-return-values.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,85 @@ sidebar_label: Method Parameters and Return Values
55
sidebar_position: 2
66
tags: [java, methods, functions, programming, java methods, java functions, method parameters, return values]
77
description: In this tutorial, we will learn about method parameters and return values in Java. We will learn about how to define methods with parameters and return values, how to call methods with arguments, and how to handle return values from methods in Java.
8-
---
8+
---
9+
10+
11+
## Method Example
12+
13+
Here are a few examples to illustrate different types of methods:
14+
15+
### Method with No Parameters and No Return Value
16+
17+
```java
18+
public void printHello() {
19+
System.out.println("Hello, World!");
20+
}
21+
```
22+
23+
### Method with Parameters and a Return Value
24+
25+
```java
26+
public int multiply(int x, int y) {
27+
return x * y;
28+
}
29+
```
30+
31+
### Method with No Parameters but a Return Value
32+
33+
```java
34+
public String getGreeting() {
35+
return "Hello, Java!";
36+
}
37+
```
38+
39+
### Method with Multiple Parameters
40+
41+
```java
42+
public double calculateArea(double width, double height) {
43+
return width * height;
44+
}
45+
```
46+
47+
## Calling Methods
48+
49+
To call a method, use the method name followed by parentheses. If the method requires parameters, provide the appropriate arguments within the parentheses.
50+
51+
### Example
52+
53+
```java
54+
public class Main {
55+
public static void main(String[] args) {
56+
Main obj = new Main();
57+
58+
// Calling a method with no parameters and no return value
59+
obj.printHello();
60+
61+
// Calling a method with parameters and a return value
62+
int result = obj.add(5, 3);
63+
System.out.println("Sum: " + result);
64+
65+
// Calling a method with no parameters but a return value
66+
String greeting = obj.getGreeting();
67+
System.out.println(greeting);
68+
69+
// Calling a method with multiple parameters
70+
double area = obj.calculateArea(5.5, 4.0);
71+
System.out.println("Area: " + area);
72+
}
73+
74+
public void printHello() {
75+
System.out.println("Hello, World!");
76+
}
77+
78+
public int add(int a, int b) {
79+
return a + b;
80+
}
81+
82+
public String getGreeting() {
83+
return "Hello, Java!";
84+
}
85+
86+
public double calculateArea(double width, double height) {
87+
return width * height;
88+
}
89+
}

0 commit comments

Comments
 (0)