A small snippet to show the difference:
// declare a array variable that can hold a reference.
String [] array;
// make it null, to indicate it does not refer anything.
array = null;
// at this point there is just a array var initialized to null but no actual array.
// now allocate an array.
array = new String[3];
// now make the individual array elements null..although they already are null.
for(int i=0;i<array.length;i++)
{
array[i] = null;
}
// at this point we have a array variable, holding reference to an array,
// whose individual elements are null.
Answer from codaddict on Stack OverflowA small snippet to show the difference:
// declare a array variable that can hold a reference.
String [] array;
// make it null, to indicate it does not refer anything.
array = null;
// at this point there is just a array var initialized to null but no actual array.
// now allocate an array.
array = new String[3];
// now make the individual array elements null..although they already are null.
for(int i=0;i<array.length;i++)
{
array[i] = null;
}
// at this point we have a array variable, holding reference to an array,
// whose individual elements are null.
No, the first one makes each element of the array null, the length of the array will still be array.length, the second will set the array variable to null.
No you can't remove an element from an array, as in making it shorter. Java arrays are fixed-size. You need to use an ArrayList for that.
If you set an element to null, the array will still have the same size, but with a null reference at that point.
// Let's say a = [0,1,2,3,4] (Integer[])
a[2] = null;
// Now a = [0,1,null,3,4]
Yes, you can set elements in an array to null, but code like a[i].equals(a[i+1]) will fail with a NullPointerException if the array contains nulls, so you just have to be more careful if you know that your array may contain nulls. It also doesn't change the size of the array so you will be wasting memory if you remove large numbers of elements. Fixed size arrays are generally not a good way to store data if you are often adding and removing elements - as you can guess from their name.
java - Explicitly setting NULL value to an array element - Stack Overflow
Set an array element to Null - Java - Stack Overflow
Setting an array element to a null value
How do i work will null arrays?
Videos
Correct. int is a primitive type, which means that it contains an explicit value (a number from -2^31 to 2^31-1), and not a reference, so it can't be null. If you really need null values, use Integer.
Your array
int[] a
is of primitive type int. Primitives cannot have a null value but instead have a default value of 0. Only objects in java can have null as a value. Primitives include: byte,short,char,int,long,float,double.
Primitive Java integers cannot be null, but the Integer class, which wraps a primitive int can be null. Here is one option to consider:
Integer[] num = new Integer[3];
num[0] = 23;
num[1] = null;
num[2] = 12;
The boxing rules in Java make it fairly easy to use int and Integer interchangeably, most of the time.
In int array all the elements enitialize to 0 whether you have assigned a value or not. its because array is primitive type.
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World");
int [] a = new int[10];
for(int i=0; i<10; i++)
System.out.println(a[i]+"");
}
}
when you are initialize a Person object, Person is not a primitive type. so person can be null but age cannot be null since age is primitive type
So I have to use a count variable to store the amount of values that are in an array and then using a capacity to store how many slots make up the array. And finally the code is suppose to detect when the array is more than 80% full and re-size it so that it will then only be 50% full.
The only problem I'm having is the fact that if I make a capacity for an int array, won't the rest of the slots that have nothing be set to '0'?
Any ideas what is the best way to go about this? Like I could just scan for the 0's but the thing is if an int is actually a '0' then my code won't be able to tell the difference. I just wanna hear some ideas and see what the best method would be. Thanks for reading this I really appreciate it!