0
Can we use datatypes other than numeric ones on switch statement!??
confusion!!!!!!
4 Antworten
+ 3
Firstly, let me correct your question "Can we use datatypes other than numeric ones on switch statement!??". it is strictly recommended not to use double values with switch statement I won't go much into why that is the case within this answer so basically, you mean "can we use datatypes other than integer values with switch statement?". The answer is yes, I will give an example below:
String storeDay = "Friday";
switch(storeDay){
case "Monday" : System.out.print("it is monday!!");
break;
case "Tuesday" : System.out.print("it is Tuesday!!");
break;
case "Wednesday" : System.out.print("it is Wednesday!!");
break;
case "Thursday" : System.out.print("it is Thursday!!");
break;
case "Friday" : System.out.print("it is Friday!!");
break;
case "Saturday" : System.out.print("it is Saturday!!");
break;
case "Sunday" : System.out.print("it is Sunday!!");
break;
default: System.out.print(" invalid day entered");
}
Since you've seen the example above let's derive what is the purpose of a switch case?. Well you can think of a switch case as a better version of many if statements doing a certain task depending on its expression. So switch cases allow you to test a variable for equality against a list of values called "cases" and then performing a certain task depending on what case is triggered.
Finally, you can use Strings, Integer Values, characters, Enumerations with switch statements.
+ 1
yes for example strings, chars,...
+ 1
can u gimme example 😳
0
String name = "Kallzo";
Switch(name) {
case "Kallzo":
System.out.println("correct");
break;
default:
System.out.println("not my name");
}