3

I saw a lot of SO posts saying that Java set any uninitialized variable to null (like here, here or here...).

But lately, I went upon this code, written by Google here :

cur =  cr.query(builder.build(), INSTANCE_PROJECTION, selection, selectionArgs, null);
while (cur.moveToNext()) {
    String title = null;
    long eventID = 0;
    long beginVal = 0;

    // Get the field values
    eventID = cur.getLong(PROJECTION_ID_INDEX);
    beginVal = cur.getLong(PROJECTION_BEGIN_INDEX);
    title = cur.getString(PROJECTION_TITLE_INDEX);

    // Do something with the values.
    ...
}

I would genuinely rather do this :

// Get the field values
long eventID = cur.getLong(PROJECTION_ID_INDEX);
long beginVal = cur.getLong(PROJECTION_BEGIN_INDEX);
String title = cur.getString(PROJECTION_TITLE_INDEX);

I assume Google developpers are somehow really qualified, so I wonder, since we are in the very same scope : what are the pros and cons of declaring the first way instead of the second ?

Community
  • 1
  • 1
Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
  • 1
    I´d say it´s a matter of style and how your programming style should look like based on the companies standard (or even personal standards). It might be more readable to them. But in the end this question is rather opinion-based in my eyes. – SomeJavaGuy Aug 15 '16 at 09:11
  • 3
    That is not what those posts say. Java only sets uninitialised member variables and static variables to null. This does not apply to local variables. – khelwood Aug 15 '16 at 09:14
  • When I learned to program in the 70’s, I was using Pascal, a language that at that time required you to separate the declaration of variables from their use. I still sometimes see a style developed back then being continued (though more and more seldom). In a modern programming language like Java, my personal taste is clearly for your version over Google’s, no matter how much I agree that Google developers are most often very qualified. – Ole V.V. Aug 15 '16 at 09:46
  • @khelwood so what is the difference between these 2 kind of statements ? And then, what are the pros ans cons of them ? – Dan Chaltiel Aug 15 '16 at 11:04

1 Answers1

9

It's a question of style. I don't initialise unnecessarily for two reasons:

  1. Doing so clobbers an extra check a Java compiler will give you as compilation will not be successful if a variable that is not initialised on all control paths is encountered.

  2. It gives the impression that null is an acceptable value for the reference, which often it isn't.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 8
    For a third point, initialising to some `null` or default value disallows making those variables `final` – JonK Aug 15 '16 at 09:15
  • 4
    I agree with the reasons why you say it is advisable not to initialize to null, but I think that if you claim "it is a question of style" you should be able to point to at least one reason why you might want to initialize to null. I can't think of a good reason to, except to satisfy definite assignment requirements that the compiler can't statically infer; and that's not a matter of style, that's to satisfy a requirement of the language. – Andy Turner Aug 15 '16 at 09:36
  • As possible fourth point: doing that hides incomplete initialization in `if/else` blocks where a variable isn't initialized in one possible route. The compiler would raise a compile time error in such case without prior initialization. – Tom Aug 15 '16 at 11:18