Win a copy of Kotlin in Action this week in the Kotlin forum!
    Bookmark Topic Watch Topic
  • New Topic

Regarding array declaration  RSS feed

 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Report post to moderator
What is the differnece between the below two declarations.
As results of both the statements are the same.Apart from that are there any other differences.


int[] aray1 = {1,2,3,4,5,5};
int[] aray2 =new int[]{1,2,3,4};

Regards,
Pradeep
 
gang meng
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Report post to moderator
I think that using the second method is the best usually.
 
gang meng
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Report post to moderator
The first method delcares a primitive date type of array.That is a original array.
The second method declares a array of object.

hmilyalex258258@163.com
 
John William
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Report post to moderator
Certainly,there are differences.
Do you know the differences from heap and stack?
Array is an Object. When you declared an Object,the computer will create a space in the heap of memory and create a quote in the stack of memory. When you new an Object,the computer so do.

In my opinion,
int[] aray1 = {1,2,3,4,5,5};
The statement will create one space and one quote.

int[] aray2 =new int[]{1,2,3,4};
The statement will create two space and one quote.
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
The only difference is that the first version can only be used on the same line as the declaration.

Consider:
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please don't ask the same question twice.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!