Explanation
You should replace it with:
d[i] = Double.valueOf(d.length - i);
From its Javadoc:
Deprecated.
It is rarely appropriate to use this constructor. The static factory
valueOf(double)is generally a better choice, as it is likely to yield significantly better space and time performance.
In general, valueOf is not forced to always return a new instance. It can utilize an internal cache and re-use values created before already, which makes it faster. For example if you create hundreds of 1.0.
Note
Is there a specific reason you are using a Double[] in the first place? If not, go for double[] instead. The primitives are much faster and have less memory overhead, compared to their object wrapper.
Then your code is just:
double[] d = new double[10];
for (int i = 0; i < d.length; i++)
d[i] = d.length - i;
By the way, you should prefer to never omitt the curly braces. Even if your loop is just one line. This is a very common source for bugs that are hard to find.
Also, your variable naming is not very good. What is d? Try to give it a name that reflects what it actually means. Like ages if it stores person ages, for example. If you do not have something specific, maybe use values. That is already better than just d. Especially since it is plural, so it is clear that it is an array of multiple values.
double[] values = new double[10];
for (int i = 0; i < values.length; i++) {
values[i] = values.length - i;
}
Answer from Zabuzard on Stack OverflowExplanation
You should replace it with:
d[i] = Double.valueOf(d.length - i);
From its Javadoc:
Deprecated.
It is rarely appropriate to use this constructor. The static factory
valueOf(double)is generally a better choice, as it is likely to yield significantly better space and time performance.
In general, valueOf is not forced to always return a new instance. It can utilize an internal cache and re-use values created before already, which makes it faster. For example if you create hundreds of 1.0.
Note
Is there a specific reason you are using a Double[] in the first place? If not, go for double[] instead. The primitives are much faster and have less memory overhead, compared to their object wrapper.
Then your code is just:
double[] d = new double[10];
for (int i = 0; i < d.length; i++)
d[i] = d.length - i;
By the way, you should prefer to never omitt the curly braces. Even if your loop is just one line. This is a very common source for bugs that are hard to find.
Also, your variable naming is not very good. What is d? Try to give it a name that reflects what it actually means. Like ages if it stores person ages, for example. If you do not have something specific, maybe use values. That is already better than just d. Especially since it is plural, so it is clear that it is an array of multiple values.
double[] values = new double[10];
for (int i = 0; i < values.length; i++) {
values[i] = values.length - i;
}
From Java 9 constructor(s) method(s) was Deprecated
Deprecated. It is rarely appropriate to use this constructor. The static factory
valueOf(double)is generally a better choice, as it is likely to yield significantly better space and time performance. Constructs a newly allocated Double object that represents the primitive double argument.
So replace with:
Double.valueOf(d.length - i)
It's entirely predictable. You get back exactly the value offered in the constructor argument. For the double literal 0.1 "the value that is being passed in to the constructor is not exactly equal to 0.1", as you cited. The constructor faithfully picks up the value that's passed in. Predictably.
This program illustrates a practical use of the new BigDecimal(double) constructor. The objective is to display the range of exact results that would round to a given double. It depends on being able to obtain a BigDecimal with the exact value of a double.
import java.math.BigDecimal;
public class Test {
public static void main(String[] args) {
System.out.println(range(1.0));
System.out.println(range(Math.nextUp(1.0)));
System.out.println(range(Math.PI));
}
private static String range(double d){
BigDecimal down = new BigDecimal(Math.nextDown(d));
BigDecimal up = new BigDecimal(Math.nextUp(d));
BigDecimal bd = new BigDecimal(d);
BigDecimal halfUp = midPoint(bd, up);
BigDecimal halfDown = midPoint(down, bd);
Boolean isEven = (Double.doubleToLongBits(d) & 1) == 0;
if(isEven){
return "[" + halfDown + "," + halfUp + "]";
} else {
return "(" + halfDown + "," + halfUp + ")";
}
}
private static BigDecimal midPoint(BigDecimal low, BigDecimal high){
return low.add(high).divide(BigDecimal.valueOf(2));
}
}