Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between {} and {null}  RSS feed

 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi all

I have a method like
String[] getThings(){ return {}; } -I
String[] getThings(){ return {null}; } -II

What is the difference between both of these ?
II version is throwing me exception since i am doing sort() on Returned Array converted into Collection but not 1st one.

 
Campbell Ritchie
Marshal
Posts: 55734
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.printf("(new String[]{}).length = %d%n", (new String[]{}).length);
System.out.printf("(new String[]{null}).length = %d%n", (new String[]{null}).length);
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes right,

II array has length =1 and I is zero length.
Even it prints out value =null
Does that mean it treats null as literal even though i dint enclose it in quotes?i mean how is java going to interpret this 1 element.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are still objects, and therefore each String reference can also have the value null, just like any other object reference.
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So Is there any way to avoid this 'Null exception' because of null coming out from some other method my method is calling and adding to my collections
and doing sort on it.


Something like-
String[] array= SomeObject.getThings();
List<String> mylist=new ArrayList<String>(Arrays.asList(array));
Collections.sort(mylist); //Exception point

I was thinking of -
1-Use {"Null"} in getThings() instead of {null} ,which is 3rd party program ,i cannot change it unless i have to.
2.Is there any other implemention to avoid null objects?

Thanks


 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Specify a custom Comparator. By default, that method uses the compareTo method of the elements to order them, but most compareTo implementations cannot handle null. By using a Comparator you can solve this:
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys..
Another confusion is :
If my list has single element null ie .List -> [null]
and i do sort on it ,i don't get null pointer exception but if list has say - List->[assert, null]
and do the sort- it throws exception.
why is that so.
This is without using any comparator.Even if there is nothing to compare to , JVM tries to access null and it should throw the exeption.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When sorting, elements are compared to other elements. If there is only one element that element will be compared to exactly 0 other elements. In other words, the compareTo method won't be called. If there are at least two elements then the null element will be part of a compareTo method call, either as the reference the method is called on or the parameter to the method.
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much.

Is there any other case or example where a person would like to use String[]{null} (1 element ) instead of String[] {}?
null is not literal and using it creates NPE.Why would any one use this?


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!