Tuesday, 31 March 2015

Uninitialized local variables in C++ vs Java

If you did not initialize a local variable the variable might contain a garbage value which cannot be determined (in both C++ and Java ).  For example

int x;

x will not be intialized automatically to zero( if it is an instance variable, then yes it would be intialized to zero in Java).

You are responsible for assigning an appropriate value before you use x. If you forget you get a compile-time error telling you the variable might not have been initialized. (Many C++ compilers will warn you about uninitialized variables, but in Java these are errors.)

No comments:

Post a Comment