0

Reading about Java's default initialization of class fields, I have gotten confused as to how the default values offer advantage over the garbage values of variables like in C.
For integers, an initial integer value of -1933438 or 0 would both render wrong calculations.Same is the case with characters and floats.
And, in case of pointers, there are none in Java and NULL reference leads to Runtime exception. So, references are safer.

So, how are the default values better?

Manish Kumar Sharma
  • 12,982
  • 9
  • 58
  • 105
  • I don't know about C, but having arbitrary standard default values is an advantage insofar you know what to expect, i.e. if you haven't initialized an `int` you know it will have value `0`, an object will be `null`, etc. As every arbitrary decision, it may generate some discussion, not necessarily worth having. – Mena Jan 11 '16 at 11:03
  • references **are** initialized to null by default. And that leads to runtime exceptions only if your code has a bug and forgets to check for null when it should. Having default values is better, IMO, because it simply makes the code less verbose when you want your variable to be initialized to 0, false or null, which is quite often the case. – JB Nizet Jan 11 '16 at 11:06
  • @JBNizet Less Verbose? It is never recommended to rely on the default value provided by Java as a good programming practice. Let's say, you use it directly as 0. But if you write, x=0; explicitly, it would imply, you intended it. You would save some other programmer from calling you and confirm your decision. – Manish Kumar Sharma Jan 11 '16 at 11:09
  • 1
    Default values do not cause [undefined behavior](http://stackoverflow.com/questions/11962457/why-is-using-an-uninitialized-variable-undefined-behavior-in-c). Even if the computation is wrong, or an exception is thrown. In doubt, one could say: "They have predefined default values, in order to have *defined* values, at least". (I don't think that the Java Language or VM spec contains the words "undefined behavior" **at all** - and IMHO, this is a good thing). – Marco13 Jan 11 '16 at 11:16
  • 1
    @pulp_fiction that's your best practice, which is probably useful in C, but not in Java. Mine (and others) is to avoid specifying things that don't need to be. See http://pmd.sourceforge.net/pmd-5.1.1/rules/java/optimizations.html#RedundantFieldInitializer for example. If I don't explicitly initialize a field, then my intention is not to do it and rely on the **well-known, clearly specified** default value. – JB Nizet Jan 11 '16 at 11:27
  • Zero and null values are *natural defaults*. And they are *consistent*. Your code will work the same way regardless of previously executed programs. – Gergely Bacso Jan 11 '16 at 12:09
  • As far as I know, it is best best practice even in C to manually set that memory area to 0 (aka NULL) when deallocating. See http://stackoverflow.com/questions/1931126/is-it-good-practice-to-null-a-pointer-after-deleting-it , especially Adrian McCarthy's answer. He explains in detail why it is a very, very big problem to expose such items in memory for future processes to exploit. – Cristina_eGold Jan 11 '16 at 14:11

0 Answers0