The basic grammar of JAVA basic programming only needs to include two parts: variables and operators, and flow control.
Java basic knowledge diagram:
One, variables and operators
(1) Variable
<1>Identifier
Naming conventions in Java:
<2> Variable
For developers, variables are aliases used to describe a piece of information, and one or more variables can be used in the program code. Variables can store various types of information, such as login information, version name, file size, a certain English word, and airline ticket prices. There are usually two ways to assign a variable, define it first, assign it later, or assign it directly at the time of declaration. Variables usually include local variables, member variables and static variables, and different variables have their scope;
Comparison of different types of variables:
Classification of variables:
There are eight common data types: basic data types include boolean (boolean), float (single-precision floating-point), char (character), byte (byte), short (short integer), int (integer) Type), long (long integer) and double (double-precision floating point
Default value of basic data type:
Conversion between basic data types:
When forced type conversion:
Second, process control
Process control refers to the execution sequence of code blocks during the running of the program. It can be divided into three categories: sequence structure, loop structure, and branch structure. The knowledge structure is as follows:
(1) Sequence structure
The sequence structure means that the program is executed sequentially from front to back.
(2) Branch structure
<1> if-else structure
code show as below:
public class Test {
public static void main(String args[]){
int x = 30;
</span><span style="color: #0000ff;">if</span>( x == 10<span style="color: #000000;"> ){
System.out.print(</span>"Value of X is 10"<span style="color: #000000;">);
}</span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>( x == 20<span style="color: #000000;"> ){
System.out.print(</span>"Value of X is 20"<span style="color: #000000;">);
}</span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>( x == 30<span style="color: #000000;"> ){
System.out.print(</span>"Value of X is 30"<span style="color: #000000;">);
}</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
System.out.print(</span>"这是 else 语句"<span style="color: #000000;">);
}
}
}
The output result is:
Value of X is 30
<2>switch-case
switch(expression){ case value : //Statement break; //optional case value : //Statement break; //optional //You can have any number of case statements default: //optional //Statement }
The test code is as follows:
public class Test {
public static void main(String args[]){
//char grade = args[0].charAt(0);
char grade = ‘C’;
</span><span style="color: #0000ff;">switch</span><span style="color: #000000;">(grade)
{
</span><span style="color: #0000ff;">case</span> 'A'<span style="color: #000000;"> :
System.out.println(</span>"优秀"<span style="color: #000000;">);
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">case</span> 'B'<span style="color: #000000;"> :
</span><span style="color: #0000ff;">case</span> 'C'<span style="color: #000000;"> :
System.out.println(</span>"良好"<span style="color: #000000;">);
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">case</span> 'D'<span style="color: #000000;"> :
System.out.println(</span>"及格"<span style="color: #000000;">);
</span><span style="color: #0000ff;">case</span> 'F'<span style="color: #000000;"> :
System.out.println(</span>"你需要继续努力"<span style="color: #000000;">);
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">default</span><span style="color: #000000;"> :
System.out.println(</span>"无效等级"<span style="color: #000000;">);
}
System.out.println(</span>"你的等级是 " +<span style="color: #000000;"> grade);
}
}
The output result is:
Your grade is good
(2) Loop structure
Introduction to the loop structure:
<1>for loop
public class Test {
public static void main(String args[]) {
for(int x = 10; x < 20; x = x+1) {
System.out.print(“value of x : " + x );
System.out.print(”\n");
}
}
}
The above program for loop execution demonstration:
<2>while-do
public class Test { public static void main(String args[]) { int x = 10; while( x < 20 ) { System.out.print("value of x : " + x ); x++; System.out.print("\n"); } } }
Results of the:
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19
<3>do-while
public class Test {
public static void main(String args[]){
int x = 10;
</span><span style="color: #0000ff;">do</span><span style="color: #000000;">{
System.out.print(</span>"value of x : " +<span style="color: #000000;"> x );
x</span>++<span style="color: #000000;">;
System.out.print(</span>"\n"<span style="color: #000000;">);
}</span><span style="color: #0000ff;">while</span>( x < 20<span style="color: #000000;"> );
}
}
operation result:
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19
About break and continue:
Both of these keywords are jumping out of the loop, but the difference is that break refers to jumping out of the loop body and executing the subsequent code, while continue refers to jumping out of the current loop and executing the next loop.
2020-09-15 18:26:33