Switch Multi-Select Structure
Another way to implement the multi-select structure is the switch case statement
The switch case statement determines whether a variable is equal to a certain value in a series of values, and each value is called a branch.
The variable type in the switch statement can be:
switch(expression){ case value: //Sentence break;//Optional case value: //Sentence break;//Optional //You can have any number of case statements default://Optional //Sentence}
byte, short, int or char
Starting with Java SE 7
switch supports String type
At the same time, the case tag must be a string constant or literal
public static void main(String[] args) { //case penetration //switch matches a specific value char grade = 'B'; switch (grade){ case 'A': ("excellent"); break;//Optional case 'B': ("good"); break;//Optional case 'C': ("Pass"); break;//Optional case 'D': ("make persistent efforts"); break;//Optional case 'E': ("Failed"); break;//Optional default: ("Unknown Level"); } } //Output resultgood
public static void main(String[] args) { String name = "Cao Yanbing"; //The new feature of JDK7, the expression result can be a string! ! ! //The essence of characters is still numbers switch (name){ case "Cao Yanbing": ("I have the final say on this street!"); break; case "Cao Xuanliang": ("You will be your brother in the next life, and I will be your younger brother!"); break; default: ("You've found the wrong person!"); } }
Circular structure
While loop
do...while loop
for loop
Introduced an enhanced for loop mainly for arrays in Java5
While loop
While is the most basic loop, its structure is:
while(Boolean expressions) { //Loop content}
As long as the boolean expression is true, the loop will continue to execute.
In most cases, we will stop the loop, and we need a way to invalidate the expression to end the loop.
A few cases require loop execution, such as the server's request response listening, etc.
If the loop condition is always true, it will cause an infinite loop [dead loop]. In normal business programming, we should try to avoid dead loops. It will affect the performance of the program or cause the program to crash!
Thinking: Calculate 1+2+3+...+10=?
public static void main(String[] args) { //Output 1~100 int i = 0; while (i < 100) { i++; (i); } }
public static void main(String[] args) { //The dead cycle while (true){ //Waiting for the client to connect //Timed check //... } }
public static void main(String[] args) { //Calculate 1+2+3+...+100=? //Gauss's Story int i = 0; int sum = 0; while (i <= 100) { sum = sum + i; i++; } (sum); } //Output result5050
do...while loop
For a while statement, if the condition is not met, the loop cannot be entered. But sometimes we need to execute it at least once even if the conditions are not met.
The do...while loop is similar to a while loop, except that the do...while loop is executed at least once.
do{ //Code statement}while(Boolean expressions);
The difference between while and do-while:
While judge first and then execute. do-while is to execute first and then judge!
do-while always ensures that the loop will be executed at least once! This is their main difference.
public static void main(String[] args) { int i = 0; int sum = 0; do { sum = sum + i; i++; }while (i <= 100); (sum); } //Output result5050
public static void main(String[] args) { int a = 0; while (a < 0) { (a); a++; } ("===================="); do { (a); a++; }while (a < 0); } //Output result==================== 0 Process finished with exit code 0
Daily Java Interview Questions
What are the uses of keywords?
static stands forStatic", which can be used to modify:
Static inner class (static inner class can be instantiated without relying on external class instance objects, while inner class needs to be instantiated after external class is instantiated)
Static methods (static methods belong to class methods and can be called without instantiating objects)
Static variables (static variables belong to classes and can be called without instantiating objects)
Static code blocks (static code blocks will only be executed once when the class is loaded)
The usage sample code is as follows:
public class Test1 { static { ("Static Code Block"); } static class Test2 { } private static int id = 0; public static void staticMethod() { } }
What is the difference between a variable and a normal variable?
Different goals
Static variables belong to variables of the class, and ordinary variables belong to variables of the object.
Different storage areas
Static variables are stored in the static area of the method area, and ordinary variables are stored in the heap area.
in addition:JDK7And above,Static variables are stored in their correspondingClassIn the object,andClassObjects are the same as other normal objects,All stored in the heap。
Different loading times
Static variables are loaded as the class is loaded and disappear as the class disappears.
Normal variables are loaded as the object is loaded and disappear as the object disappears.
Different calls
Static variables can only be called through class names and objects, while ordinary variables can only be called through objects.
The difference and usage of final, finally, finalize
final
Final is a modifier and a keyword
Classes modified by final cannot be inherited
-
For a final variable, if it is a variable of the basic data type, its value cannot be changed once it is initialized;
If it is a variable of reference type, it cannot be allowed to point to another object after initializing it. But the content of the object it points to is mutable.
-
Methods modified by final will not be rewritten, but reloading is allowed
Note: The private method of the class will be implicitly specified as a final method.
-
finally
Finally is a keyword
Finally provides finally blocks to perform any clearing operations when exception handling is performed. Regardless of whether an exception is thrown or caught, the finally block will be executed, usually used to free resources.
-
Under normal circumstances, the finally block will definitely be executed. But there are at least two extremes:
If the corresponding try block is not executed, the finally fast try block will not be executed
If jvm is shut down in the try block, for example (n), then finally block will not be executed (all powered off, how to execute it)
If there is a return statement in the finally block, the return statement in the try or catch will be overwritten, resulting in the inability to return both. Therefore, it is strongly recommended that the return keyword should not exist in the finally block.
finalize
finalize() is a protected method of the Object class, and the subclass can override this method to achieve resource cleaning.
GC will call this method before recycling the object
There are many problems with the finalize() method:
Java language specifications and ensures that finalize methods will be executed in a timely manner, and there is no guarantee that they will be executed at all.
The finalize() method may cause performance problems, because the JVM usually completes the finalize execution in a separate low priority thread.
In the finalize() method, the object to be recycled can be assigned to the object reference that can be reached by GC Roots, thereby achieving the purpose of object regeneration.
The finalize method is executed by GC at most once (but the finalize method of the object can be called manually)
This is the article about switch multi-select structure and loop structure. For more related switch multi-select structure and loop structure, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!