·  JAVA – Syntax – Variables – double

 

- Info:

  - This tutorial shows how to create double 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) { 

        

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

        //double.

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

        

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

        double  value  =  65;                           //Decimal.

                value  =  0101;                         //Octal.

                value  =  0x41;                         //Hexadecimal.

             

                value  = Double.parseDouble ("65.0" ); 

   

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

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

        char    conv2  = (char)  value;         //Explicit conversion since not all double fit into char

        byte    conv3  = (byte)  value;         //Implicit conversion since not all double fit into byte

        short   conv4  = (short) value;         //Implicit conversion since not all double fit into shor

        int     conv5  = (int)   value;         //Implicit conversion since not all double fit into int.

        long    conv6  = (long)  value;         //Implicit conversion since     all double fit into long

        float   conv7  = (float) value;         //Implicit conversion since     all double fit into floa

        String  conv8  = String.valueOf(value); //Explicit conversion to String object.

                conv8  =  ""  +  value;         //Implicit conversion while concatenating.

   

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

        System.out.println(value);

        System.out.println(Double.toHexString(value));

        System.out.println(Double.toHexString(value));

                

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

        //Double.

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

                    

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

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

                object = new Double        ("65.0" );   //Decimal.

      

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

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

 

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

        double  primitive= object.intValue();   

        String  string   = object.toString();    

          

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

        System.out.println(object);

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

        

      }

   

    }