-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethod.java
More file actions
65 lines (43 loc) · 1.62 KB
/
Copy pathmethod.java
File metadata and controls
65 lines (43 loc) · 1.62 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
public class method {
// main() method se program start hota hai
public static void main(String[] args) {
System.out.println("Program Start");
// Method ko call kar rahe hain
printMessage();
// greetUser method ko value pass kar rahe hain
greetUser("Krishna");
// addNumbers method do numbers lega
addNumbers(10, 20);
// getSum method result return karta hai
int answer = getSum(50, 30);
// Returned value ko print kar rahe hain
System.out.println("Returned Sum = " + answer);
System.out.println("Program End");
}
// 1. Simple method
// Ye method koi value receive nahi karta
// Ye method koi value return bhi nahi karta
public static void printMessage() {
System.out.println("Hello! Ye ek simple method hai.");
}
// 2. Method with parameter
// String name ek parameter hai
// Method call karte time name ki value deni hogi
public static void greetUser(String name) {
System.out.println("Hello " + name);
System.out.println("Welcome to Java Methods");
}
// 3. Method with multiple parameters
// int number1 aur int number2 parameters hain
public static void addNumbers(int number1, int number2) {
int sum = number1 + number2;
System.out.println("Sum = " + sum);
}
// 4. Method with return value
// int ka matlab ye method integer value return karega
public static int getSum(int number1, int number2) {
int sum = number1 + number2;
// return method ka result wapas bhejta hai
return sum;
}
}