java - Modification of value in switch case -


public class sample { public static void main(string[] args) {     int = 9;     switch (i) {     default:         system.out.println("default");     case 0:         system.out.println("zero");         break;     case 1:         system.out.println("one");     case 2:         system.out.println("two");     } } } 

output:

default 0 

although i initialized 9, how possible case 0 picked? reason?

you miss break statement after first default case excution falls through second case.also put default case last excuted after other cases checked.

 public static void main(string[] args) {         int = 9;         switch (i) {          case 0:             system.out.println("zero");             break;         case 1:             system.out.println("one");             break;         case 2:             system.out.println("two");             break;         default:             system.out.println("default");             break;         }     } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -