3

I have been programming for quite sometime and I came across something I never noticed before.

What is the difference between these two?

double[] nums = {1,2,3,4};
double[] nums2 = new double[]{1,2,3,4,};

They both compile and have the same properties. At first I thought that nums2 could accept a new int[] and have integer values in it since its lower down in the hierarchy. But It actually didn't work.

YCF_L
  • 49,027
  • 13
  • 75
  • 115
Elias
  • 95
  • 1
  • 7

1 Answers1

3

The first way is just a shortcut syntax to create and initialize an array of the second way.

This is the only difference.


Take a look at https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

YCF_L
  • 49,027
  • 13
  • 75
  • 115