Java Virtal Machine.net

[ News ] rss

February 01, 2011

Mark Wielaard: New GPG key. Finally created a new GPG key using gnupg. The old one was a DSA/1024 bits one and 8 years old. The new one is a RSA/2048 bits one. I will use the new one in the future to sign any release tarballs I might create. pub 2048R/57816A6A 2011-01-29 Key f...

More »

February 01, 2011

Andrew Hughes: [SECURITY] IcedTea6 1.7.8, 1.8.5, 1.9.5 Released!. We are pleased to announce a new set of security releases, IcedTea6 1.7.8, IcedTea6 1.8.5 and IcedTea6 1.9.5. This update contains the following security updates: The IcedTea project provides a harness to build the source code from OpenJDK6 u...

More »

March/2024
SunMonTueWedThuFriSat
     12
3456789
10111213141516
17181920212223
242526272829 30
31      

[ Archives News
for 'Developers' ]

home > news > developers > code advice #14: don't initialize fields to default values

Code Advice #14: Don't initialize fields to default values

(See intro for a background and caveats on these coding advice blog entries.) If you've been coding in C, you've probably picked up the habit of initializing all your fields: char *foo = NULL; int bar = 0; This is necessary, because in C, memory can be left uninitialized, so there's no telling what value foo will have before it is assigned something. In Java, however, the language specification clearly defines default values, so virtual machines will for example always initialize reference fields to null. Specifically, this means that code like the following is redundant: private int foo = 0; private Bar bar = null; private boolean baz = false; The alternative form of initializing the fields explicitly in the constructor, is the same: private int foo; private Bar bar; private boolean baz; public Foo() { foo = 0; Bar bar = null; baz = false; } You can leave out the above initializations, and the program will behave the same way. Carl Quinn once convinced me that this was more readable, so I picked up the habit, and I now swear by it. On the one hand, you can argue that leaving the explicit initializations in is more readable because you're making it really clear what it is you are intending. On the other hand, Java programmers quickly learn what th...


Date: October, 16 2006
Url: http://blogs.sun.com/tor/entry/code_advice_14_don_t


Others News

©2002-2019Java-Virtual-Machine.net https://mobirise.com/ | html website maker | easy website builder software | easyhtml5video.com | image slider