This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes difference: int[] a = {1,2,3}; int[] b = new int[3]{1,2,3}; Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference: int[] a = {1,2,3}; int[] b = new int[3]{1,2,3};" Watch "difference: int[] a = {1,2,3}; int[] b = new int[3]{1,2,3};" New topic
Author

difference: int[] a = {1,2,3}; int[] b = new int[3]{1,2,3};

Sergii Trotsenko
Greenhorn

Joined: Oct 23, 2012
Posts: 6
Hi guys!

On the interview I've been asked the following question and was confused to be honest. Can anybody please explain?

What is the difference between:

int[] a = {1,2,3};

and

int[] b = new int[]{1,2,3};

Thanks!
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
My guess is that there is no difference beyond the syntax one: they get compiled the same way.

However if you manually declare the dimension, you do not make use of the compiler's ability to count the elements.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
That is different from what the thread title shows. I suggest you try compiling the two lines and see what happens. Note the new Foo[] part is needed when you are reassigning the array, and redundant in the two lines you quoted.
 
jQuery in Action, 2nd edition
 
subject: difference: int[] a = {1,2,3}; int[] b = new int[3]{1,2,3};