Results 1 to 8 of 8
  1. #1
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by kulangotski View Post
    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
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    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.

  4. #4
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default

    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?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by kulangotski View Post
    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?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default

    thanks jos for the quick answer :D

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by kulangotski View Post
    thanks jos for the quick answer :D
    You're welcome of course; personally I don't understand the last rule: one of the 'ignorable' characters is char 0x7f ('delete'); it can be part of a Java identifier. That would be a nice one for code obfuscation ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default

    yeah, I will hit up eclipse and try all those rules especially the last ones you mentioned. :D

    regards

    simon

Similar Threads

  1. Java voucher available in Hyderabad valid till 24th Jan
    By qcsurya in forum Java Certification
    Replies: 2
    Last Post: 09-25-2011, 11:54 PM
  2. Confirming a user is valid
    By Zack in forum New To Java
    Replies: 2
    Last Post: 08-03-2010, 01:07 AM
  3. Valid declarations
    By heat84 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 03:29 AM
  4. Help with valid declarations of a float
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 11:28 AM
  5. MySite/Valid.java:56: cannot resolve symbol
    By Albert in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 07-05-2007, 06:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •