• 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

Sybex CSG17 - Chapter 1 Page 35 - The € symbol

 
Greenhorn
Posts: 16
1
  • Number of slices to send:
    Optional 'thank-you' note:
After reading page 35 about the valid identifiers, I got to be curious about whether $, ¥, and where valid variable names on their own. I wrote a small java program:



To my surprise, when I tried to compile this from Git Bash via javac VariablePlay.java, I got the following exception:


I then commented out lines 5 and 6 and was able to run and get "foo" and "baz" to print out. What I discovered is that I believe my default encoding is causing the euro symbol to not compile. If I instead compile with javac -encoding UTF-8 VariablePlay.java, it successfully compiles and I get "foo", "bar", and "baz".

I did see in the Errata OCP 17 Developer Study Guide topic that there is a comment that the only currency symbol we should expect to see on the OCP 17 exam is the $. So, this is more a comment to the curious observer on this fact. Perhaps something to note in a future version.
 
Tim Mousaw
Greenhorn
Posts: 16
1
  • Number of slices to send:
    Optional 'thank-you' note:
I meant to mention I am running Windows 11 Home.
 
Saloon Keeper
Posts: 15619
366
  • Number of slices to send:
    Optional 'thank-you' note:
What tool did you use to create and save the source file?
 
Tim Mousaw
Greenhorn
Posts: 16
1
  • Number of slices to send:
    Optional 'thank-you' note:
I used Notepad++ and the file itself is UTF-8 encoded. At least, according to Notepad++ it is...
 
Stephan van Hulst
Saloon Keeper
Posts: 15619
366
  • Number of slices to send:
    Optional 'thank-you' note:
Java uses your system locale's default encoding as the default when you don't specify it on the command line.

This is appropriate if your source code files are also saved using the system locale's default encoding.

In reality, many different text editors use many different encoding settings. I think in Notepad++ you can configure the encoding to use for new files, so if you set it to the system locale's default encoding, you won't have to specify the -encoding option when calling javac.

But the easiest way to deal with all of this is to simply limit the characters you use in source files to those that can be encoded in 7-bit ASCII.
 
Tim Mousaw
Greenhorn
Posts: 16
1
  • Number of slices to send:
    Optional 'thank-you' note:

In reality, many different text editors use many different encoding settings. I think in Notepad++ you can configure the encoding to use for new files, so if you set it to the system locale's default encoding, you won't have to specify the -encoding option when calling javac.



I checked my preferences in Notepad++ using Settings -> Preferences -> New Document and the default is already set to UTF-8. So, I'm confused why this doesn't just work assuming javac pays attention to the encoding of my source file.

But the easiest way to deal with all of this is to simply limit the characters you use in source files to those that can be encoded in 7-bit ASCII.



Unless I'm mistaken, there's no way to render € with 7-bit ASCII. While I understand what you are saying, limiting myself to 7-bit ASCII seems to make the point my post impossible.
 
Stephan van Hulst
Saloon Keeper
Posts: 15619
366
  • Number of slices to send:
    Optional 'thank-you' note:

Tim Mousaw wrote:So, I'm confused why this doesn't just work assuming javac pays attention to the encoding of my source file.


Your mistake is in thinking that javac auto-detects the encoding of a source file. It doesn't, and for a good reason: you can't reliably detect the encoding of a source file.

Let's pretend I saved my source code file using an encoding that looks almost exactly like UTF-8, but the byte sequence that encodes '€' in this special encoding actually translates to '¿' in UTF-8.

If javac tries to auto-detect the encoding of my source code file, it might mistakenly detect UTF-8 and think my source code file contains '¿', when I really intended it to interpret the character as '€'.

Text files do not store any metadata about the encoding that was used to encode the text they contain, so there is no reliable way for applications to know how to read a text file. When you look at the encoding of a text file in the bottom right corner of Notepad++, that is not actually a property of the text file itself. It's just an educated guess that Notepad++ made.

So javac has two ways to deal with this:

  • Assume that the source file uses the same encoding as your system locale's default.
  • Have you tell it the encoding to use through the -encoding option.

  • On Windows, the system locale's default encoding is almost never UTF-8. Instead, it is probably some Microsoft variant of an ANSI codepage.

    So, you saved your source file as UTF-8, with the Euro sign encoded as [0xE2, 0x82, 0xAC], but without using the -encoding option, javac might interpret that byte sequence as "€" instead of "€", if your system default codepage is Windows-1252.

    Unless I'm mistaken, there's no way to render € with 7-bit ASCII. While I understand what you are saying, limiting myself to 7-bit ASCII seems to make the point my post impossible.


    Which means you are forced to supply the -encoding option explicitly. Build tools like Maven do this for you automatically.
     
    Marshal
    Posts: 79422
    377
    • Number of slices to send:
      Optional 'thank-you' note:
    What an interesting problem! I wish I had seen this thead earlier. Have a cow for bringing it up I have seen similar error messages when using a word processor and getting " changed toor.

    Tim Mousaw wrote:. . . whether $, ¥, and where valid variable names . . . .

    Let's look in the JLS (=Java® Language Specification. It refers to this method, which says it includes currency symbols, as you have already found out. I tried copying'n'pasting your class onto JShell, and it compiled first try.
    Agree with Stephan; it is an encoding problem. I can't reproduce it because I hardly ever use Windows®, and my computer usually defaults to UTF‑8. Try changing the encoding to platform default and to UTF‑16 and see what happens. You can encode neithernor ¥ in 7‑bit ASCII, and their encoding in UTF‑8 is different from in UTF‑16. Stephan has told you what the actual encoding is.
     
    Tim Mousaw
    Greenhorn
    Posts: 16
    1
    • Number of slices to send:
      Optional 'thank-you' note:

    Try changing the encoding to platform default and to UTF‑16 and see what happens.



    Notepad++ supports both UTF-16 big-endian and little-endian. I updated the default to these and compiled the same program and got many errors. I'd include the errors below, but it makes the post too long. If I compile either of these files with the -encoding UTF-16, they compile and run just fine in Git Bash.

    I also tried the same file in Ubuntu 22.04.3 LTS on WSL 2. Created it using the vi editor. It compiled and ran on my Linux installation no problem without specifying the encoding.
     
    Campbell Ritchie
    Marshal
    Posts: 79422
    377
    • Number of slices to send:
      Optional 'thank-you' note:

    Tim Mousaw wrote:. . . Created it using the vi editor. . . .

    Carfeul. You might start a vi versus emacs argument
    Obviously on a Linux box the default encoding is UTF‑8 and Java® assumed that when compiling your file.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic
    vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
    http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |