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

前端 未结 5 658
忘掉有多难
忘掉有多难 2021-01-04 05:58

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:

相关标签:
5条回答
  • 2021-01-04 06:40

    1st Question answer:- {a,b,c} are the values to giving to array at the time of compilization. So, the name of the stuff is array initialization.... and those are not literals

    2nd question answer:- The difference is []{a,b,c} calling the parameterised constructor and {a,b,c} just intilizing the array...

    3rd: I cant get your questing

    4th: yes both are constructors....

    0 讨论(0)
  • 2021-01-04 06:47

    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).

    0 讨论(0)
  • 2021-01-04 06:50

    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.

    0 讨论(0)
  • 2021-01-04 06:58
    1. Syntactic Sugar
    2. The compiler fills it in, because it is able to deduce it
    3. Because on a return line the language doesn't provide that syntactic sugar
    0 讨论(0)
  • 2021-01-04 07:02
    char[ ] charAr=new char[10];
    

    Is a proper array declaration. You could assign charAr to another char array.

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

    Syntax where you do both declaration and initialization in one line. You can reassign this to a new char array.

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

    Is a array literal. You can assign literal value only where you define the array. You cannot reassign this to a new char array.

    You cannot return array literals because during compile time, the compiler does not know the type of elements you are returning.

    0 讨论(0)
提交回复
热议问题