yamilelias
yamilelias

Reputation: 305

For what is the symbol $ after a variable in Java?

I was reviewing a program I found in internet and I'm trying to figure out how it works, but I can't find anything about this function that could help me.

 public static double regress(double x, ArrayList<Double> terms) {
    double a = 0.0;
    int exp = 0;
    Iterator<Double> i$ = terms.iterator();
    while (i$.hasNext()) {
        double term = i$.next();
        a += term * Math.pow(x, exp);
        ++exp;
    }
    return a;
}

I know the method is iterating in an ArrayList to calculate a value, but what does it mean this line Iterator<Double> i$ = terms.iterator();? More specific, for what is this symbol $ used after the variable i?

Upvotes: 0

Views: 1655

Answers (5)

Nicolas Filotto
Nicolas Filotto

Reputation: 44965

i$ is simply the name of your variable indeed $ is a character that you can use to define the name of a Java Identifier. To check if a character is allowed (in a Java identifier as other than the first character) you can use Character.isJavaIdentifierPart(char), if you test it with $, you will get true as mentioned in the Javadoc:

A character may be part of a Java identifier if any of the following are true:

  • it is a letter

  • it is a currency symbol (such as {@code '$'})

  • it is a connecting punctuation character (such as {@code '_'})

  • it is a digit

  • it is a numeric letter (such as a Roman numeral character)

  • it is a combining mark

  • it is a non-spacing mark

  • {@code isIdentifierIgnorable} returns {@code true} for the character

Upvotes: 0

Stephen C
Stephen C

Reputation: 718698

This looks like code that was "written" by a decompiler. Don't expect decompiled code to be particularly readable. For what it is worth, a (human) Java programmer would probably write the code in your question as:

public static double regress(double x, ArrayList<Double> terms) {
    double a = 0.0;
    int exp = 0;
    for (double term : terms()) {
        a += term * Math.pow(x, exp);
        ++exp;
    }
    return a;
}

The $ character is a legal character in a Java identifier, except that it is informally reserved for the use of compilers, source code generators and other tools. Use of $ in human written code is strongly discouraged. As JLS 3.8 says:

The $ sign should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

You can get you into trouble if you use $ injudiciously. The character is used in the synthetic names of inner classes, and for the synthetic variables used when an inner class refers to an "effectively final" local variable in an enclosing scope. If you declared your own $ variables, they could collide with the compiler's usage, and the result would be undefined.

Upvotes: 1

Andy Turner
Andy Turner

Reputation: 140299

A $ is a valid symbol to use in an identifier; it means nothing special.

However, refer to JLS Section 3.8, which suggests that $ should not typically be used in identifiers:

The $ sign should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

Upvotes: 0

dryairship
dryairship

Reputation: 6077

$ is just the part of the identifier, i.e, the name of your variable. You can use only a few special symbols as parts of Identifiers. $ is one of them. (In fact all the currency symbols are allowed).

It does not have any special meaning.

But one place where it is used by the compiler, is when you use inner classes. Then It is used to separate the name of the Inner class with the main class.
eg: MyClass$InnerClass1.class

Upvotes: 0

Lasse Meyer
Lasse Meyer

Reputation: 1479

The $ doesn't have any special meaning. In the line you mentioned an Iterator object for the list terms is created, which is then used to access all elements of it. i$ is just an odd choice for a variable name.

Upvotes: 0

Related Questions