Java.lang - SPLessons

Java.lang Float and Double

Home > Lesson > Chapter 7
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.lang Float and Double

Java.lang Float - Double

shape Description

The functionality of java.lang.Float class is to wrap float(primitive type) value in an object, as every one know that in Java floats are not objects, they supposed to be wrapped. This class hire the methods from java.lang.Object. Following is the class declaration of java.lang.Float class. [java] public final class Float extends Number implements Comparable<Float> [/java] The functionality of java.lang.Double class is to wrap the double(primitive type) value in an object, in Java doubles are also not objects so they supposed to be wrapped. This class hires the methods from java.lang.Object. Following is the class declaration of java.lang.Double class. [java] public final class Double extends Number implements Comparable<Double>[/java]

java.lang Float Class

shape Methods

The following are the regularly using methods of java.lang.Float class.
Methods Description
doubleValue() To return the double value of the float.
floatValue() float value of the Float object will be returned.
isNaN() If float value is Nan(Not-a-Number) then it returns true.
toString() String indication of the float object will be returned.
longValue() To return long value of float.

shape Fields

Fields Description
MAX_VALUE Maximum value a float can have.
MIN_VALUE Minimum value a float can have.
NEGATIVE_INFINITY To hold the negative infinity of float type.
POSITIVE_INFINITY To hold the positive infinity of float type.

shape Example

Following is an example for the java.lang.Float.doubleValue() method. DemoFloat.java [java] package com.SPlessons; import java.lang.*; public class DemoFloat { public static void main(String[] args) { /* returns the float value represented by this object converted to type double */ Float obj = new Float("42.10f"); double d = obj.doubleValue(); System.out.println("Value = " + d); obj = new Float("8.0"); d = obj.doubleValue(); System.out.println("Value = " + d); } } [/java] The functionality of the doubleValue() method is that to return the double of the Float object. Output: Now compile the code result will be as follows. [java] Value = 42.099998474121094 Value = 8.0[/java]

By using java.lang.Float.floatValue() method

[java] package com.SPlessons; import java.lang.*; public class DemoFloat { public static void main(String[] args) { /* returns the float value represented by this object converted to type double */ Float obj = new Float("42.10f"); float d = obj.floatValue(); System.out.println("Value = " + d); obj = new Float("8.0"); d = obj.floatValue(); System.out.println("Value = " + d); } } [/java] Here the developer has taken the same example by changing the float instead of double with the same values. Output: Now compile the code result will be as follows. [java] Value = 42.1 Value = 8.0[/java]

By using java.lang.Float.isNaN() method

[java]package com.SPlessons; import java.lang.*; public class DemoFloat { public static void main(String[] args) { Float f1 = new Float(-3.0/0.0); Float f2 = new Float(0.0/0.0); // returns true if this Float value is a Not-a-Number(NaN) //else false System.out.println(f1 + " = " + f1.isNaN()); System.out.println(f2 + " = " + f2.isNaN()); } } [/java] In the above code given the float value is negative and if the float value is NAN then it will give the result as false otherwise, it gives true. Output: Now compile the code result will be as follows. [java] -Infinity = false NaN = true[/java]

java.lang Double Class

shape Methods

The following are the regularly using methods of java.lang.Double class.
Methods Description
isInfinite() If the given number is huge in magnitude then it returns true.
longValue() Long value of the Double will be returned.
hashCode() hashcode for the double will be returned.
floatValue() Float value of the double will be returned.

shape Fields

Fields Description
MAX_VALUE Maximum value a float can have.
MIN_VALUE Minimum value a float can have.
NEGATIVE_INFINITY To hold the negative infinity of float type.
POSITIVE_INFINITY To hold the positive infinity of float type.

shape Example

Following is an example for java.lang.Double.isInfinite() method. DemoDouble.java [java] package com.SPlessons; public class DemoDouble { public static void main(String[] args) { Double d1 = new Double(3.0/0.0); Double d2 = new Double(0.0/0.0); // returns true if positive or negative infinity System.out.println(d1 + " = " + d1.isInfinite()); // returns false for other cases System.out.println(d2 + " = " + d2.isInfinite()); } } [/java] If the given number is huge in magnitude then infinite() returns true. Now see the result. Output: Now compile the code result will be as follows. In the output NAN is Not-a-Number. [java] Infinity = true NaN = false[/java]

By using java.lang.Double.longValue() method

[java] package com.SPlessons; public class DemoDouble { public static void main(String[] args) { /* returns the double value represented by this object converted to type long */ Double obj = new Double("443287.40"); long l = obj.longValue(); System.out.println("Value = " + l); obj = new Double("80.0"); l = obj.longValue(); System.out.println("Value = " + l); } } [/java] This method is used to return the double value as long value, where given the value is 4432287.40, to change the value as 4432287 the developer used longValue() method. Output: Now check the result that will be as follows. [java] Value = 443287 Value = 80 [/java]

Summary

shape Key Points

  • The toString(double) method is used to return a string for the double value.
  • The NaN stands for Not A Number.
  • The java.lang.Object class is the root class for all the Java classes.