0

I was wondering what is the reason behind this and why we cannot use any symbol than those two, I think I never use a variable with those symbols but it is worth to know the cause

jam
  • 489
  • 1
  • 4
  • 19
  • 2
    It's a legacy from C. – Andreas Jun 19 '20 at 17:27
  • 4
    [Why are special characters not allowed in variable names?](https://stackoverflow.com/questions/24250231/why-are-special-characters-not-allowed-in-variable-names) – Sercan Jun 19 '20 at 17:28
  • 1
    Because Sun Microsystems decided that it should be that way. As stated above, there were good reasons for doing it that way, but they could have done it differently and there are good reasons for that, too. –  Jun 19 '20 at 17:36
  • 2
    Underscore is legal name symbol in almost all languages, so there was no reason to break this rule in Java. Dollar is delimiter in inner class names like OuterClass$InnerClass, so it's allowed in identifiers too. Other symbols was missed in compiler developer's keyboard:) – Andrew Fomin Jun 19 '20 at 17:48

1 Answers1

2

The $ in variables is a historical convention that is commonly used in Linux systems (among other languages) to call variables. In a Linux system, if I set a variable, I call it with this key (i.e. x = 4, echo $x).

Underscores and Camel Case are 2 common ways to identify multi-word variables. If I want to declare the variable iLoveJava or I_Love_Java, both are easily readable.

Of course, all of this is a rationalization of stylistic choices made by Sun Microsystems when they made the language, but I think it's a fair interpretation ;). If you want more details on why the others aren't used in detail, the link posted by Sercan is excellent information as well.

Zach Rieck
  • 419
  • 1
  • 4
  • 23