close

In the last chapter, I talked about basic variable types, literal values, and type conversion. Today I will continue to talk about the remaining parts of variables, such as naming rules, the scope, the final variable, the variable expression and the block.

  1. Variable naming rules
  2. Variable names can only use the letter, number, $and _

The first character of the variable can only use letters, $ _, not numbers.

The specific code is as follows:

int a=1;

int _b=2;

int $=3;

int $a=4;

int 123a=5;//error: The naming method starting with a number is wrong.

  • Use full words instead of abbreviations

When naming, try to use complete words, such as name, armor, instead of using the abbreviations n, a.

The specific code is as follows:

String name;

double defensive;

double armor;

int moveSpeed;

  • Practice variable naming convention

Consider whether the following variable naming conforms to the specification?

1. int b_;

2. int a@;

3. int d3;

4. int 1@;

5. int 6_;

6. int X$_;

7. int y2;

8. int _$_;

9. int $_$;

10. int $*$;

  1. Variable scope

When the variables are in different positions, there are different names, and different names correspond to fields, properties, parameter and local variable.

  • Fields or properties

When a variable is declared under the class, the variable is called the field or property. The specific code is as follows:

public class essay {        

                        static int i=1;

                        static int j=2;

            public static void i()

            {

                        System.out.println(i);

            }

            public static void j()

            {

                        System.out.println(j);

            }         

            public static void main(String[] args) {

                i();

                j();

            }

            }

As we all see from this code, the variables I and the variable j are defined under the class, and they are collectively called fields or properties. Its scope is from the beginning of the variable declaration to the end of the entire class.

  • Parameter

If a variable is declared on a method, it is called a parameter. The scope of the parameter is all the code in the method. Other methods cannot access the parameter. The parameter cannot be accessed in the class.

The specific code is as follows:

public static void i1(int i)//The scope of parameter i is on method i1

            {

                        System.out.println(i);

            }

            public static void i2()

            {

                        System.out.println(i);//i2 cannot access parameter i

            }         

  • Local variable

The variables declared in the method are called local variables.

Its scope is at the beginning of the declaration to the end of the block where it is located.

The specific code is as follows:

public class essay {

                            public void i1() {

                                int i  = 1;  //Its scope is from the 3rd line of the declaration to the 11th line at the end of the block.

                                System.out.println(i);

                                {                          //Sub-block

                                    System.out.println(i); //can access i

                                    int j = 5;

                                    System.out.println(j); //can access j

                                }

                                System.out.println(j); //Can’t access j, because its scope ends at line 10.

                            }

                        }

  • Exercise-scope

The scope of the attribute is in the method, and the scope of the parameter is also in the method. What if the name of the attribute and the parameter are the same? So which value is finally in the console?

The specific code is as follows:

public class essay {

                    int i= 1;

                            public void i1( int i) {

                                System.out.println(i);

                            }

                            public static void main(String[] args) {

                                                new essay().i1(2);

                             }

}

The final console output is 2

When the accessed variable is affected by multiple scopes, the value is taken according to the principle of proximity.

  1. Variable final

When final modifies a variable, the value of the variable cannot be changed.

In other words, when a variable is modified by final, the variable has only one chance of assignment.

  • Assignment at the time of declaration

The specific code is as follows:

public class essay {

                    final int i= 1;

                            int i=2;//

                            public static void main(String[] args) {

                                                System.out.println(i);//i has been modified by final in the second line, so the assignment cannot be repeated in the third line.

                                    }

}

Therefore, this code will eventually cause compilation errors.

  • No assignment at the time of declaration

If there is no assignment at the time of declaration, then the only assignment can be made in the behind of the code.

The specific code is as follows:

public class essay {

                            public static void main(String[] args) {

                                      final int i;

                                            i=1;//i is only declared in the third line, but not assigned, so the only assignment can be made in the fourth line.

                                                System.out.println(i);

                            }

}

The final console output is 1.

  • Final modifies others

In addition to modifying variables, final can also modify classes and methods, which will be expanded in subsequent chapters.

  • Exercise-final

If the final modified data type is a parameter, can you assign a value to this parameter in the method?

The specific code is as follows:

public class essay {

                          public void i1(final int i){

                                      i=1;//Can this line of code be executed?

                          }            

}

The answer is no. because when the method i1 is called, it must be assigned for the first time, and it cannot be assigned multiple times later.

  1. Variable expression

A piece of code ended in ; is defined as an expression.

The specific code is as follows:

int j=10;

System.out.println(j);

These two lines of code are expressions.

At the same time, in the expression,; is also a complete expression.

The specific code is as follows:

public class essay {

            public static void main(String[] args) {

            ;

            ;

            ;

            ;

            ;

            }

}

  • The block

The definition of the block is from { start to} end.

The specific code is as follows:

public class essay {//Block for class

            public static void main(String[] args) {//The block for the main method

                        int j=10;

                        System.out.println(j);

            }

}

Today’s tutorial is over! I believe that through the study of this chapter, you have basically mastered the concept of the variable naming rules, the scope, the final variable, the variable expression and the block.

Tags : Common Errors in Variable DefinitionsVariable naming rules

Leave a Response