What is the difference between an identifier and variable?

52,016

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

Solution 2

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.

Solution 3

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.

Solution 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

Solution 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.
Share:
52,016

Related videos on Youtube

Reena
Author by

Reena

Updated on July 11, 2022

Comments

  • Reena
    Reena almost 2 years

    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.

  • Jean-François Côté
    Jean-François Côté over 10 years
    Please use the code formating next time. I have edited your answer for now.
  • urso
    urso over 9 years
    This question intrigued me because it is a simple concept but often confusing to beginners such as myself trying to get our heads around the larger concepts of Java programming. So I did some digging and found what I think is a good definition of a Java Identifier (reference: Beginning Java 8 Fundamentals by Kishori Sharan). To Summarize: An Identifier is simply the technical term for a name given to a class, method, variable, file etc. in a java program.
  • papacito
    papacito almost 9 years
    Your answer is misleading. "int" is not an identifier - it is a variable type. An identifier in your example is the word "god", which is also a variable with the same name. "int god" is a declaration of a variable of type int.
  • MCG
    MCG over 8 years
    Good analogy between Reena as a person and Reena as organization
  • Admin
    Admin almost 7 years
    Very nice example.
  • Blaisorblade
    Blaisorblade over 6 years
    Multiple identifiers for the same variable? I've only seen that in C++. You can have multiple references to the same object, but an object is not a variable. Conversely, multiple variables with the same identifier are possible in every language with shadowing, namespaces, or even just scope.
  • barlop
    barlop over 6 years
    an identifier is not just a variable's name, as you even show later in your answer, so your first sentence is misleading
  • barlop
    barlop over 6 years
    identifiers are not just names of variables. you should give a definition of identifier
  • barlop
    barlop over 6 years
    identifiers can hold constants too, not just variables, so your definition is wrong even in your first sentence. And the name of a class is an identifier too
  • barlop
    barlop over 6 years
    your definition of identifier could be improved if you cited a source, also, you missed out constants. So if you had a source and included a complete definition for identifier in java then that'd be good
  • Fourat
    Fourat about 5 years
    Can you please explain further your solution ?
  • viswanath
    viswanath about 5 years
    i meant, a function name is not a variable since we don't intend to store any value in it although a function's name itself will store the address of first statement of the function.