Basic grammar of Java basic programming

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:

                       Package name: all letters are lowercase when multiple words are formed: xxxyyyzzz
                       Class name, interface name: when multiple words are formed, the first letter of all words is capitalized: XxxYyyZzz
                       Variable name and method name: when multiple words are formed, the first letter of the first word is lowercase, and the second word starts with uppercase of each word: xxxYyyZzz
                        Constant name: All letters are capitalized. When there are multiple words, each word is connected with an underscore: XXX_YYY_ZZZ 

    <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:

 

Copy code
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;">);
  }

}
}

Copy code

 

 

The output result is:

Value of X is 30

 

<2>switch-case

Copy code
switch(expression){
    case value :
       //Statement
       break; //optional
    case value :
       //Statement
       break; //optional
    //You can have any number of case statements
    default: //optional
       //Statement
}
Copy code

 

 

 The test code is as follows:

Copy code
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);

}
}

Copy code

 

The output result is:

Your grade is good

(2) Loop structure

Introduction to the loop structure:

 

 

 

   <1>for loop

     

 

 

 

 

Copy code
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");
}
}
}

Copy code

The above program for loop execution demonstration:

<2>while-do

 

 

 

Copy code
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");
      }
   }
}
Copy code

 

Results of the:

Copy code
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
Copy code

 

<3>do-while

 

 

 

Copy code
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 &lt; 20<span style="color: #000000;"> );   

}
}

Copy code

 

operation result:

Copy code
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
Copy code

 

 

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

 

Guess you like

Origin blog.csdn.net/weixin_41792162/article/details/108673773