[SalesForce] Null List VS Empty List

I have a basic question about how a list is loaded.

Consider these scenarios:

List<Lead> leadList1 = [SELECT Id FROM Lead];
List<Lead> leadList2 = new List<Lead>();
List<Lead> leadList3;
system.debug(leadList1, leadList2, leadList3);

Let's say that there are no leads in the database. When I go to system.debug these values, leadList1 will error out, leadList2 will be an empty string, and leadList3 will be null.

  • Why does leadList1 not return a NULL value or an empty string?

Best Answer

The results of a query are never null. An individual record at a given index in the results list will also never be null. Individual fields, however, can be null if they contain no data. If there are no leads the database, the first and second example are basically the same, except that one uses a query towards the governor limit.

Also, leadList2 is not an "empty string" (this has a specific meaning, namely an Object that is a String with a size of exactly zero). It simply happens to be rendered as an empty list via String.valueOf for debugging purposes. leadList1 and leadList2, in this example, are equal to each other (leadList1 == leadList2).

Edit: It's also not correct to say that you have a null list. A list is never null. The variable might be null, but a list itself is not null. Null is just null, and also has a specific intrinsic meaning. The following code demonstrates that there's no difference between a variable that's an uninitialized List versus an uninitialized Integer.

List<Lead> leads;
Integer anInt;

System.assertEquals((Object)leads, (Object)anInt);

By casting both variables to a common Object, we can see that null is always null, because it does not have a "type" in the common sense that other objects have types. There's no difference between a "null list" and a "null number".