File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why $ only ???" Watch "why $ only ???" New topic

why $ only ???

vaibhav srivastava

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?

vaibhav srivastava

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: 15092

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 8 API documentation
vaibhav srivastava

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 -
if (Posts < 30) JavaRanchFAQ);
I agree. Here's the link:
subject: why $ only ???
It's not a secret anymore!