Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By eRaaaa
  • 1 Post By pbrockway2

Thread: Why does casting 339 as char results in '' ?

  1. #1
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Why does casting 339 as char result in '' ?

    I asked this question back in this thread, but the lack of response seems to indicate that the topic warrants its own thread.

    The following code results in variable pound containing a reference to '', or the pound sign:

    Java Code:
    char pound = 339;
    Why does this happen? I expect that other integers can be casted into a special character, but I wasn't able to find any information or a code-table. I also tried several values surrounding 339, but they all produced '?'. Does anyone have an explanation or a table that maps out other integers that can be casted into special characters?
    Last edited by awinston; 08-05-2012 at 03:35 AM. Reason: Title Edit
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Why does casting 339 as char results in '' ?

    ?? can not agree with you.
    The pound sign is 163 -> System.out.println((int)''); (Pound sign - Wikipedia, the free encyclopedia -> "The pound sign has Unicode code point U+00A3" , A3 = 163 decimal)
    339 is the 'œ'
    awinston likes this.

  3. #3
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Why does casting 339 as char results in '' ?

    Perhaps I should have mentioned that I am running the program via the command prompt. This code results in the pound sign printing out:

    Java Code:
    System.out.println((char)339);
    As far as I know Unicode is not involved. That is part of what is puzzling me.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default Re: Why does casting 339 as char results in '' ?

    Did you look at the StackOverflow discussion DarrylBurke linked to?

  5. #5
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Why does casting 339 as char results in '' ?

    Quote Originally Posted by pbrockway2 View Post
    Did you look at the StackOverflow discussion DarrylBurke linked to?
    Yes, and from what I remember it said that the encoding of the Windows console is different from the encoding of Windows default system. Java assumes that the encoding of the default system is being used in the command prompt, but the console encoding is being used. The solution to this problem was to use System.console().writer().println() when printing to the command prompt in order to use the console encoding and print out the pound symbol.

    I don't understand how this connects to why a pound symbol can be printed via the standard output stream to the command prompt by casting 339 into a char.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default Re: Why does casting 339 as char results in '' ?

    Sorry, I hadn't realised you acknowledged db's post in that other thread. Also the original post in this thread confused me because you didn't speak of what was printed, but seemed to be saying that '' is the value 339. Of course it isn't that value. '' is the value 163.

    Java Code:
        /* Run in Eclipse because my command line simply won't print a ! */
    public class Pound {
        public static void main(String[] args) {
            System.out.println((int)'');
            System.out.println('' == 339);
            System.out.println('' == 163);
            System.out.println((char)339);
            System.out.println((char)163);
            //System.console().writer().println((char)339);
            //System.console().writer().println((char)163);
        }
    }
    (My compiler actually gives me a warning about ''==163 because I'm comparing "identical expressions")

    As I understand it System.out is printing bytes so if you see when you print (char)339 then your command line is interpreting those bytes as a pound sign.
    awinston likes this.

  7. #7
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Why does casting 339 as char results in '' ?

    Thank you for the responses. I think it would be better for me to gain a better understanding of input/output in Java before I tackle this issue so that no time is wasted.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

Similar Threads

  1. Replies: 2
    Last Post: 03-08-2011, 07:20 AM
  2. check a string char by char
    By Sotsiak in forum New To Java
    Replies: 2
    Last Post: 10-23-2010, 10:24 PM
  3. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 06:48 AM
  4. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 01:49 PM
  5. Casting an int value into a char
    By kurtulas in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 09:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •