2

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?

1
  • 2
    How can we use both Java and Groovy syntax initializing array at the same class? - It's not both groovy and java, it's groovy. It may be similar to java sometimes, but it's still groovy. Commented Apr 11, 2019 at 13:07

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Basically only good thing is using same JVM runtime environement? I can use objects of Java classes in Groovy code and vice versa, right?
Yes, although it's much easier to use Java object in Groovy code than the other way around.
@Miki Also groovyc can work with both .java and .groovy source files (it delegates to javac for the first). That allows for easy mixing of code bases and prevent circular dependency problems. So you can at least mix and match at the file level.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.