my dog learned polymorphism
The moose likes Beginning Java and the fly likes Regarding array declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Regarding array declaration

Pradeep Kumar
Ranch Hand

Joined: Sep 11, 2007
Posts: 77
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


SCJP 1.6
gang meng
Greenhorn

Joined: Sep 19, 2007
Posts: 3
I think that using the second method is the best usually.
gang meng
Greenhorn

Joined: Sep 19, 2007
Posts: 3
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

Joined: Sep 20, 2007
Posts: 5
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

Joined: Oct 27, 2005
Posts: 19216

The only difference is that the first version can only be used on the same line as the declaration.

Consider:


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32688
    
    4
Please don't ask the same question twice.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding array declaration
 
Similar Threads
integer literals
can anyone please check my method
Generating thumbnail on unix
What is the difference between these statements
getting values from JTable and putting them into textfields