What is the difference between an identifier and v

2019-01-22 02:54发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

Identifier are the names of variables and variables are storage locations of data.Variables point to the memory location where data is read and modified



回答5:

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.


回答6:

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.



回答7:

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'.



回答8:

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).



回答9:

Identifiers are the words which clarifying the variable but the variable is the space within the ram to store the data. Example: once you declare a variable such as: int god; the word int is the identifier and int god; is the variable declared to enter an integer data



回答10:

In simple words variable is something that occupy space in memory and identifier is the name to that location.



回答11:

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).



回答12:

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.