Java switch cases: with or without braces?

前端 未结 8 1839
猫巷女王i
猫巷女王i 2020-12-23 11:05

Consider the following two snippets, with braces:

switch (var) {
  case FOO: {
    x = x + 1;
    break;
  }

  case BAR: {
    y = y + 1;
    break;
  }
}
<         


        
8条回答
  •  余生分开走
    2020-12-23 11:42

    is there any sort of performance penalty for using the braces with a case?

    None.

    The curly braces are there to help the compiler figure out the scope of a variable, condition, function declaration, etc. It doesn't affect the runtime performance once the code is compiled into an executable.

提交回复
热议问题