• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Sybex CSG 11 - Pg 48 - Identifying Identifiers - The four rules do not include all exceptions

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On page 48 of the Java SE 11 Complete Study Guide, near the bottom of the page is a list of four rules to help remember how to legally name identifiers.  The first two rules states:
  • Identifiers must begin with a letter, a $ symbol, or a _symbol.
  • Identifiers can include numbers but not start with them


  • What's not part of the four rules is what character or symbol cannot be used in any part of the identifier.  On page 50, one of the identifiers that do not compile is "byte hollywood@vine;".  The reader might not have known that @ was illegal because there's no rule stating what is or isn't allowed in the identifier after the first character.

    Maybe the list could include a fifth rule, where it lists what can be included in any part of the identifier after the first character.  "Identifiers can only include a number, letter, a $ symbol, or a _symbol anywhere after the first character."  Or something similar.
     
    Marshal
    Posts: 73021
    330
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to the Ranch

    Actually, that isn't accurate. A legal identifier must be different from any reserved words or restricted identifiers. Identifiers may contain letters, numbers (except at their beginning), _s, and currency symbols. Any currency, including pound, rupee, and euro, is permissible, but their use is discouraged as a style matter. Those rules are correct; they give a complete list of all the characters permissible, once you factor in all currencies. It might make the rules clearer if you add the word “only”.
    Neither @ nor & is a letter; they are both abbreviations for whole words.
     
    Marshal
    Posts: 26626
    81
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It's Unicode which defines what characters are letters and what characters are currency symbols, not Java. Java just follows Unicode's lead there. Here's a random page from my web search with a list of currency symbols:

    Unicode Characters in the 'Symbol, Currency' Category

    which I'm sure you haven't even heard of many of them.
     
    Omar Betancourt
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Welcome to the Ranch

    Actually, that isn't accurate. A legal identifier must be different from any reserved words or restricted identifiers. Identifiers may contain letters, numbers (except at their beginning), _s, and currency symbols. Any currency, including pound, rupee, and euro, is permissible, but their use is discouraged as a style matter. Those rules are correct; they give a complete list of all the characters permissible, once you factor in all currencies. It might make the rules clearer if you add the word “only”.
    Neither @ nor & is a letter; they are both abbreviations for whole words.



    I can see where I was confused.  When I read the first of the four rules on Identifying Identifiers, I took it as a letter, a _s and literally a $ (dollar) symbol. There's no mention of 'currency' in the Identifying Identifiers section, so it didn't occur to me that the $ symbol was a placeholder for all currency symbols.    The wording in Oracle's API reference document is a bit more clear on what's legal as the start and part of an identifier.

    The Character.isJavaIdentifierPart​(int codePoint) method (https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Character.html#isJavaIdentifierPart(int)) lists the following conditions:

    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



    I prefer the rules outlined in the study guide over the API documentation.  However, when reading the API doc, it's clear that a currency symbol would mean all currency symbols, including the $ symbol.

    There's also the Character.isJavaIdentifierStart​(int codePoint) method (https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Character.html#isJavaIdentifierStart(int)):

    isLetter(codePoint) returns true
    getType(codePoint) returns LETTER_NUMBER
    the referenced character is a currency symbol (such as '$')
    the referenced character is a connecting punctuation character (such as '_')



    Again, I might have been the only reader who took it as $ symbol and not all currency.

    Thank you for your reply and clarifying my input.  Keep up the great work.
     
    Omar Betancourt
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Clapham wrote:It's Unicode which defines what characters are letters and what characters are currency symbols, not Java. Java just follows Unicode's lead there. Here's a random page from my web search with a list of currency symbols:

    Unicode Characters in the 'Symbol, Currency' Category

    which I'm sure you haven't even heard of many of them.



    I'd mistakenly read it as literally a $ symbol, not that the $ meant all currency.  Thank you for the clarification.  If the exam throws in a rupee as part of an identifier, I'll be sure to choose the correct answer.  :cool:
     
    I child proofed my house but they still get in. Distract them with this tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic