• 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

$ in identifiers in mock exams

 
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question on whether $ is legal or recommended in Java identifiers has come up on these forums numerous times.

I know the exams love to be tricky, and the mock exams should be similarly tricky, ideally in the same ways, to provide maximal benefits to those paying for them and taking them.

If there are tricks on the exams themselves that people consider dirty, underhanded, unfair or downright mean, they should take it up with Oracle's exam-writing teams, not vent on those working hard to provide highly useful preparatory materials.

That said, I hate questions that seem to reward people who know a little and punish those who know more than a little.

If Oracle is going to do something like this on Real Cert Exams I think someone should speak to them arguing this is counter-productive, and if they aren't, well, the mock exams are tricky enough without this dog-poop-on-land-mine question.

Let me quote from the JLS section 3.8 on Valid Identifiers so I can feel like I am arguing a case before the Supreme Court (Hey, I bet you already realized I just got this wrong on a mock exam, didn't you?  Good catch!):

3.8 Identifiers
An identifier is an unlimited-length sequence of Java letters and Java digits, the
first of which must be a Java letter.
Identifier:
IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral
IdentifierChars:
JavaLetter {JavaLetterOrDigit}
JavaLetter:
any Unicode character that is a "Java letter"
JavaLetterOrDigit:
any Unicode character that is a "Java letter-or-digit"
A "Java letter" is a character for which the method
Character.isJavaIdentifierStart(int) returns true.
A "Java letter-or-digit" is a character for which the method
Character.isJavaIdentifierPart(int) returns true.
The "Java letters" include uppercase and lowercase ASCII Latin letters A-Z (\u0041-
\u005a), and a-z (\u0061-\u007a), and, for historical reasons, the ASCII dollar sign
($, or \u0024) and underscore (_, or \u005f). The dollar sign should be used only in
mechanically generated source code or, rarely, to access pre-existing names on legacy
systems
.
The underscore may be used in identifiers formed of two or more characters, but
it cannot be used as a one-character identifier due to being a keyword.
The "Java digits" include the ASCII digits 0-9 (\u0030-\u0039).



They don't go into details on why we should avoid '$' in identifiers, but Inner Class much?

Anyway, the question I got wrong was:
Which of the following would be a legal module name? (Choose all that apply.)
A. com.book
B. com-book
C. com.book$
D. com-book$
E. 4com.book
F. 4com-book



Just looking at it, beginning with a digit knocks E. and F. out on sight.  Then we see the minus sign that is uncool in identifiers in general, because it is a minus sign, and each component of a valid package names must be valid identifiers (wait, I think you can name a package com.atari.1040.ste so that isn't *quite* true)...but yeah, no - signs.  That leaves A. and C as the only choices.  Then I remembered how putting $ explicitly into an identifier is wrong in all sorts of ways and makes you a bad person, so I de-selected C and got the question wrong.

I often hear people complain that the Certification Exams reinforce bad behavior.
I don't often agree.
I sort of feel that pushing the legality of using $ in Java identifiers (it is true but doesn't mean you should do it) skirts that.

Lastly, the question asks what is legal, it doesn't request best practices suggestions, so I guess it is on me.

Real exams shouldn't use $ in non-auto-generated identifiers, and then mock exams should follow them.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Real and mock exams do all sorts of things that are bad practice. I agree that $ *shouldn't* be in an identifier name. But it is fair game for both mock and real exams.
 
Jesse Silverman
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Jeanne, I've been advised (both please don't do this in Real Life and fair game for the cert exams!)

I will note that if we had this in a jar name that was being processed as an "automatic module":

com.book$

The rules would first translate the $ to a .:
com.book.

and then remove the . because it was trailing, resulting in an automatic module name of:

com.book

So it might be worth remembering that not all legal module names might not ever be seen in a generated automatic module name, tho the best ones certainly would be.

Thanks again!
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh fun. <sarcasm>. Thanks for sharing!
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the things I hate about exams. They're packed with land mines and bad usages. Well and good, but to be professionally valid, they should be spending most of their time confirming that the applicant knows how to use things properly and secondarily to detect improper usages, especially common improper usages, not so much really obscure stuff or abominations. I really respect performance exams where you have to do something like actually take a cold-iron box and turn it into a fully-operational production server in 4 hours, but, alas, the software equivalent would probably require 4 weeks to prove.

There were 3 "magic" characters in the ancient IBM mainframe world and I think that they have informed a lot of technology since then. They were $, _ and I think maybe %, although I no longer remember for certain. These characters were more or less considered valid alphabetical characters and often used accordingly. C made a point of co-opting names with leading underscores (and having no use for the other 2 characters). Then we got into double underscores, but enough.

Java has 2 reasons to be wary of the $ in names. Aside from the stated cases and the internal use to replace the "." in inner class names. it's also the trigger character for certain types of EL expressions. So it could get awkward if you define a web page where something like 'title="${$page_name}"' popped up. Add jQuery to the mix and it gets even worse.

Of course on top of that, "$" is commonly simply "currency symbol" which may render as a completely different character depending on locale.
 
Jesse Silverman
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Of course on top of that, "$" is commonly simply "currency symbol" which may render as a completely different character depending on locale.


There are posts on these forums where they are discussing that actually "all Unicode currency symbols" are legal characters in identifiers and wondering if THAT will show up on exams.

I didn't see any evidence that it did.

Cheers
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:wondering if THAT will show up on exams.

I didn't see any evidence that it did.


No. The exams are written in a way that US centered people will be ok with. So using the euro symbol or yen symbol is as "exotic" as they are likely to get . And even they, putting it in a variable is pushing it. I'd expect "international" symbols to show up in an example on localization at most.
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic