3

Can we create primitive arrays like byte array or int array using javax.script?

1
  • 1
    You either create the array in Java the normal way or you create it in the scripting language. How you do that depends on the language scripted. Commented Nov 14, 2012 at 9:30

2 Answers 2

7

To create a Java Array of 10 bytes in Mozilla's Rhino:

var buff = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 10)

Notice that java array is different type than native JavScript array - they both can be used in Rhino JavaScript.

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

Comments

1

If you are using the JavaScript engine of the package, use

var array = new Array();
array[0] = 1;

I'd recommend a reading through

Java Docs Java6Master Java Scripting Programmer's Guide

1 Comment

Basically, it needs to be an array of 1024 bytes size. In Java, it used to be byte[] bytes = new byte[1024]; But in javax.script with javascript, I can not find a way to do this.

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.