Double and Float

  • These are the wrappers for floating point values double and float.
  • The constructors for Float are

Float(double num) //constructs Float object for type double

Float(float num) //constructs Float object for type float

Float(String str) throws NumberFormatException //constructs Float object for type String contains floating point number.

  • The constructors for Double are

    Double(double num) //constructs Double object from double type value

    Double(String str) throws NumberFormatException//constructs Double object from String contains floating point number.

//example for creating Double
public class DoubleDemo 
{
 public static void main(String args[])
  {
 Double d1 = new Double(3.14159);
 Double d2 = new Double("314159E-5");
 System.out.println(d1 + " = " + d2 + " -> " + d1.equals(d2));
 }
}

Output:

3.14159 = 3.14159 –> true

  • Both Float and Double define the following constants:

  • Methods defined by Float are :

// Demonstrate isInfinite() and isNaN()
public class FloatDemo
{
  public static void main(String args[])
  {

    Float f1=new Float(1.23);
      Float f2=new Float(1.2);

    System.out.println("byte byteValue( ) "+f1.byteValue());  //returns byte value for a Float object

    System.out.println("static int compare(float num1, float num2) "+Float.compare(f2,f1));  //compares two float values

    System.out.println("int compareTo(Float f ) "+f1.compareTo(f2));   //compares the invoking float object to another object

    System.out.println("double doubleValue( ) "+f1.doubleValue());     //returns the double value of the object

   System.out.println("boolean equals(Object FloatObj) "+f1.equals(f2));  //checks for equality of one object to another


     float f11=f1.floatValue();
    System.out.println("float floatValue( )"+f11);      //returns the float value of the object


    System.out.println("int hashCode( ) "+f1.hashCode());  //returns the hashcode for the invoking object


     System.out.println("static int hashCode(float num) "+ Float.hashCode(f11)); //returns the hashcode for the float number


    System.out.println("static float intBitsToFloat(int num) "+Float.intBitsToFloat(7));//converts the integer value to the float object value


    System.out.println("int intValue( ) "+f1.intValue( )); //returns the integer value for the object


    System.out.println("long longValue( ) "+f1.longValue()); //returns the long value for the object


    System.out.println("static float max(float val, float val2) "+Float.max(f1,f2)); //returns the value of the float variable having maximum value


    System.out.println("static float min(float val, float val2) "+Float.min(f1,f2));  //returns the minimum value among the two float values


    System.out.println("short shortValue( ) "+f1.shortValue()); //returns the shortValue for a Float object


    System.out.println("static float sum(float val, float val2) "+Float.sum(f1,f2)); //returns the sum of the two float objects


    System.out.println("String toString( ) "+f1.toString());  //returns the corresponding string object to the Float object


    System.out.println("static String toString(float num) "+Float.toString(f1));//returns the string format for the corresponding object


    System.out.println("static Float valueOf(float num) "+Float.valueOf(f11));//returns the float object containg the value of num


 }
}

Output:

byte byteValue( ) 1

static int compare(float num1, float num2) -1

int compareTo(Float f ) 1

double doubleValue( ) 1.2300000190734863

boolean equals(Object FloatObj) false

float floatValue( )1.23

int hashCode( ) 1067282596

static int hashCode(float num) 1067282596

static float intBitsToFloat(int num) 9.8E-45

int intValue( ) 1

long longValue( ) 1

static float max(float val, float val2) 1.23

static float min(float val, float val2) 1.2

short shortValue( ) 1

static float sum(float val, float val2) 2.43

String toString( ) 1.23

static String toString(float num) 1.23

static Float valueOf(float num) 1.23

  • Methods defined by Double are:

// Demonstrate isInfinite() and isNaN()
public class FloatDemo
{
public static void main(String args[])
{

Double d1=new Double(1.23433);
Double d2=new Double(1.2045);

System.out.println("byte byteValue( ) "+d1.byteValue()); //returns byte value for a Double object

System.out.println("static int compare(double num1, double num2) "+Double.compare(d2,d1)); //compares two double values

System.out.println("int compareTo(Dopuble d) "+d1.compareTo(d2)); //compares the invoking Double object to another object

System.out.println("double doubleValue( ) "+d1.doubleValue()); //returns the double value of the object

System.out.println("boolean equals(DoubleObj)) "+d1.equals(d2)); //checks for equality of one object to another


float f11=d1.floatValue();
System.out.println("float floatValue( )"+f11); //returns the float value of the object


System.out.println("int hashCode( ) "+d1.hashCode()); //returns the hashcode for the invoking object


System.out.println("static int hashCode(double num) "+ Double.hashCode(d1)); //returns the hashcode for the float number


System.out.println("static float intBitsToFloat(int num) "+Float.intBitsToFloat(7));//converts the integer value to the float object value


System.out.println("int intValue( ) "+d1.intValue( )); //returns the integer value for the object


System.out.println("long longValue( ) "+d1.longValue()); //returns the long value for the object


System.out.println("static double max(double val, double val2) "+Double.max(d1,d2)); //returns the value of the double variable having maximum value


System.out.println("static double min(double val, double val2) "+Double.min(d1,d2)); //returns the minimum value among the two double values


System.out.println("short shortValue( ) "+d1.shortValue()); //returns the shortValue for a Double object


System.out.println("static  double sum(double val, double val2) "+Double.sum(d1,d2)); //returns the sum of the two double objects


System.out.println("String toString( ) "+d1.toString()); //returns the corresponding string object to the Double object


System.out.println("static String toString(double num) "+Double.toString(d1));//returns the string format for the corresponding object


System.out.println("static Float valueOf(double num) "+Double.valueOf(d1));//returns the double object containg the value of num


}
}

Output:

byte byteValue( ) 1

static int compare(double num1, double num2) -1

int compareTo(Dopuble d) 1

double doubleValue( ) 1.23433

boolean equals(DoubleObj)) false

float floatValue( )1.23433

int hashCode( ) -275500080

static int hashCode(double num) -275500080

static float intBitsToFloat(int num) 9.8E-45

int intValue( ) 1

long longValue( ) 1

static double max(double val, double val2) 1.23433

static double min(double val, double val2) 1.2045

short shortValue( ) 1

static double sum(double val, double val2) 2.43883

String toString( ) 1.23433

static String toString(double num) 1.23433

static Float valueOf(double num) 1.23433

isInfinite( ) and isNaN( )

  • These are the methods provided by the both Float and Double.
  • These methods are used to test for two unique values.
  • isInfinite( ) returns trueif the value being tested is infinitely large or small in magnitude.

  • isNaN( )returns true if the value being tested is not a number

// Demonstrate isInfinite() and isNaN()
public class InfNaN 
{
  public static void main(String args[])
  {
 Double d1 = new Double(1/0.);
 Double d2 = new Double(0/0.);
 System.out.println(d1 + ": " + d1.isInfinite() + ", " + d1.isNaN());
 System.out.println(d2 + ": " + d2.isInfinite() + ", " + d2.isNaN());
 }
}

Output:

Infinity: true, false
NaN: false, true

results matching ""

    No results matching ""