·  JAVA – Syntax – Variables – char

 

- Info:

  - This tutorial shows how to create char and convert it to other Primitive Types or Strings.

 

- Procedure:

  - Create JAVA Console Application Test.java

 

    Test.java

    public class Test {

   

      public static void main(String[] args) {

        

        //==============================================================================================

        //char.

        //==============================================================================================

           

        //CREATE.---------------------------------------------------------------------------------------

        char      value    =  'A';                      

                  value    =  65;               //Decimal     ASCII value.

                  value    =  0101;             //Octal       ASCII value.

                  value    =  0x41;             //Hexadecimal ASCII value.

                                

        //CONVERT TO.-----------------------------------------------------------------------------------

        boolean   convert1 = (value == 1) ;     //True only if left byte argument is 1.  

        byte      convert2 = (byte)  value;     //Explicit conversion since not all char fit into byte.

        short     convert3 = (short) value;     //Explicit conversion since not all char fit into short.

        int       convert4 =         value;     //Implicit conversion since     all byte fit into int. 

        long      convert5 =         value;     //Implicit conversion since     all byte fit into long.

        float     convert6 =         value;     //Implicit conversion since     all byte fit into float.

        double    convert7 =         value;     //Implicit conversion since     all byte fit into double

        String    convert8 = String.valueOf(value);  //Explicit conversion to String object: "A"

                  convert8 =  ""  +  value;     //Implicit ocnversion while concatenating.

   

        //DISPLAY.--------------------------------------------------------------------------------------

        System.out.println(value);

                

        //==============================================================================================

        //Character.

        //==============================================================================================

                

        //CREATE.---------------------------------------------------------------------------------------

        Character object   = new Character( value    ); //Constructor accepting char argument.      

                  object   = new Character((char)65  ); //Decimal     ASCII value.

                  object   = new Character((char)0101); //Octal       ASCII value.

                  object   = new Character((char)0x41); //Hexadecimal ASCII value.

                  

                  object   = Character.valueOf(value);  //char argument.

      

        //CONVERT TO.-----------------------------------------------------------------------------------

        char      primitive= object.charValue();

       String    string   = object.toString();

                                              

        //DISPLAY.--------------------------------------------------------------------------------------

        System.out.println(object);

        System.out.println(object.toString());

        

      }

   

    }