8

I'm following a book by Walter Savitch called Absolute Java. A sample program in it contains the following lines:

Double[] d = new Double[10];
for (int i = 0; i < d.length; i++)
    d[i] = new Double(d.length - i);

And I got the following warning message:

warning: [deprecation] Double(double) in Double has been deprecated

I believe that the warning message is telling me to replace the use of constructors since it is already deprecated, so what should I replace it with?

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Satellite Sage
  • 440
  • 1
  • 4
  • 13
  • 2
    Your question is answered in the Javadoc. – user207421 Feb 02 '20 at 09:02
  • The question is asked very well, but it is answered by simply looking up the Javadoc. This is something that is generally expected before posting at SO. So I have to give a downvote for lack of research. Please see [ask], thanks. – Zabuzard Feb 02 '20 at 09:06

2 Answers2

15

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;
}
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • 1
    Yes, there is a specific reason for using Double since the author was trying to demonstrate a program that will sort any kind of data types using the Comparable interface which is implemented by the class Double. – Satellite Sage Feb 02 '20 at 09:14
  • Fair enough, then stick to `Double[]` :) – Zabuzard Feb 02 '20 at 09:24
  • 2
    @Zabuza but you could use `d[i] = (double) (d.length - i);`, which implicitly uses `valueOf` via autoboxing. – Andy Turner Feb 02 '20 at 09:36
1

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)
Ori Marko
  • 56,308
  • 23
  • 131
  • 233