-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
99 lines (83 loc) · 2.78 KB
/
Copy pathMain.java
File metadata and controls
99 lines (83 loc) · 2.78 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
97
98
99
// main.java
public class Main {
// Method Parameters
// Metotlara dışarıdan veri iletmemizi sağlar
public void printText(String text) {
System.out.println(text);
}
// Birden fazla parametre kullanımı
public void printTwoTexts(String text1, String text2) {
System.out.print(text1);
System.out.print(text2);
}
// Parameters vs. Variables
// Parametreler de değişken gibidir, değeri okunabilir ve değiştirilebilir
public void modifyText(String text1, String text2) {
text1 = "Yeni Değer 1";
text2 = "Yeni Değer 2";
System.out.println(text1 + ", " + text2);
}
// Final Parameters
// Final parametreler yeniden atanamaz
public void printFinalTexts(final String text1, final String text2) {
System.out.println(text1);
System.out.println(text2);
// text1 = "hatalı"; // Bu satır hata verir çünkü final
}
// Local Variables
// Metot içerisinde tanımlanırlar ve sadece o metotta geçerlidir
public void sumLocalVariables() {
int a = 5;
int b = 10;
System.out.println(a + b);
}
// Method Return Types
// Metotların geriye değer döndürmesini sağlar
public int sum(int x, int y) {
return x + y;
}
public String concat(String a, String b) {
return a + b;
}
// Multiple Return Statements
public String conditionalConcat(String s1, String s2, boolean reverse) {
if (reverse) {
return s2 + s1;
}
return s1 + s2;
}
// Access Modifiers (Erişim Belirleyiciler)
// public, private, protected (detayları başka dosyada)
public void publicMethod() {
System.out.println("Erişim tipi: public");
}
// Exception Declarations
public String safeConcat(String s1, String s2) throws Exception {
if (s1 == null) throw new Exception("s1 null");
if (s2 == null) throw new Exception("s2 null");
return s1 + s2;
}
// Calling Methods (Metot Çağırma)
public void callSum() {
int result = sum(3, 7);
System.out.println("Toplam: " + result);
}
public static void main(String[] args) {
Main m = new Main();
m.printText("Merhaba!");
m.printTwoTexts("Hello", " World");
m.modifyText("Deneme1", "Deneme2");
m.printFinalTexts("A", "B");
m.sumLocalVariables();
System.out.println(m.sum(4, 6));
System.out.println(m.concat("Emre", "Karaman"));
System.out.println(m.conditionalConcat("A", "B", true));
m.publicMethod();
try {
System.out.println(m.safeConcat("Open", "AI"));
} catch (Exception e) {
System.out.println(e.getMessage());
}
m.callSum();
}
}