The dollar symbol ($) is a valid character to name a variable, e.g. String superSecretFormula$;
, but when we're talking about naming conventions, when should I use this symbol?
Underscore for example is mostly used to separate words, as blank spaces can't be used.
Variable names can be up to 100 characters long, not including the dollar sign ($). Examples of variable names are $docname and $_currentline. Although a variable must start with a letter or underscore (_), the remainder of the name can consist of letters, digits, and underscores.
It stands for the initials of the United States'.
Rasmus Lerdorf, the father of the PHP language, explains the $ sign as an ability to insert variables inside literal string values (interpolation), so that the variables are distinguished from the rest of the string.
"Java does allow the dollar sign symbol $ to appear in an identifier, but these identifiers have a special meaning, so you should not use the $ symbol in your identifiers."
The dollar sign ($) and the underscore (_) are permitted anywhere in an identifier. The dollar sign is intended for use only in mechanically generated code. The dollar sign ($) and the underscore (_) are permitted anywhere in an IdentifierName. As such, the $ sign may now be used freely in variable names.
A dollar sign in php acts like the dollar sign in bash, it refers to a variable, in bash you can use the dollar sign with commands to get their output and assign them to a variable like this:
The leading $ symbol before a variable name is called a sigil. Its purpose is to make it clear that the name following the sigil is a variable and not something else, like a function name or a constant or a keyword. Sigils remove ambiguity to make the programming language interpreter's job easier.
Java allows the dollar sign to begin identifiers, but recommends that it only be used for mechanically generated code. (see here) It appears that in C++ identifiers may be able to use dollar signs as an extension, but it's not part of the standard.
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.
Your question is tagged Java, but it looks like you're asking about type characters, which are still supported in Visual Basic but not widely used these days (nor for a long time).
This is a bit subjective, but I think it's fair to say: never
One scenario where you might want to prefix a variable name with $
is when writing JavaScript code to distinguish jQuery objects.
Regarding starting a variable name with a $
, the Oracle Java tutorials tell us:
A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "
$
", or the underscore character "_
". The convention, however, is to always begin your variable names with a letter, not "$
" or "_
".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With