One returns Double; the other, double.
The differences between primitive Java types and their wrapper counterparts are discussed, for example, here.
Answer from NPE on Stack OverflowDouble parameter can be null when double can't.
First off you need to understand the difference between the two types.
double is a primitive type whereas Double is an Object.
The code below shows an overloaded method, which I assume is similar to your lab code.
void doStuff(Double d){ System.out.println("Object call"); }
void doStuff(double d){ System.out.println("Primitive call"); }
There are several ways you can call these methods:
doStuff(100);
doStuff(200d);
doStuff(new Double(100));
These calls will result in:
"Primitive call"
"Primitive call"
"Object call"
Depends on the implementation. openJDK 6 b14 uses this implementation of Double(String s):
this(valueOf(s).doubleValue());
So it calls valueOf(String s) internally and must be less efficient compared to calling that method directly.
Your assumption is right. The second way of getting a Double out of String can be faster because the value may be returned from a cache.
Regarding the second question, you may create a helper null safe method which would return a null instead of throwing NullPointerException.
As in, the way Java works, why was it designed this way.. I understand double[] myList but why is it necessary to state double... same thing with like... Scanner input = new Scanner(System.in)
it just feels... redundant.. but im at such a low level, my curiosity got to me. whats the utility of this?
can you show me a short line of code that shows the deeper complexity of declaring a new instance.
id imagine its just a syntax thing built into java/previous languages.. but i guess from my experience with higher level languages seem to have really simplified the type of statement above
I am learning Java, and am confuse on when to choose double or float for my real numbers or int. It feels like, it doesn’t matter because from my limited experience (with Java) both of them deliver the same results, but I don’t want to go further down the learning curve with Java and have a bad habit of using either messing up my code, and not having a clue as to why. So, when should you use float and double?
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;
}
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)