Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

What are arrays in Java? Do all array elements be initialized to default value?

0
Posted

What are arrays in Java? Do all array elements be initialized to default value?

0

Arrays are objects in Java. Array elements are just member variables of the class, and they are initialized to default value as any other class of objects.MyObject[] myobjarray; There is no array here, only an array reference type variable called myobjarray. If it is a field in another class, myobjarray will be initialized to null. If it is in a method, it will not be initialised. No exceptions here!!! MyObject[] myobjarray = new MyObject[5]; You are initializing myobjarray by constructing a MyObject[5] array, inside the new object instance of the MyObject[5] array, all fields will be initialized to default values, which are myobjarray[0] = null, myobjarray[1] = null, … and the length field would be initialized to 5. No exceptions here either!!!The only difference is that array classes do not have an known name. If you are really interested to know the class name of various arrays, you can see them here. It actually prints out some odd class names of arrays: TestRange.javaSee the bot

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123