What Is the Meaning of $ in a Variable Name

What is the meaning of $ in a variable name?

$ is used internally by the compiler to decorate certain names. Wikipedia gives the following example:

public class foo {
class bar {
public int x;
}

public void zark () {
Object f = new Object () {
public String toString() {
return "hello";
}
};
}
}

Compiling this program will produce three .class files:

  • foo.class, containing the main (outer) class foo
  • foo$bar.class,
    containing the named inner class foo.bar
  • foo$1.class, containing the
    anonymous inner class (local to method foo.zark)

All of these class names are valid (as $ symbols are permitted in the JVM specification).

In a similar vein, javac uses $ in some automatically-generated variable names: for example, this$0 et al are used for the implicit this references from the inner classes to their outer classes.

Finally, the JLS recommends the following:

The $ character should be used only in mechanically generated source
code or, rarely, to access preexisting names on legacy systems.

What's the use/meaning of the @ character in variable names in C#?

Straight from the C# Language Specification, Identifiers (C#)
:

The prefix "@" enables the use of
keywords as identifiers, which is
useful when interfacing with other
programming languages. The character @
is not actually part of the
identifier, so the identifier might be
seen in other languages as a normal
identifier, without the prefix. An
identifier with an @ prefix is called
a verbatim identifier.

What do * and ** before a variable name mean in a function signature?

Inside a function header:

* collects all the positional arguments in a tuple.

** collects all the keyword arguments in a dictionary.

>>> def functionA(*a, **kw):
print(a)
print(kw)

>>> functionA(1, 2, 3, 4, 5, 6, a=2, b=3, c=5)
(1, 2, 3, 4, 5, 6)
{'a': 2, 'c': 5, 'b': 3}

In a function call:

* unpacks a list or tuple into position arguments.

** unpacks a dictionary into keyword arguments.

>>> lis=[1, 2, 3, 4]
>>> dic={'a': 10, 'b':20}
>>> functionA(*lis, **dic) #it is similar to functionA(1, 2, 3, 4, a=10, b=20)
(1, 2, 3, 4)
{'a': 10, 'b': 20}

What's the meaning of * and & when applied to variable names?

The unary prefix operator &, when applied to an object, yields the address of the object: &obj.

The type modifier &, when applied to a variable about to be declared, will modify the variable's type to be a reference type: int&.

The same applies to *: When applied as a unary prefix operator to a pointer, it will dereference the pointer, yielding the object referred to: *ptr.

When used as a type modifier to a variable about to be declared, * will modify the type to be a pointer: int*.

In a similar way, the type modifier [] applied to a variable that's being declared will modify the variable's type to an array, while the binary infix operator [] applied to an object of array type will access one of the array's sub-objects.


It's not helpful that type modifiers apply to the variable that is declared, not to the type they are declared with. For example, this

int *p, **pp, i, a[10], &r = i; 

defines an int pointer, a pointer to a pointer to an int, a vanilla int, an array of 10 int, and an int reference. (The latter is immediately initialized, because you cannot have an uninitialized reference.) Note that the type modifiers syntactically belong to the declared variable whose type they are modifying, not to the declared variable's type. Nevertheless, type modifiers (* and &) modify the type of the variable.

In the following case, however, with p, i, and a presumed to be variables that have already been declared

*pp = &i;
a[0] = i;

* and & are unary prefix operators dereferencing pp and yielding the address of i, while [] yields the first int object in the array a.

The fact that C and C++ don't care about the whitespaces around type modifiers and that this led to different camps when it comes to place them doesn't really make things easier.

Some people place the type modifiers close to the type. They argue that it modifies the type and so it should go there:

int* ptr;

The disadvantage is that this becomes confusing when declaring several objects. This

int* a, b;

defines a to be a pointer to int, but b to be an int. Which is why some people prefer to write

int *ptr;
int *a, *b;

I suggest to simply never declare multiple objects in the same statement. IMO that makes code easier to read. Also, it leaves you free to pick either convention.


To complicate things even further, besides the type modifiers and the unary prefix operators & and *, there are also the binary infix operators & and *, meaning "bitwise AND" and "multiplication". And to add insult to injury, in C++ you can overload both the unary prefix and the binary infix variants of these operators (and the binary infix []) for user-defined types and be completely free as to their semantics.

What's the meaning of $ in front of a variable

As everyone said, its just a convention.

I use the $ sign in front of a variable to identify that this variable holds an object.

var thisIsANumber = 1024; // No $.. Its a normal variable
var $divElement = $('div#idOfDiv'); // Yes! Its a jQuery Object
var $this = $(this); // Commonly used to reduce the work javascript has to do!

//Now I can use something like this.. (Notice how easy it is to read!)
$divElement.slideUp();

// Or a more `real world` example!
$('#element').click(function(){
// Hold $(this) inside a variable
// So we don't have to traverse the dom unnecessarily
var $this = $(this); // Save it (its a object.. so prepend a `$` )
$this.hide(); // Use it again
$this.fadeIn(); // and again
// ^ Has a dollar sign, because it is a jQuery Object.
});

You will see loads of plugins use this convention (well.. at least the well written ones).

By storing the object inside a variable, Javascript doesn't have to crawl through your code each time to get the element. Instead, we already have the element (inside the variable), so we use that to reference it.

If you use $(this) more then once inside the same callback function, you should store it inside a variable.. (var $this = $(this);). Otherwise, every time you use it, javascript will have to get the element from your source code each time (which can massively decrease performance! (especially for people browsing on a slow/old computer!).

Why is the variable name an expression?

Because the language designers have decided as such when creating the grammar.


As a more practical reason, grammars are often defined recursively. You can disallow a naked variable to be not allowed as an expression, but it's much easier to define that a variable is an expression as well. Because then you can define addition as simply <expr> + <expr>, as an example.

When should I use the dollar symbol ($) in a variable name?

From the Java Language Specification on identifiers:

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

What is the meaning of simply __ double underscore as a variable name? Just __ not follow another chars

According to the IPython docs, the _* values cache the values of recent outputs:

The following variables always exist:

  • _ (a single underscore): stores previous output, like Python’s default interpreter.
  • __ (two underscores): next previous.
  • ___ (three underscores): next-next previous.

Conversely, the _i* variable store recent inputs:

_i, _ii, _iii: store previous, next previous and next-next previous inputs.

Why do we use _ in variable names?

It doesn't mean anything. It is rather a common naming convention for private member variables to keep them separated from methods and public properties. For example:

class Foo
{
private int _counter;

public int GetCounter()
{
return _counter;
}

public int SetCounter(int counter)
{
_counter = counter;
}
}


Related Topics



Leave a reply



Submit