0

As we know java follows unicode system which supports all alphabets of many languages. I searched and found that Unicode value for अ is 2309 and value for आ is 2310 which are alphabets of DEVANAGRI language. The code i have written is something like this.

class Test
{
    public static void main(String args[])
    {
        char a=(char)2310;
        System.out.println(a);
    }
}

No matters I write 2309 or 2310 the output is '?' always. How this is happening?

  • try executing it in a for loop from 0, you will get all character values – sidgate Jan 25 '16 at 08:26
  • You may have to change your IDE settings, in Eclipse go to Window -> Preferences -> General -> Workspace : Text file encoding. If you're using e.g. gradle you'll have to change compiler encoding setting too. – DamianoPantani Jan 25 '16 at 08:26
  • 3
    @Damiano: Why would the *compiler* settings be relevant? The source code is entirely in ASCII. – Jon Skeet Jan 25 '16 at 08:26
  • 9
    Where are you trying to print the results? In an IDE, on a command line, something else? What operating system are you using? All of these are relevant to the problem. – Jon Skeet Jan 25 '16 at 08:27
  • Did you try preceding it with `\u`? – endowzoner Jan 25 '16 at 08:27
  • This is a configuration issue. On my local machine 2309 does cast to अ – CarbonAssassin Jan 25 '16 at 08:28
  • 5
    @FleshWound: That would be for a character literal. There are no character literals here. The code is fine. – Jon Skeet Jan 25 '16 at 08:28
  • i tried it with \u also still output is '?' and i am using window 7 jdk 1.8 and executing the program on command line – Manish Kumar Jan 25 '16 at 08:37
  • @JonSkeet `(char)2310` of course is also a character literal - these can be anything between `0` and `FFFF` - which is a subset of Unicode. What he was suggesting is simply a method to negate the simplest of mistakes, if it doesnt work with both representations the cause most likely is not the value itself – specializt Jan 25 '16 at 08:49
  • 12
    @specializt: No, it's not. 2310 is an integer literal and it's being cast to a `char`. It's a constant expression of type `char`, but it's not a character literal. See http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.4 for what character literals look like. And no, there is no compiler setting which would be relevant in this case. – Jon Skeet Jan 25 '16 at 09:30
  • @JonSkeet You probably should lookup your own terms in the JLS or oracle documentation ... this is getting quite shameful for you so ... i recommend stopping right about now. `(char)2310` will result in a character literal which is equivalent to its numeric codepoint - the fact that `char` cannot store supplementary characters isnt really important unless you need them – specializt Jan 25 '16 at 09:34
  • 17
    @specializt: I never mentioned supplementary characters. Nothing "results" in a literal - either an expression in source code is a character literal or it isn't. In this case, it isn't. It's an integer literal which is being cast to `char`. That's a constant `char` expression, with exactly the same value whether you actually use a character literal (`'\u0905'` or a character literal with the character itself in the right encoding for your compiler settings) or the integer literal cast to `char`. – Jon Skeet Jan 25 '16 at 09:43
  • You're contradicting yourself with your own words .... just saying. I think its best to stop now, your holy crusade wont change anything. – specializt Jan 25 '16 at 09:45
  • 12
    Nope, no contradiction here. Happy to discuss this in chat further if you want, after you read http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.4, http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1 and http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.28 – Jon Skeet Jan 25 '16 at 09:49
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101569/discussion-between-jon-skeet-and-specializt). – Jon Skeet Jan 25 '16 at 09:49
  • Just as a test, can you try: `new PrintStream(System.out, true, "UTF-8").println(a);` instead of `System.out.println(a);`? – Jcl Jan 25 '16 at 10:22
  • @specializt I implore you take some time out to read this - http://meta.stackexchange.com/questions/9134/jon-skeet-facts – Suhas Jan 25 '16 at 22:29
  • Possible duplicate of [What is character encoding and why should I bother with it](http://stackoverflow.com/questions/10611455/what-is-character-encoding-and-why-should-i-bother-with-it) – Raedwald Jan 27 '16 at 07:36

1 Answers1

13

you are trying to print characters that are not supported by character set of console. you can try changing console character set as mentioned here Can't print hindi characters

Community
  • 1
  • 1
Pranalee
  • 3,389
  • 3
  • 22
  • 36