Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Why can't return array using {..} without new operator?

Tags:

java

arrays

I have searched a lot on the website, but didn't find any related question. So I believe it is not a duplicate.

I know we can initialize an array with 3 ways:

  • char[ ] charAr=new char[10];
  • char[ ] charAr={'a', 'b', 'c'};
  • char[ ] charAr=new char[]{'a','b','c'};

1st question is: what is the name of {'a','b','c'} kind of stuff? Is it called array literals?

2nd question is: what is the difference between new char[]{'a','b','c'} with {'a','b','c'}?

3rd question is: why I can't return a newly created array with {'a','b','c'}? I have to return new char[]{'a','b','c'}.

4th question: both new char[10] and new char[]{'a','b','c'} are constructors of array, right?

like image 982
user3068966 Avatar asked Dec 05 '13 07:12

user3068966


People also ask

Is it necessary to use new operator to initialize an array?

Arrays can be initialized using new or by assigning comma-separated values enclosed in curly braces. So when we initialize an array using Array literal as shown below. You do not need to use new ones. int[] myarray = {1,2,3,4,5};

Is new operator mandatory while initializing an array in Java?

It is necessary to use new operator to initialize an array.

What will happen if you don't initialize an array in Java?

If the array is not initialized at the time of declaration or any time after that then it will contain some random values in each memory position. These random values can be of two types: 1. Default values.


2 Answers

In the declaration:

char[ ] charAr={'a', 'b', 'c'};

the array {'a', 'b', 'c'} is called an array initializer. It is described in Section 10.6 of the Java Language Specification. It can only be written like that (without the new char[] in front of it) as part of a variable declaration.

In any other place (including a return statement), you need to use an array creation expression. These are described in Section 15.10 of the JLS. You can use either an array initializer or an array creation expression to initialize a variable when it is declared. That's why the new char[] appears to be optional when you declare the variable.

Regarding your 4th question: they technically are not "constructors" (which has a specific meaning in Java terminology) but yes, they both construct arrays (or, perhaps more accurately, they both create arrays).

like image 179
Ted Hopp Avatar answered Sep 28 '22 11:09

Ted Hopp


1st question is:what is the name of {'a','b','c'} kind of stuff? Is it called array literals?

No Array literals in Java, they are filling array with elements, i.e initialization.

2nd question is: what is the difference between new char[]{'a','b','c'} with {'a','b','c'}?

That first one called as * inline array declaration*.

With second you can assign it to a array which already with a defined type.

Example by me here :https://stackoverflow.com/a/19658726/1927832

3rd question is: why I can't return a newly created array with {'a','b','c'}? I have to return new char[]{'a','b','c'}.

Because in the first case the type is missing, and compiler doesn't know which type of elements they are.

4th question: both new char[10] and new char[]{'a','b','c'} are constructors of array, right?

That is creating an array of chars with specified length.

like image 32
Suresh Atta Avatar answered Sep 28 '22 13:09

Suresh Atta