Tuesday, February 23, 2010

Java Quiz: Array Initialization Answer

Its a difficult answer if you come from a Pascal (Delphi), C or C++ back ground where you had to initialize the array first with default values like 0 before using.

Java automatically initialize all elements in an array to default values, object references become null and primitive integers become 0.

This is in my SCJP exam studies, tricky since I come from a Pascal and C++ back ground.

In C/C++ you get some random data (whatever is in memory at that pointer address). So for them you need to initialize the array before use.

1 comment:

Ewald Horn said...

Needless to say, this has been the root of many C/C++ bugs in the past, so this is one of the many ways Java protects programmers against themselves.

Nobody screws up your code as you do...