Since the Java part has already answered, I will add the C ++ part a bit. The specific version of brace initialization that you call is called aggregate initialization and (not surprisingly) is used to initialize aggregates. Each element in the aggregate will be initialized with the corresponding element inside curly brackets, and you can use what you want to use, which can be implicitly converted to the type of the element in the aggregate.
There are a couple of specific parts of the function that you can consider for the specific case of arrays. The number of elements inside curly brackets cannot be greater than the size of the array, but it can be less, in which case the remaining elements will be initialized by default.
int a[5] = { 1, 2 };
If the size of the array is not specified in the user code, the compiler will set it to the number of elements in the aggregate initialization list:
int a[] = { 1, 2, 3 };
Note that unlike Java, size is an integral part of an array type, so although you can enter int a[] = { 1 }; It can never be a shared array of an undefined number int .
In C ++ 11, the curly bracket syntax has been extended to provide uniform initialization, but this is probably beyond the scope of the question, I just mentioned this if you want to read more on this subject.
source share