How to use both Java and Groovy syntax when working with arrays?

Miki :

Hypothetical question from a fresh Groovy wanderer/learner:

If there are differences between initializing arrays as stated in official Groovy page, 3. Array initializers

In Groovy, the { …​ } block is reserved for closures. That means that you cannot create array literals with this syntax:

int[] array = { 1, 2, 3} 

You actually have to use:

int[] array = [1,2,3]

How can we use both Java and Groovy syntax initializing array at the same class? Wouldn't compiler report errors at compile time? If not - why would it compile code?

Karol Dowbecki :

You can't mix up the array initialization syntax between languages.

A class is defined in either .groovy or .java source file. A source file can't be compiled by both Groovy and Java compiler at the same time. Because of that you can't use the Java syntax in .groovy source file and Groovy syntax in .java source file.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=149956&siteId=1