Results 1 to 8 of 8
Thread: valid java identifiers
- 01-09-2011, 02:36 PM #1
Senior Member
- Join Date
- Feb 2009
- Posts
- 117
- Rep Power
- 0
valid java identifiers
hi just want to ask some questions about naming variables in java, we were given some questions that are for me are a bit tricky and I don't seem to understands why these are illegal identifiers
int :b; // totally clueless with this one
int -d; // maybe because it start with an operator?
int e#; // what is the symbol used for anyway?
int .f; // why is this illegal?
int 7g; // this is obvious
please do give me some info on these things.. thanks! :D
- 01-09-2011, 02:42 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,425
- Blog Entries
- 7
- Rep Power
- 17
None of the above are valid identifier names:
1) starts with a colon
2) starts with a minus sign
3) ends with a cross hatch
4) starts with a dot
5) starts with a digit.
Also have a look at the Character class; it has static methods for the recognition of characters that can be part of an identier name.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-09-2011, 02:47 PM #3
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)
The first character of an identifier needs to be an letter or underscore. Subsequent characters can be letters, numbers, underscores or dollar signs, although by convention dollar signs are not used and underscores are only used in constants.
- 01-09-2011, 02:56 PM #4
Senior Member
- Join Date
- Feb 2009
- Posts
- 117
- Rep Power
- 0
thanks jos for the tip, i will definitely look at the character class and iron thanks for the link, and just to make sure the general rule would be
a variable name can start with a dollar sign, an underscore and a letter. anything else would be illegal or are there any exceptions?
- 01-09-2011, 03:12 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,425
- Blog Entries
- 7
- Rep Power
- 17
Straight from the Character class API documentation:
A first character can be any of:
•isLetter(ch) returns true
•getType(ch) returns LETTER_NUMBER
•ch is a currency symbol (such as "$")
•ch is a connecting punctuation character (such as "_").
(for completeness) an (optional) following character can be any of:
•it is a letter
•it is a currency symbol (such as '$')
•it is a connecting punctuation character (such as '_')
•it is a digit
•it is a numeric letter (such as a Roman numeral character)
•it is a combining mark
•it is a non-spacing mark
•isIdentifierIgnorable returns true for the character
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-09-2011, 03:16 PM #6
Senior Member
- Join Date
- Feb 2009
- Posts
- 117
- Rep Power
- 0
thanks jos for the quick answer :D
- 01-09-2011, 03:55 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,425
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-09-2011, 09:47 PM #8
Senior Member
- Join Date
- Feb 2009
- Posts
- 117
- Rep Power
- 0
Similar Threads
-
Java voucher available in Hyderabad valid till 24th Jan
By qcsurya in forum Java CertificationReplies: 2Last Post: 09-25-2011, 10:54 PM -
Confirming a user is valid
By Zack in forum New To JavaReplies: 2Last Post: 08-03-2010, 12:07 AM -
Valid declarations
By heat84 in forum New To JavaReplies: 1Last Post: 12-22-2007, 02:29 AM -
Help with valid declarations of a float
By baltimore in forum New To JavaReplies: 1Last Post: 07-31-2007, 10:28 AM -
MySite/Valid.java:56: cannot resolve symbol
By Albert in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 07-05-2007, 05:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks