Hi, I have noticed seeing a lot of: public class A { String theString = null; } But theString is going to be null anyways since it's initialized by default.
I thought i missed something. I am new to Java. Passed JCP but, well, not much experience. So asked out of curiosity, maybe I don't know something. Thanks!
I guess u cannot print the string that is not initialized... In this case, it is string3. U won't be able to compile it, if u don't initialize the string3... Just want to share some experiment...
Co-author of SCMAD Exam Guide, Author of JMADPlus SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Hi Ko Ko, You totally right with your example. Java does not initialize variables declared inside the method scope. But it does so if the variable is a class field. Class field variables are guaranteed to be initialized. Also array elements.
It doesn't need. But probably a programmer forgot the thing and/or previous experience was C++. If compiler is smart enough it do noting is such case. So it's NOP.