• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sergii Trotsenko
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 981
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic