-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathLocalVariablesDemo.java
More file actions
48 lines (38 loc) · 1.38 KB
/
LocalVariablesDemo.java
File metadata and controls
48 lines (38 loc) · 1.38 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
public class LocalVariablesDemo {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) { // i declared as a local variable
int x = i * 2; // x is a local variable
System.out.println("i = " + i + ", x = " + x);
}
int number = 10;
if (number > 0) {
int square = number * number;
System.out.println("The square of " + number + " is " + square);
} else {
int absoluteValue = -1 * number;
System.out.println("The absolute value of " + number + " is " + absoluteValue);
}
}
public void myMethod() {
int x = 10;
System.out.println(x);
int number = 2;
switch (number) {
case 1:
System.out.println("Odd number");
// System.out.println(i); // Compilation error as i is never declared
break;
case 2:
int i = number; // local variable i declared first time
System.out.println(i);
System.out.println("Even number");
break;
default:
i = 10; // local variable i can be accessed as it is declared in the top
System.out.println(i);
System.out.println("Invalid number");
break;
}
// System.out.println(i);
}
}