35

in java I can create byte array: byte[] array = new byte[] { 0, 0, 0, 0, 0 }; but this construct is invalid in groovy. How I can create byte array in groovy ?

3 Answers 3

45

The following should suffice:

def array = [0, 0, 0, 0, 0] as byte[]

Have a look here for more details on arrays in groovy.

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

Comments

19

In addition to rich.okelly's answer,

byte[] array = [0, 0, 0, 0, 0]

works as well

Comments

5

You can't initialize a literal array the same way because Groovy thinks the curly brackets form a closure. What you want is something like

def x = [ 0, 0, 0, 0, 0 ] as byte[]

See more: here

Comments

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.