• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why $ only ???

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ???
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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++ ???
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Various currency symbols may be used; the following are all valid statements.

 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... but you should never use this facility. Stick to letters, numbers and underscore, for Java variable names.
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Well THAT's new! Comfort me, reliable tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic