Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By eRaaaa

Thread: (char)

  1. #1
    coderX is offline Member
    Join Date
    Feb 2012
    Location
    UK
    Posts
    16
    Rep Power
    0

    Default (char)

    Java Code:
      public class prg {
          public static void main(String[]args) {
             int i;
             i=10;
             char a = 'n';
             System.out.println((char)('n'+i));   //<---------------
          }
       }
    The output I get is x
    Similarly if it is (char)('n'+2),output is p

    I really don't get it.What is happening here? What does (char) actually do?

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: (char)

    char is an integer describing a characters code. So you can modify it by adding integers to get other characters.

    EDIT: (char) casts an integer to a character interpreting it as character code, google "java type casting" maybe to get more info about casts.
    Last edited by Sierra; 01-10-2013 at 07:18 PM.
    I like likes!

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

    Default Re: (char)

    Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
    "char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive)."
    Unicode Zeichentabelle
    n = 6e hex = 110 dec
    110 + 10 = 120 dec = 78 hex = x
    coderX likes this.

Similar Threads

  1. concatenate two char into one char
    By nadissen in forum Eclipse
    Replies: 3
    Last Post: 04-06-2011, 02:40 PM
  2. Replies: 2
    Last Post: 03-08-2011, 06:20 AM
  3. check a string char by char
    By Sotsiak in forum New To Java
    Replies: 2
    Last Post: 10-23-2010, 09:24 PM
  4. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 05:48 AM
  5. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 12:49 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
  •