Why is the scope of the switch statement in Java unlimited?

Why in Java this is a variable limited to a switch block, not a case block. For instance,

// Scope limited to a switch block switch (number) { case 1: String result = "..."; break; case 2: result = "..."; break; 

In the above example, the result needs only to be declared once. If you declare it twice, you will get a Duplicate local variable message.

My question is: how does the program know that you declared result if number = 2 ? (It will not fall into case 1 and will not declare a variable ... or will it?)

EDIT:

I could embarrass everyone. I understand how to limit the scope of a variable, but my question is: how does Java know that the result has been declared if it does not fall into this case?

+6
source share
7 answers

EDIT: Java uses lexical coverage (also called static coverage), so the scope of variables is determined at compile time and has nothing to do with actual evaluation.

Java is a block scope, so scope will be covered in {} in the example above.

See JLS 6.3 :

The scope of a local variable declaration in a block (Β§14.4) is the rest of the block in which the declaration appears, starting with its own initializer and including any other declarators on the right the local variable declaration statement.

+6
source

You can limit the scope to case blocks by adding curly braces as follows:

 // Scope limited to a switch block switch (number) { case 1:{ String result = "..."; break; } case 2:{ String result = "..."; break; } 
+5
source

Grandpa's Fortran language has a computed GOTO statement

  GOTO expr ... 1 ... ... 2 ... ... 

based on the value of expr , the code jumps to 1, 2, etc.

Operator

The C (and Java) switch is basically a computed GOTO. We have a continuous piece of code with some shortcuts, and we move on to one of the shortcuts. If there is no break , we will execute the rest of the block.

This mechanism with a fairly low level of control contradicts the intuition of modern programmers; we would think that the switch statement selects one sentence and executes that sentence like the if-elseif-elseif-...-else .

Java inherited the semantics of C-switches because they did not want to deviate too much from C. Newer languages ​​are unlikely to continue this error.

+3
source

Java uses the scope of the region, the choice of cases is not blocks (they are more like labels). This will work:

 switch (key) { case 1: { String result = "1"; return result ; } case 2: { String result = "2"; return result ; } } 
+2
source

What do you mean? To understand the answer, you need to know how compilers work. Think of the switch as a large block with several goto statements at the end of each switch statement.

I'm not sure how the java unroll switch instructions are, but one simple and easy way to do this is (byte code):

  if (number == 1) goto label1; if (number == 2) goto label2; goto labelEnd; label1: String result = "..."; goto labelEnd; label2: result = "..."; goto labelEnd; labelEnd: <some code> 
+2
source

The reason why the scope of variables is limited by the switch block, unlike the case block, is java, which allows you to fail from one case block to the next ie

 switch (number) { case 1: .... some code ... case 2: .... some more code ... 

in this case, if the number is 1, both case 1 and case 2 are fulfilled. The gap acts as the final end of the selection, it never ends. The volume of the variable must be select level .

As others have claimed, use a block to limit the scope of ie

 switch (number) { case 1:{ String result = "..."; break; } case 2:{ String result = "..."; break; } 
0
source

Because there is no β€œcase block".

Everything inside the switch statement is in one block. You cannot declare the same variable twice in the same block.

If you need a "block block", you must write { and } yourself.

0
source

All Articles