Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Difference between {} and {null}

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 76487
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic