How to use the switch case statement in java

Source: Internet
Author: User
Tags case statement constant switch case

Switch syntax

The code is as follows:


Switch (expression)
{
Case constant expression 1: Statement 1;
....
Case constant expression 2: Statement 2;
Default: statement;
}

Default is to execute it if there is no matching case. default is not required.
The statement after case does not need braces.
The conditions for the switch statement can be int, byte, char, or short, but cannot be of other types.
Once the case matches, the subsequent program code will be executed sequentially, regardless of whether the subsequent case matches or not until the break is met. This feature allows several cases to execute Unified statements.

Example

The code is as follows:

Public class SwitchDemo {

Public static void main (String [] args ){
System. out. println (switchTest (4); // 55
 }

Private static int switchTest (int key ){

Int value = 0;
Switch (key ){
Case 1:
Value = 11;
Case 2:
Value = 22;
Case 3:
Value = 33;
Case 4:
Value = 44;
Default:
Value = 55;
  }

Return value;
 }
}

Output: 55

Switch-case considerations

The following is a simple method of switch:

The code is as follows:
Switch (){
Case B;
}

The value in part A must be of the int type, or can be automatically converted to the int type expression. That is to say, part A can be of the byte/short/char/int type.
It should be emphasized that the value of Part B in the program must be a single byte/short/char/int type value or a final type variable.
However, final variables also have requirements, that is, they must be constants during compilation. For more information, see the following program section:
Final int a = 0;
Final int B;
The second statement is a variable that cannot be recognized during compilation because it is not initialized. Of course, this statement is also incorrect.
Therefore, the value after case can be a constant value or a final value.

Let's look at the following section:

The code is as follows:

Public class TestSwitch {
Public static void main (String [] args ){
Byte a = 11;
Switch (a) {// C
Case 11: System. out. println ("11"); break;
Case 225: System. out. println ("11"); break; // D
}
}
}

Is the code correct? The answer is No. Although it is valid in C, that is, the value of the byte type can appear in the switch, but the statement at D, that is, the value after the second case, is 225, which exceeds the byte range, so it is incorrect. The value after case cannot be repeated. Therefore, pay attention to it.

==================================
In addition, the easiest thing to ignore when using the switch-case method is to forget to write the break statement after each case is processed. So what are the consequences? The following small program section will tell you:

The code is as follows:
Public class TestSwitchCase {
Public static void main (String [] args ){
Byte a = 2;
Switch (){
Case 1: System. out. println ("");
Case 2: System. out. println ("B ");
Case 3: System. out. println ("C ");
Case 4: System. out. println ("D ");
Default: System. out. println ("default ");
}
}
}
========= The output result is:
B
C
D
Default

--------------------------
Have you seen it? Even the default command is executed. Pay attention to the end character break; then OK.

A cannot be A variable in case.

In Java switch case, because switch case and if-else work differently, some companies may avoid using if-else and use the switch case method to solve the problem.

In jdk1.6:

The code is as follows:

Int I = "test". hashcode ();

String s = "test ";

Switch (s. hashcode ()){

Case I: System. out. println ("bad ");

} // This method is not supported. Compilation fails because I is a variable.

 

This function may be supported in jdk1.7.

However, the problem cannot be solved simply because many enterprises are not using version 1.7.

It's okay to come up with a solution in front of the computer. It should be feasible to solve the problem of case not using variables in jdk1.6.

Code on

The code is as follows:

Public class TestHash {

/**
* Comments:
* @ Author Jacky
* Chinese:
* @ Param args
*/
Public static void main (String [] args ){
 

String s = "test ";

 

Int I = "test". hashCode ();

 

Switch ("test1". hashCode () = I? 1:1) * ("test2". hashCode () = I? 2: 1) * (s. hashCode () = I? 3: 1 )){


Case 1: System. out. println ("not good"); break;


Case 2: System. out. println ("not good"); break;


Case 3: System. out. println ("good"); break;

 

Default: System. out. print (4); break;


} // Switch


} // Main

 

} // Class

 

OK, I am running, and the running result is print good

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.