Java - Short floatValue() Method



The Java Short floatValue() method retrieves the equivalent float value of the given short value after a widening primitive conversion (converting smaller data type to a bigger data type). The data type float can store fractional numbers in the range of 3.4e-038 to 3.4e+038.

This may involve rounding or truncation. Truncation in Java programming refers to removing certain float or double-type digits from the right of a string. Rounding refers to round off the given value to its closest float value.

Syntax

Following is the syntax for Java Short floatValue() method −

public float floatValue()

Parameters

This method does not accept any parameter.

Return Value

This method returns the numeric value represented by this object after conversion to type float.

Example

The following example shows the usage of Java Short floatValue() method by creating a Short object 'obj' and assigning a value to it. Thereafter, returning the equivalent float value of short by invoking the method on the Short object −

import java.lang.*;
public class ShortDemo {
   public static void main(String[] args) {
      
      // returns the float value of this Short object
      Short obj = new Short("5");
      float f = obj.floatValue();
      System.out.println("Value = " + f);
      obj = new Short("32");
      f = obj.floatValue();
      System.out.println("Value = " + f);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Value = 5.0
Value = 32.0

Example

Following is an example of Java Short floatValue() method. Here, we are assigning negative short values to the created Short object ‘obj’ and printing the equivalent float value of it −

import java.lang.*;
public class ShortDemo {
   public static void main(String[] args) {
      Short obj = new Short("-65");
      float f = obj.floatValue();
      System.out.println("Value = " + f);
   }
} 

Output

Following is an output of the above code −

Value = -65.0

Example

In the following example, a Short object with value '8986' is created and it is converted into float using Java Short floatValue() method −

import java.lang.*;
public class ShortDemo {
   public static void main(String[] args) {
      short obj = 8986;
      Short s = new Short(obj);
      System.out.println("The Short value is = " + s);
      
      // Short into float conversion
      float f = s.floatValue();
      System.out.println("The converted Float value is = " + f);
   }
}

Output

On executing the program above, the output is obtained as follows −

The Short value is = 8986
The converted Float value is = 8986.0

Example

Following is another example of the floatValue() method −

import java.lang.*;
public class ShortDemo {
   public static void main(String[] args) {
      
      // initializing the variables
      short value1 = 76;
      short value2 = 87;
      
      // Returning the short value represented by this Short object 'res1'
      // and converting it to a float by calling res1.floatValue()
      Short res1 = new Short(value1);
      
      // Printing the res1 result
      System.out.println("The float value of res1 is: " + res1.floatValue());
      
      // Returning the short value represented by this Short object 'res1'
      // and converting it to a float by calling res2.floatValue()
      Short res2 = new Short(value2);
      
      // Printing the res2 result
      System.out.println("The float value of res2 is: " + res2.floatValue());
   }
}

Output

While executing the above code, we get the following output −

The float value of res1 is: 76.0
The float value of res2 is: 87.0
java_lang_short.htm
Advertisements