Omar Betancourt

Greenhorn
+ Follow
since May 30, 2021
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Omar Betancourt

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:

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.
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.