• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can an arraylist hold null reference or not using add(Index i, Object obj) method?  RSS feed

 
Vinod Vijay
Ranch Hand
Posts: 165
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, while using overloaded add method(using 2 parameters) of array list class, can't I place deliberately an object at any position in array list which is not in sequence or order. For e.g- I kept on adding the objects using simple add method(which takes one argument i.e. object as parameter) like

*/

By doing this can't I think my array list in the end would be something like:-
One|Two|null|Three

I tried above case but failed on runtime although my compilation was successful. Does it mean that an array list can't hold null reference? If No, then I again tried like num.add("") is this same as null reference or not? Please clear my basic concept.


 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod Vijay wrote:By doing this can't I think my array list in the end would be something like:-
One|Two|null|Three


You can think that if you like, but it isn't the case. So it wouldn't be a good idea to think it.

I tried above case but failed on runtime although my compilation was successful. Does it mean that an array list can't hold null reference?


No, the code you used didn't add any nulls to the list, so it didn't test the question of whether an ArrayList could contain nulls.

If No, then I again tried like num.add("") is this same as null reference or not?


No, an empty string is not the same as a null reference. Here's how you represent null in Java:



You certainly chose a complex and roundabout way to find out whether an ArrayList can contain nulls. Here's a simpler way:

 
Vinod Vijay
Ranch Hand
Posts: 165
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
You can think that if you like, but it isn't the case. So it wouldn't be a good idea to think it.

Why are you saying like that? I tried as suggested by you to use null and it worked. I think array list can hold null references.


No, the code you used didn't add any nulls to the list, so it didn't test the question of whether an ArrayList could contain nulls.

Certainly not. However I tried but later commented that statement. //num.add(7,"SEVEN");



No, an empty string is not the same as a null reference. Here's how you represent null in Java:


Thanks for clearing this. So this means, null reference means no reference to an object on Java Heap and empty String means there is a reference to an object on Java Heap but for JVM it doesn't matter what the size of that object is until or unless it is eligible for Garbage Collection. Please correct me if I'm wrong.

You certainly chose a complex and roundabout way to find out whether an ArrayList can contain nulls. Here's a simpler way:





Updated code:-


Output:-
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod Vijay wrote:Can't I place deliberately an object at any position in array list which is not in sequence or order.
I tried above case but failed on runtime although my compilation was successful. Does it mean that an array list can't hold null reference? If No, then I again tried like num.add("") is this same as null reference or not? Please clear my basic concept.

If you look at the definition for List.add(int, E), you'll see that it says:
"Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices)."
and also specifies that it throws IndexOutOfBoundsException "if the index is out of range" (ie, < 0 || > size()).

All of which mean that you can't insert elements at arbitrary points (which is, in effect, what you're hoping to do).

There is however, a structure that allows you do do exactly what you want:
Map<Integer, E>
and it'll likely take a lot less space if your elements are widely spaced too.

HIH

Winston
 
Manjunath An
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this

You would see an output like [1, 2, null, 4]

And yes arraylist can hold null references
 
Vinod Vijay
Ranch Hand
Posts: 165
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manjunath An wrote:Try this



You would see an output like [1, 2, null, 4]

And yes arraylist can hold null references



Thanks Manjunath. When I looked at your code first without running it, I thought there is some mistake in the sequence which you had shown like [1, 2, null, 4]
because null reference would have replaced the object completely on position 2nd (object 4) but then I thought there is another method of array list class called set() which is used to update the object by replacing it when we provide the index position and new object value to be replaced, so here in this case, it is gonna insert it in between 2 and 4 where 4 will move towards right giving space to null....

I have verified it by running it as well. Yeah, I will remember now that an array list can hold null references as well.
Could you please confirm this also if I'm wrong in below statement:-
Vinod Vijay wrote:
Thanks for clearing this. So this means, null reference means no reference to an object on Java Heap and empty String means there is a reference to an object on Java Heap but for JVM it doesn't matter what the size of that object is until or unless it is eligible for Garbage Collection. Please correct me if I'm wrong. [/code]

 
Manjunath An
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's correct empty Strings are not null. As soon as we say empty Strings we mean there is an object of type String but it is just empty. i.e ""

When we do String s = new String(""); We have an object of type String (which is empty) and s is a variable of type String referring to that empty String.

In the next line if we do s = null; then the reference variable is now not pointing to any object but null.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manjunath An wrote:When we do String s = new String("");...

Which you should never (or hardly ever) do.
String s = "";
achieves exactly the same thing.

Winston
 
Vinod Vijay
Ranch Hand
Posts: 165
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Manjunath An wrote:When we do String s = new String("");...

Which you should never (or hardly ever) do.
String s = "";
achieves exactly the same thing.

Winston


Winston Gutkowski wrote:
Which you should never (or hardly ever) do.

Why Winston?

As this topic is touched now, I would like to know that what is the difference between the both i.e. String s = new String(""); and String s = "";

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod Vijay wrote:
As this topic is touched now, I would like to know that what is the difference between the both i.e. String s = new String(""); and String s = "";



The second one gets a reference to a String object in the constant pool, and stores that reference value in s.

The first one get a reference to a String object in the constant pool, creates a new String object, and passes the reference from the constant pool into the constructor, so the new String object can be a copy of the one from the pool.

Since Strings are immutable, it is pointless to copy a String object like this.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Since Strings are immutable, it is pointless to copy a String object like this.

Worse than pointless; it is a waste of resources and bloats the code.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nobody clicked this link: ArrayList? It tells you quite clearly whether nulls are permitted.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!