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.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Elias
  • 131
  • 2
  • 9

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

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140