forked from bhagat-hrishi/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitch.java
More file actions
61 lines (48 loc) · 1.63 KB
/
Copy pathSwitch.java
File metadata and controls
61 lines (48 loc) · 1.63 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
public class Switch {
public static void main(String[] args) {
//Except switch else were {} are optional
//switch(5)//error
//cases and default is optional
// switch(90)//valid
// {
// Every statement inside switch must be under some case (or) default.
// Independent statements are not allowed
// System.out.println("I am independent statement");//error
// }
int x=90;
final int y=2;
switch (90) {
//Every case lable should be compile time constant else we get Error
case x://error
System.out.println("cdcd");
break;
case y://This is valid as y is final variable
;
default:
System.out.println(" I addcd");
break;
}
//Every Case label should be in range of Switch argument type
//here we are using byte so case label should in range of byte (-256 to 255)
byte b = 10;
switch (b)
{
case 10:
System.out.println("10");
case 100:
System.out.println("100");
case 1000://error
System.out.println("1000");
}
//Within switch statement we can default only once
switch(89)
{
case 89:
break;
default:
;
default://error
;
}
}
}