0

In C++, the code Card cards[20]; initializes 20 Card objects, whereas in Java, the code Card[] cards = new Card[20]; initializes an array that can hold 20 Card objects, but the Card objects aren't actually initialized. You have to do

for (int i = 0; i < cards.length; i++) {
    cards[i] = new Card();
}

Is there any way to avoid having to do this in Java, similar to C++?

1
  • In Java you cannot declare objects, only object references. Commented Aug 31, 2014 at 18:37

3 Answers 3

7

No, there is not.

Array elements are values.

For reference types, that value is a reference. The default initialization value for references is null. That's the value that each element in the array will be initialized to. You have to explicitly initialize the elements yourself to reference new objects.

For primitive types, that value is the primitive type value, a char, an integer, a floating point value, or a boolean. The default value for those is '\u0000', 0, 0.0, and false, respectively. That's the value that each element in the array will be initialized to.

Sign up to request clarification or add additional context in comments.

7 Comments

What about arrays of primitives?
@ChristianHackl Elements of arrays of primitives are also initialized to their default value, 0 or false. Are you asking me to add that to the answer? I'll do that now.
Not a big deal, but I think your sentence "An array in java holds references." may mislead someone into thinking that arrays of primitives are not possible or are implemented with auto-boxing.
@ChristianHackl Yeah, I had that impression too. Made it cleaner and clearer.
and '\u0000' for char
|
5

No, there is no purely-array-way of doing what you want. If you don't want to use loops explicitly for this problem, then one of alternative ways could be using streams (added in Java 8).

Card[] cards  = Stream.generate(Card::new).limit(20).toArray(Card[]::new);

or maybe using Arrays.setAll will be less cryptic

Card[] cards  = new Card[20];
Arrays.setAll(cards, i -> new Card());

6 Comments

@HotLicks Believe it or not, there are some people to whom this actually is simpler and clearer :)
@HotLicks For me, yes, but it depends on personal preferences :) Anyway it is just an alternative.
No offense, but this looks like taken straight from a code obfuscation contest. Is this coding style really considered superior by renowned Java experts?
Looks very complicated and is doing too much for the mentioned purpose.
I think the setAll version reads much clearer, and may actually be a better choice than the loop.
|
2

The C++ equivalent of a Java Card[] cards = new Card[20]; would roughly be Card* cards[20]; (or std::array<Card*, 20> cards; in modern C++).

And as it turns out, you have to explicitly initialize every array element in C++ as well, if you want those pointers to actually point to Card objects in memory:

for (int i = 0; i < 20; ++i)
{
    cards[i] = new Card;
}

There is no Java equivalent for the C++ Card card[20], for the same reason that there is no Java equivalent for a C++ Card single_card;.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.