Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Can someone explain to me the difference between Float and float in java? Manythanks.
Float is an object; float is a primitive. Same relationship as Integer and int, Double and double, Long and long.
float can be converted to Float by autoboxing, e.g.
float f=1.0f;
Float floatObject = f;
or explicitly
Float floatObject = new Float(f);
Initially primitives were retained alongside the object versions for speed. Autoboxing/unboxing was added with java 5 to facilitate conversion.
Float is a class which wraps the primitive float. In newer versions of Java, a feature called autoboxing makes it hard to tell that they are different but generally speaking, use float when you using the number to do calculations and Float when you need to store it in Object collections.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a Float variable which is initialised from a function returning the Float value, and there can be chance to return null in some cases. So I need to check the value in the variable is null or empty. If the value is empty I need to initialise variable wb1 with value 1. I am using Java Spring. This is the code currently I amusing,
Float wb1 = !StringUtils.isEmpty(getArea())?getArea() : 1.0f;
What is the correct way to check and initilize with value 1.0f
First: Float is an object and float is the primitive type.
null check is used for objects. For primitives you should use the default values check. For float the default value is 0.0f.
Assume you mean Float, the box of float, you can use
Optional.ofNullable(wbCalculation(area.getArea(), CitySizeByArea.SIZE1.value, area.getRate1(), task.getTaskId())).orElse(0f)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am trying to assert that my double is NaN.
Here is a code snippet:
private Double calculateIt(String input){...}
assertEquals(Double.NaN, calculateIt("input text"));
The code does not compile, Double.NaN is defined as primitive
public static final double NaN = 0.0d / 0.0;
To make the assertion work I wrap NaN with a Double object.
assertEquals(new Double(Double.NaN), calculateIt("input text"));
Is there a shorter way to do this?
You could use:
boolean isNan = Double.isNaN(calculateIt("input text"));
assertTrue(isNan);
Double.NaN values cannot be compared with == (Double.NaN == Double.NaN will return false), because NaN is considered as a special one.
More info:
IEEE floating point
You could try the following:
assertTrue(Double.compare(Double.NaN, calculateIt("input text")));
Hope this helped you.
assertEquals(Double.NaN, calculateIt(...), 0.0) with assertEquals(double, double, double)
or
assertThat(calculateIt(...), isNan()) with Hamcrest.
better way of doing this
What best shows the intent? Which can you skim read and easily see what the method is testing?
Shorter is Double.valueOf("NaN") or with a static import valueOf("NaN"), but it's basically the same as you already have.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
If we use float data type in Java we have to add f at the end of floating point literal as Java assumes it to be double data type and gives an error why not do the same for short and byte as both have lower ranges than int.
You can't have this thing with a byte for example because a byte can always be treated the same. It is allways a byte. But real numbers can only be represented as approximations. The difference between double and float is that double uses 64 bits and float 32. I.e. float is of less percision.
It is a similar thing to the int and long for integers. The default type for integers is int. Similarly the default type for real numbers is double.
Now if you want to use a float precision instead. You need to somehow let the compiler know.
That is precisely why the f is there. To tell the compiler how to treat the value.
Basically, when you do this float x = 0.1f you implicitly cast the 0.1 literal to a float.
This statement is equal to float x = (float) 0.1
Now lets try something:
float x = 0.1f;
double y = 0.1;
System.out.println(x == y) will give you false. Why? Lets see the first 20 digits after the decimal point:
0.1f --> 1000000014901161200
0.1 --> 1000000000000000055
I hope this helps.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Why do most programmers avoid using the float and long data types in their video tutorials?
Is it only to avoid the "0000f" and "0000L" notation?
I avoid float has it has poor precision. I would rather use double (or long with fixed precision or if I have to BigDecimal).
I suspect long is not often used as int is usually enough and many Java APIs only accept int values. e.g. array sizes and Collection/Map size() must be int.
And why should they use them? Perhaps the one reason is to avoid writing a letter indicating that the float/long type is used.
However if you don't need any special precision, why use double instead of float or long instead of int?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am in a computer science class (11th) for hw i have to do some code following a picture
--------------------Configuration: --------------------
C:\Users\I_SLAY_NOOBS\Desktop\Variables.java:26: error: possible loss of precision
float floatOne = 58.5678;
^
required: float
found: double
1 error
Process completed.
You assign a double which is more precise than a float to a variable declared as float. Float values end with an f in Java. Plain floating point numbers are automatically considered to be doubles...
Either do:
float f = 58.5678f;
or:
double d = 58.5678;
If you truly want a float (single precision IEEE754), you can use:
float floatOne = 58.5678f;
Me, I'd simply go with the double type everywhere since it provides more precision than float:
double doubleOne = 58.5678;
The default for floating point constants in Java (and C and C++, among others) is double precision and you're trying to shoehorn that into a single-precision variable, hence the message. Appending f to a floating point constant tells the compiler that you want it to be single precision.
Unless you have vast arrays of them (and limited space to store them), doubles are generally preferred.