-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava Methods-02.java
More file actions
100 lines (70 loc) · 2.91 KB
/
Java Methods-02.java
File metadata and controls
100 lines (70 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
-------------------------------------------------Java Methods------------------------------------------------------
*******there are two types of methods:
1.User-defined Methods: We can create our own method based on our requirements.
2.Standard Library Methods: These are built-in methods in Java that are available to use.
-----------------------------------User-defined Methods-----------------------------------------------------------
syntax........
returnType methodName() {
// method body
}
.......returnType - It specifies what type of value a method returns For example if a method has an int return type then it returns an integer value.
If the method does not return a value, its return type is void.
.........methodName - It is an identifier that is used to refer to the particular method in a program.
.........method body - It includes the programming statements that are used to perform some tasks. The method body is enclosed inside the curly braces { }.
For the Examples:
int addNumbers() {
// code
}
In the above example, the name of the method is adddNumbers(). And, the return type is int. We will learn more about return types later in this tutorial.
.....................................Calling a Method in Java........................................
In the above example, we have declared a method named addNumbers(). Now, to use the method, we need to call it.
// calls the method
addNumbers();
The user- defined Methods three types
.............sampl java methods
.............Java Return Type Methods
.............Java Parameters Methods
...................................sampl java methods...................................
public class simpleMethods {
public int addNumber(int a, int b){
int sum = a+b;
return sum;
}
public static void main(String[] args) {
simpleMethods s1 = new simpleMethods();
int res=s1.addNumber(43, 65);
System.out.println("the sum res "+res);
}
}
............................................Java ReturnType Methods...................................................
public class simpleMethods {
public static int square(int num) {
// return statement
return num * num;
}
public static void main(String[] args) {
int result;
result = square(10);
System.out.println("Squared value of 10 is: " + result);
}
}
.............................................Java Parameters Methods.......................................
int addNumbers(int a, int b) {
// code
}
int addNumbers(){
// code
}
public class simpleMethods {
public void display1() {
System.out.println("Method without parameter");
}
public void display2(int a) {
System.out.println("Method with a single parameter: " + a);
}
public static void main(String[] args) {
Main obj = new Main();
obj.display1();
obj.display2(24);
}
}