This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes why $ only ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why $ only ???" Watch "why $ only ???" New topic
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

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.
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);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why $ only ???