12

Question is in

String text1;
String text2 = null;

Is there any difference? Why is it possible at all to 'initialize' with null?

EDIT: I specify my question. Why someone will write String text2 = null;?

EDIT2:

Contact contact1;
List<Contact> contacts = [...];
if (!contacts.isEmpty()) {
 contact1 = contacts[0];
} else {
 contact1 = new Contact();
}

and in another class I see

Contact contact2 = null;
List<Contact> contacts = [...];
if (!contacts.isEmpty()) {
 contact2 = contacts[0];
} else {
 contact2 = new Contact();
}

If it were different persons - ok - this might be the habit.

  • 2
    There's no difference. It's possible to assign null to a variable, albeit on declaration or just somewhere after it's been declared. – rael_kid Oct 18 '14 at 16:04
12

They really are on in the same as when you initialize a variable with no value, it's set to null automatically.

You can test this by running this in an execute anonymous window

String s1;
system.debug('s1 value is: ' + s1);

String s2 = null;
system.debug('s2 value is: ' + s2);

This code produces the following

enter image description here

As far as why someone would explicitly set it to null, I really think that might just be habit or preference, as I can't think of a specific reason as to why you would NEED to.

Similar to how some people initialize a list by using list<sObject> vs sObject[]. Neither one is incorrect and they both equate to the same thing.

I may be wrong and perhaps someone can prove me wrong, but I can't think of a reason to HAVE to set it explicitly to null.

  • I know that they are the same. The question is why to do so? What for? – Andrii Muzychuk Oct 18 '14 at 16:33
  • 2
    I edited the answer. I really don't think there is a reason to to do it. – Chris Duncombe Oct 18 '14 at 16:37
  • 6
    I suspect the initialization is a carry over from other languages like Java, where the code won't compile if a local variable is uninitialized. You'd get a compiler error like "variable contact2 might not have been initialized", whereas Apex just sets it to null. – Peter Knolle Oct 19 '14 at 0:17
  • Good point @Peter Knolle, that could the reason some people do this as well. – Chris Duncombe Oct 19 '14 at 12:21
  • 1
    @sfdcfox - My comment was for local variables, specifically. I'm pretty sure that those must be initialized. You are thinking of instance variables, I suspect, that will default, if not initialized. I can see how it makes sense with Apex to have it always default. Much simpler. – Peter Knolle Oct 20 '14 at 16:38
7

The two are logically the same, but one is less efficient than the other.

This is one logical unit of execution:

String test1;  // Declare

This is two logical units of execution:

String test2 = null; // Declare/assignment

This leads to a small performance penalty that you can observe in large loops:

Long t1 = DateTime.now().getTime();
for(Integer i = 0; i < 100000; i++) {
    String s = null;
}
Long t2 = DateTime.now().getTime();
for(Integer i = 0; i < 100000; i++) {
    String s;
}
Long t3 = DateTime.now().getTime();
System.debug(LoggingLevel.ERROR, t2-t1);
System.debug(LoggingLevel.ERROR, t3-t2);

Typical examples:

09:48:26.028 (28836934)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
09:48:26.113 (113284690)|USER_DEBUG|[10]|ERROR|47
09:48:26.113 (113325519)|USER_DEBUG|[11]|ERROR|36

09:48:20.024 (24803638)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
09:48:20.100 (100566826)|USER_DEBUG|[10]|ERROR|39
09:48:20.100 (100609709)|USER_DEBUG|[11]|ERROR|36

09:48:04.068 (68311804)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
09:48:04.151 (151989844)|USER_DEBUG|[10]|ERROR|44
09:48:04.152 (152032421)|USER_DEBUG|[11]|ERROR|39

This yields about a 10% increase in execution time. Of course, in all but the largest loops, this difference would be inconsequential.

0

String and Contact both are reference type. It is automatically initialize with "null". when we are not initialize with any value that time compiler automatically initialize with null. That why both are same.

Regards Satya

0

Sometimes, I initialize my variables explicitly, (ie., String str1 = null;) just for the sole purpose of code readability; for the benefit of myself or other developers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.