3

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

Meera Menon
  • 69
  • 1
  • 4
  • 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. – Peter Lawrey Nov 14 '12 at 09:30

2 Answers2

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.

Tomasz Gawel
  • 8,379
  • 4
  • 36
  • 61
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

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
  • 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. – Meera Menon Nov 15 '12 at 09:50