• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why $ only ???

 
vaibhav srivastava
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
in any book for java, for legal identifiers they say that the identifier should start with a currency character but on keyboard that applies only for '$'. can't they say directly that variable can start with '$' sign. why this kind of generalization ???
 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Murali Kakarla
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems to be a very good question.
Can some one explain this? Is it valid to have a java variable name starting with a 'pound' char/symbol? If so how to code it?

Murali...
SCJP5
 
vaibhav srivastava
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks murli,
this question is nagging me for a very long time.
the same thing( can I use a pound symbol) struck me also.
can't able to find one valid, good answer to it.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is probably one of those things that Java inherited from C and C++, which has the same rules with regard to variable names. One of the goals that James Gosling had in mind when he was inventing Java was that it should be familiar to C++ programmers.
 
vaibhav srivastava
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jesper,
thats all fine but the answer is still not there...
c and c++ have clearly defined things that they used. are you referring that the question dates back to c++ ???
 
Nikos Pougounias
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Various currency symbols may be used; the following are all valid statements.

 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... but you should never use this facility. Stick to letters, numbers and underscore, for Java variable names.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To restate Peter's statement, DON'T DO IT!

The $ (and others - but never you mind what they are) are intended for use by code generation programs so that no token names will clash with human-written code.

If YOU use the $variable, then you can clash with them.

(Plus it's ugly and looks like a scripting language with a weak parser that can't tell when something is supposed to be... what?... so it needs a special character to say "variable" - my own personal opinion.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic