Friday, 18 April 2008

Euro and Pound signs in Java

Never use € (euro) and £ (pound) symbols in a Java src file. If you do you're asking for trouble. Rather use their unicode prepresentation (and possibly a comment telling readers what that unicode represents), that is:

public static final char EURO = '\u20AC';
public static final char POUND = '\u00A3';

The main problem occurs when your src is meant to be managed in a windows platform (where encoding is by default Cp1252) and in a Linux platform (where encoding is by default UTF-8).
Unless you have to, then, don't use them. If you really have to, the option is to share between the platforms the same encoding.

You may need to consider

  1. -Dfile.encoding
  2. Use the same encoding in both platforms by passing to the ant the encoding attribute
  3. Change the default workspace encoding in eclipse (windows/general/workspace) and set it to UTF-8 (bear in mind that then your € and £ wont be visible anyway)
Obviously this happens for any other outside US-ASCII char that is not represented in UTF-8

No comments: