·  JAVA – Syntax – Variables – int

 

- Info:

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

  - Ecplicit conversion is needed for converting int to primitive types with smaller containers.

 

- Procedure:

  - Create JAVA Console Application Test.java

 

    Test.java

    public class Test {

   

      public static void main(String[] args) { 

        

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

        //int.

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

        

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

        int     value    = 65;                                //Decimal.

                value    = 0101;                              //Octal.

                value    = 0x41;                              //Hexadecimal.

             

                value    = Integer.parseInt ("65"        );   //Decimal     since default base is 10.

                value    = Integer.parseInt ("1000001", 2);   //Binary      since base is set to   2.

                value    = Integer.parseInt ("101"    , 8);   //Octal       since base is set to   8.

                value    = Integer.parseInt ("41"     ,16);   //Hexadecimal since base is set to  16. 

   

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

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

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

        byte    convert3 = (byte)  value;       //Implicit conversion since all int fit into byte.   

        short   convert4 = (short) value;       //Implicit conversion since all int fit into short.   

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

        float   convert6 =         value;       //Implicit conversion since all int fit into long: 65.0

        double  convert7 =         value;       //Implicit conversion since all int fit into long: 65.0

        String  convert8 =  ""  +  value;                     //Decimal.

                convert8 = String .valueOf       (value);     //Decimal.      

                convert8 = Integer.toString      (value);     //Decimal.

                convert8 = Integer.toBinaryString(value);     //Binary.

                convert8 = Integer.toOctalString (value);     //Octal.

                convert8 = Integer.toHexString   (value);     //Hexadecimal.

                convert8 = Integer.toString      (value, 5);  //Base 5.

        

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

        System.out.println(value);                            //Decimal.

                

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

        //Integer.

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

                            

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

        Integer object   = new Integer      (value);          //int argument.

                object   = new Integer      ("65" );          //Decimal.

      

                object   = Integer.valueOf  (value       );   //int argument.        

                object   = Integer.valueOf  ("65"        );   //Decimal     since default base is 10.

                object   = Integer.valueOf  ("1000001", 2);   //Binary      since base is set to   2.

                object   = Integer.valueOf  ("101"    , 8);   //Octal       since base is set to   8.

                object   = Integer.valueOf  ("41"     ,16);   //Hexadecimal since base is set to  16.

      

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

        int     primitive= object.intValue();

        String  conv1    = object.toString();                 //Decimal.

          

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

        System.out.println(object);

        

      }

   

    }