Declaring arrays in Java

前端 未结 4 1953
予麋鹿
予麋鹿 2020-12-21 16:01

After reading, I came to know that, arrays in Java are objects. The name of the array is not the actual array, but just a reference. The new operator creates th

4条回答
  •  别那么骄傲
    2020-12-21 16:14

    Please follow comments

    int[] myArray = new int[5]; //memory allocated for 5 integers  with nulls as values
    
    
    int[] myArray= new int[]{5,7,3}; //memory allocated for 3 integers  with  values
    
    
    int[] myArray= {5,7,3}; // same as above with different syntax memory allocated for 3integers  with  values.
    

    Diffrerence between second and third style.

          someX(new int[] {1,2,3}); //  inline creation array  style
          someX(declaredArray);     // using some declared array
          someX({1,2,3});     //Error. Sorry boss, I don't know the type of array
    
    
          private  void someX(int[] param){
              // do something
          }
    

提交回复
热议问题