if statement - if else vs switch performance in java -


i wonder if there efficiency difference between using if statement or switch. example:

if(){ \\code } else if(){ \\code } else{ \\code } 

i believe program needs go , check of if statement if first if statement true.

switch(i){  case 1: //code break; case 2: \\code break; 

but in switch, there break command. approaching right? if not, explain efficiency difference between them?

switch perf better if else in case of switch there 1 time evaluation . once evaluated switch knows case needs executed in case of if else has go through conditions in case of worst scenario.

the longer list condition, better switch performance shorter list (just 2 conditions), can slower also

from why switch faster if

with switch jvm loads value compare , iterates through value table find match, faster in cases


Comments

Popular posts from this blog

html - Difficulties with background-image property -

visual studio code - What does the isShellCommand property actually do and how should you use it? -

ios - Segue not passing data between ViewControllers -