You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
-**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.
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.
- 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
+
publicclassMain {
96
+
publicstaticvoidmain(String[] args) {
97
+
Main obj =newMain();
98
+
int number =5;
99
+
int result = obj.factorial(number);
100
+
System.out.println("Factorial of "+ number +" is "+ result);
101
+
}
102
+
103
+
publicintfactorial(intn) {
104
+
if (n <=1) {
105
+
return1;
106
+
} else {
107
+
return n * factorial(n -1);
108
+
}
109
+
}
110
+
}
111
+
```
112
+
113
+
### Example: Fibonacci Series
114
+
115
+
```java
116
+
publicclassMain {
117
+
publicstaticvoidmain(String[] args) {
118
+
Main obj =newMain();
119
+
int number =10;
120
+
for (int i =0; i < number; i++) {
121
+
System.out.print(obj.fibonacci(i) +"");
122
+
}
123
+
}
124
+
125
+
publicintfibonacci(intn) {
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.
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:
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
+
publicclassMain {
55
+
publicstaticvoidmain(String[] args) {
56
+
Main obj =newMain();
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
0 commit comments