hudi
hudi

Reputation: 16535

byte array in groovy

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 ?

Upvotes: 35

Views: 28307

Answers (3)

Rich O'Kelly
Rich O'Kelly

Reputation: 41767

The following should suffice:

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

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

Upvotes: 45

smcg
smcg

Reputation: 3273

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

Upvotes: 5

tim_yates
tim_yates

Reputation: 171144

In addition to rich.okelly's answer,

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

works as well

Upvotes: 19

Related Questions