| Author |
why $ only ???
|
vaibhav srivastava
Greenhorn
Joined: Feb 24, 2008
Posts: 14
|
|
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 ???
|
A computer once beat me at chess, but it was no match for me at kick boxing.<br />
|
 |
Nadeem Khan
Ranch Hand
Joined: Nov 27, 2007
Posts: 108
|
|
|
<i>If there were no Exceptions (not Errors!), Life would have kinda sucked!!</i>
|
 |
Murali Kakarla
Ranch Hand
Joined: Jul 11, 2007
Posts: 80
|
|
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
Joined: Feb 24, 2008
Posts: 14
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
|
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.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
vaibhav srivastava
Greenhorn
Joined: Feb 24, 2008
Posts: 14
|
|
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
Joined: Jan 16, 2008
Posts: 110
|
|
Various currency symbols may be used; the following are all valid statements.
|
Nikos' Java blog
|
 |
Peter Chase
Ranch Hand
Joined: Oct 30, 2001
Posts: 1970
|
|
|
... but you should never use this facility. Stick to letters, numbers and underscore, for Java variable names.
|
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
|
 |
Bill Shirley
Ranch Hand
Joined: Nov 08, 2007
Posts: 457
|
|
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.)
|
Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
|
 |
 |
|
|
subject: why $ only ???
|
|
|