·  JAVA – Syntax – Variables – float

 

- Info:

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

        

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

        //float.

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

        

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

        float   value  =  65;                         //Decimal.

                value  =  0101;                       //Octal.

                value  =  0x41;                       //Hexadecimal.

             

                value  = Float.parseFloat ("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 float fit into char.

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

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

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

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

        double  conv7  =         value;        //Implicit conversion since     all float fit into doubl

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

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

                        

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

        System.out.println(value);

        System.out.println(Float.toString   (value));

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

        

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

        //Float.

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

                

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

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

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

      

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

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

   

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

        float   primitive = object.intValue();

        String  string    = object.toString();

          

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

        System.out.println(object);

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

        

      }

 

    }