So, this is the given code.
public class Solution {
public static void main(String[] args) {
Integer[] array = new Integer[5];
fillArray(array, 3, 1, 3);
System.out.println(Arrays.toString(array));
}
public static void fillArray(Integer[] array, int value, int begin, int end)
I already solved this but, my question is about the parameters, and main method. So, the parameters of the method uses Integer instead of int, same as the main method. I tried to run this on my intellij and used int instead of Integer for the array's. I noticed that it gave me two different results. Using Integer it returns "null" however, when you use int it returns 0. Both are references to an object since its an array. But, why does one return null and the other return 0. Also, why was Integer used instead of int in this example. When would be a good practice to use Integer instead of int. Thanks for your help
Question for my understanding
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
5 January, 18:35useful
Are you asking why an int[] has its indices pre-filled with 0 and and Integer[] has its indices prefilled with 'null'? If this is what you are asking then remember that all primitives, such as an int, will have the default value of 0 while objects, such as Integer, will always have the default value of null. While an array is an object the values contained in its indices can be either primitives or objects. When you initialize an array without specifying what values to initialize them with then all the indices will start with default values.
As far as using primitives or their object counterparts: Primitives will always be faster, but objects are the preferred OOP way to program. Using collections actually force you out of using primitives (e.g. you can't have an ArrayList of primitives). The reason that primitives are faster is because objects have code associated with them behind the scenes (starting from that inherited from the Object class) that primitives do not. Because you can code your own functionality this provides an infinite amount of possible objects. Trying to create a library of an infinite amount of primitives would be impossible. It may not make much sense but I will use an example: an ArrayList can sort any type of object because it uses the equals() method as part of its algorithm. That method is part of the Object class and is guaranteed to be there with any object. Without this an ArrayList would have to write separate sorting code for each unique type it can hold. Because there are an infinite amount of types the size of the sorting code would have to be infinite..... or otherwise, more logically, left up to the programmer. This would mean you would have to write sorting code for every new application you created... or anytime new object types were added in the code (to maybe expand functionality). But it really just all depends on the application if using primitives is better than the same objects. That is the best answer I can give.
+2
Gummy C
10 January, 00:02
@Guadalupe ty for the lengthy explanation. I haven't touched on ArrayList yet, but I will google it and look into it.
0