Usage of Java objects in JavaScript in Nashorn

suggest change

It’s possible to pass Java objects to Nashorn engine to be processed in Java code. At the same time, there are some JavaScript (and Nashorn) specific constructions, and it’s not always clear how they work with java objects.

Below there is a table which describes behaviour of native Java objects inside JavaScript constructions.

Tested constructions:

  1. Expression in if clause. In JS expression in if clause doesn’t have to be boolean unlike Java. It’s evaluated as false for so called falsy values (null, undefined, 0, empty strings etc)
  2. for each statement Nashorn has a special kind of loop - for each - which can iterate over different JS and Java object.
  3. Getting object size. In JS objects have a property length, which returns size of an array or a string.

Results:

Type | If | for each | .length | — | — | — | — | Java null | false | No iterations | Exception | Java empty string | false | No iterations | 0 | Java string | true | Iterates over string characters | Length of the string | Java Integer/Long | value != 0 | No iterations | undefined | Java ArrayList | true | Iterates over elements | Length of the list | Java HashMap | true | Iterates over values | null | Java HashSet | true | Iterates over items | undefined |

Recommendatons:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Nashorn JavaScript engine:
* Usage of Java objects in JavaScript in Nashorn

Table Of Contents
8 Arrays
10 Maps
11 Strings
25 JAXB
29 Enums
32 Audio
35 Nashorn JavaScript engine
41 Scanner
63 Logging
75 Lists
78 Sets
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap