The difference between a variable and an identifier

I am new to programming and learning Java these days. I read that Identifiers are "names given to variables and many other things in java classes, etc.". But I confused that if the identifier is the name of the variable, so the variable will have its own identity. For example, I have a book in the real world that can be a variable in programming, and its name is Book, so Book will be both Variable and Identifier. How these two things are different and different.

+4
source share
3 answers

Each variable has a name, which is an identifier. Similarly, each class has a name, which is also an identifier - like the name of the method and the name of the package. There are restrictions on how an identifier can look - for example, it cannot start with a number or include spaces.

So, for example, in this program:

public class Test {
    public static void main(String[] args) {
        int x = 0;
        System.out.println(x);
    }
}

The following identifiers are used:

  • Test
  • main
  • args
  • x
  • System
  • out
  • println

However, only argsand xare the variables declared in the code you provided. outis also a variable, but declared in a type System.

The same identifier can refer to different things in different contexts, even within the same program. For instance:

public void method1() {
    String x = "";
    System.out.println(x);
}

public void method1() {
    int x = 0;
    System.out.println(x);
}

Here, the identifier is xused in both methods - but each time it refers only to a variable declared inside the method.

, , .

, , , , ... , , . , ( ) .

+9

- , , .. , .

, , , , .

, " " , " "

+8

Identifier - This is a token that follows the rules of the token, and can also be used to identify something. the identifier can also be used for the name: Variables / Literals / Keywords / Class / Method .............. etc. A variable is an identifier that is used to store some value. The value contained in the variable can be changed (changed) at any time during program execution.

0
source

Source: https://habr.com/ru/post/1569537/


All Articles