简体   繁体   中英

What is the difference between an identifier and variable?

I'm a bit confused about identifiers. In my textbook it says, "We use identifiers to name variables (and many other things) in Java."

I'm not really sure what this means. Is it like assigning a variable...to a variable? What?

So far, I'm getting this impression:

int a, b, c; a = 2; b = 99; c = a + b;

Is c an identifier? When it says, "Using identifiers to name variables," are identifiers like int , double , boolean , things used to categorize variables? Please provide some examples.

You can think of an identifier as variable's name. I wouldn't get too worked up about it.

For example:

int a;
a = 15;

In this example, a is a identifier that refers to the variable with the same name. If a weren't a variable but a function:

int a()
{
}

a();

Then a would still be an identifier but it would identify a function. Just as "Reena" can identify both a person and some kind of a non-profit organization.

An identifier is just the name of the variable. So for the variable c , the identifier is the actual text c , which is just your way of referring to the variable. It's possible (as you'll learn later on) that you can have multiple identifiers for the same variable, kinda like how in real life a person can have multiple names.

Don't worry too much about it right now, just keep trying and focusing on the more important stuff like loops, conditionals, classes, etc.

I agree with the GNU C's definition that says that there are 5 tokens in C: keywords, identifiers, operators, constants and separators. The same goes for Java.

int is not an identifier because it is defined as a keyword. Operators are excluded from the list of valid identifier symbols. Constants are just values, but the name that refers to a constant is an identifier.

Basically: any 'word' (a group of 1 or more valid identifier symbols) is an identifier as long as it isn't on the list of keywords. In Java, the valid identifier symbols are Unicode letters, digits, dollar and underscores. Digits can't be the first symbol. Dollars and underscores, though valid, shouldn't be used with the exception of as a separator in multi-word constant identifiers like MY_MAGIC_NUMBER = 6 .

标识符是变量的名称,变量是数据的存储位置。变量指向读取和修改数据的内存位置

In Java, identifier is the name given to a program element, where a program element could be a package, type (class, interface, enum, annotation), fields (instance/static variables ), enum constants, annotation type elements, methods, local variables , and parameters. Essentially, an identifier identifies a program element.

For example, in the following code,

package declarations;

public class A {

    private int value = 1;

    private enum SUITS {CLUB, DIAMOND, HEART, SPADE};

    public A(int val) {
        this.value = val;
    }

    public int doSomething() {
        int c = value + 10;
        return c;
    }
}

// declarations, A, value, SUITS, CLUB, DIAMOND, HEART, SPADE,
// A, val, value, val, doSomething, c, value, c are the 
// identifiers in order of their occurrence.

Identifier is just like the name of any user define function such as

int gcd(int,int);

Here the name of function gcd is an identifier.

And variable is uses for assigining the value these may vary during the executaion, such as

int a,b;
int a=10;

In the above two expression first is declare the variable and second we assign the value 10. Here a and b are variables.

If I understand this correctly this seems to be an example of over-engineering (in the context of over-thinking variable names (Identifiers))

// Variable definition

int myVariable = 42;

technically speaking

42 is the variable (assigned value).

myVariable is the Identifier (name we create to store the variable)

I typically work under the generic idea of referring to Identifiers as variables and if you wanted to get technical, make the distinction that the "name we create to store the variable is the 'variable name' and the value stored in the 'variable name' is the 'variable value'.

I would say that a variable is a defined storage region ; the fact there is a place in your RAM for you where you can save values. A variable identifier is the name you give to that region.

Of course, an identifier can name more than variables. A function (a pack of code with in/out parameters) also has a name: the function identifier . A type (value domain), also has a name, the type identifier . A class (type with function members), has the class identifier , and so on.

For example, '4' is a value. Even more, it's an integer. What if you want to manage your own integers? You need a storage region to save integers, a name to refer to that storage region, you need to define what kind of values (type/domain) you want to save in that region, and a name to refer and to specify that type. So, you write something like:

int var;

If the Java parser sees a sentence with the following structure: name name; , he knows you are creating a storage region. The first part specifies the type of values that region is intended for, and the only mean to specify it is writting the type identifier of that type, because the type itself is an abstract notion (for example, int for the abstract notion of something being an integer).

The second part is the name you want to give to that region, to use it later, because refering to a storage region using a name is simpler than using any other mean (for example, memory direction or whatever; left the language manage that for you).

Identifiers are the words that represent a variable or function, but the variable is the space within the ram to store the data. For example: once you declare a variable such as: int god; the word god is the identifier and int god; is the variable declared to enter an integer data.

简单来说,变量是占用内存空间的东西,标识符是该位置的名称。

Identifiers are names given to things in Java. This includes variables, methods, classes, etc. So, technically a variable is an identifier. see: https://www.cs.umd.edu/~clin/MoreJava/Intro/var-ident.html

Regarding your confusion on "I'm not really sure what this means. Is it like assigning a variable...to a variable? What?" --> the correct way of saying is: Assigning a "name" to variable (what name would you like for a variable).

In C/C++, the names of variables, functions, labels, and various other user-defined objects are called identifiers. 16th page, C++: The Complete Reference, Herbert Schildt.

I'll try to do this without giving an example.

Every piece of data in the program is stored somewhere in the computer's memory, and its address is given a name. The name used to refer to this memory location is called identifier .

If the data at this location can be changed at runtime this identifier is called a variable . If not, we say it is a constant .

adding to above answers

every variable is an identifier but every identifier is not a variable.

ex1) name of a function is an identifier but not variable

Identifier is used to identify variables like your name identifies you but you are not your name, you are just identified by it. Identifier can also be used to identify functions as pointed out by the top rated answer of user267885. int is not an identifier but a keyword declaring the type of the variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM