·  JAVA – Syntax – Variables – long

 

- Info:

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

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

 

- Procedure:

  - Create JAVA Console Application Test.java

 

    Test.java

    public class Test {

   

      public static void main(String[] args) {

   

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

        //long.

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

        

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

        long    value    = 65;                             //Decimal.

                value    = 0101;                           //Octal.

                value    = 0x41;                           //Hexadecimal.

             

                value    = Long.parseLong ("65"        );  //Decimal     since default base is 10.

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

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

                value    = Long.parseLong ("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 long fit into char.

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

        short   convert4 = (short) value;       //Implicit conversion since not all long fit into short

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

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

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

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

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

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

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

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

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

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

                        

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

        System.out.println(value);

        

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

        //Long.

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

                    

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

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

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

      

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

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

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

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

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

      

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

        long    primitive= object.intValue();              //Decimal.     
        String  string   = object.toString();              //Decimal. 

   

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

        System.out.println(object);

        

      }

   

    }