Java – Japanese String in Java

cjkjavaunicode

I'm a newbie in Java so please bear with me if this is a very easy problem. I have a JUnit Test where I have a hardcoded Japanese word assigned directly to a string variable. Now right after that string is assigned, it turns to "??" meaning that the encoding is incorrect somewhere.

public class TestTest extends TestCase {
  public void testLocal(){
    Locale.setDefault(Locale.JAPAN);//same problem with or without this line
    String test = "会社";
    //after this line, by watching at the debugger, the variable "test" contains "??"
    assertEquals("会社", test);
  }
}

Because this is a testcase, I believe it completely isolates the problem from other UI environments. Please help me in this. Been 2 days with no solution. Thank you in advance.

Best Answer

If you've got the same exact string twice, it shouldn't really matter what encoding is being used... but I would suggest using the \uxxxx escape format to make it clear which Unicode characters are actually being used. That way it's basically encoding-independent.

If you really want to use string literals with Japanese in your code, check that all your build tools (etc) agree on the file encoding you're using. This will vary between IDE, Ant etc. (It's the -encoding flag for javac, for example.)