Search code examples
javastringcomparecompareto

java unicode value of char


When I do Collection.sort(List), it will sort based on String's compareTo() logic,where it compares both the strings char by char.

    List<String> file1 = new ArrayList<String>();
    file1.add("1,7,zz");
    file1.add("11,2,xx");
    file1.add("331,5,yy");
    Collections.sort(file1);

My understanding is char means it specifies the unicode value, I want to know the unicode values of char like ,(comma) etc. How can I do it? Any url contains the numeric value of these?


Solution

  • My understanding is char means it specifies the unicode value, I want to know the unicode values of char like ,(comma) etc

    Well there's an implicit conversion from char to int, which you can easily print out:

    int value = ',';
    System.out.println(value); // Prints 44
    

    This is the UTF-16 code unit for the char. (As fge notes, a char in Java is a UTF-16 code unit, not a Unicode character. There are Unicode code points greater than 65535, which are represented as two UTF-16 code units.)

    Any url contains the numeric value of these?

    Yes - for more information about Unicode, go to the Unicode web site.