

So if we try to run the above code snippet, the value printed will be Infinity . So what if we pass a string value which has a float equivalent that is higher than the maximum value as defined by the Float.MAX_VALUE? To have a birds eye view of this scenario lets take a look on below example: Float testValue = Float.parseFloat("3.4128235E38") Īs you would have noticed we have passed a method argument to parseint method that is greater than maximum value which certainly breached the threshold. Lets try to take a look on the parsefloat(String s) method which take an argument string and convert it into Float object. Secondly if we go through on the methods of the Float class we would find some parsing and conversion from another object type to Float type. So why do we care about these values? First reason is to know the limit on what value should we assign to our Float object. Maximum value of an Float object is 3.4028235E38 which can be found using (Float.MAX_VALUE) Īnd the minimum value of Float is 1.4E-45 which can be found also using one of the fields of Float (Float.MIN_VALUE) To find the maximum and minimum value of Float , we would be invoking two if it’s fields. There are three constructor available to Float class which we can use to instantiate a Float object: If you would like to go more into details, I am recommending for you to read the official java documentation regarding autoboxing and unboxing.

The 2nd method is using the autoboxing concept of java programming language which directly converts a primitive data type to its corresponding wrapper class. This is a straightforward method to instantiate a Float object type. A character f and F is still appended at the end of the numeric value to signify that this value is of primitive type float.
#Java string to float how to
Let’s discuss first how to instantiate a Float object Float value = new Float(1.32f) īasically the declaration of a Float object is almost the same as we are doing in assigning primitive data type float. Let’s tackle the basics of using the Float class. The Float class as part of the java.lang package is one of the classes of the java api that is being widely used.
