Enrol to start learning
Reading is open to everyone. Enrolling is free, and it is what unlocks the audio lessons, practice tests and progress tracking.
29.4. Working with Variables and Bindings
Learn content
Interactive Audio Lesson
Unlock the classroom podcast
The transcript is free to read. A free account plays the conversation back.
Today we're discussing how to work with variables and bindings in the Nashorn JavaScript engine. Bindings allow us to transfer data between Java and JavaScript. Can anyone tell me what they think bindings might mean?
I think it's like linking two things together?
Exactly! It’s like a bridge that connects our Java variables to JavaScript. Remember the acronym 'LINK' to help you recall that Bindings: L for Link, I for Is, N for Necessary, K for Knowledge!
How do we actually create these bindings?
Great question! We create bindings using engine.createBindings() in our code. After that, we can use put() to add our Java variables!
Unlock the classroom podcast
The transcript is free to read. A free account plays the conversation back.
Now that we’ve created our bindings, let’s see how we can use them in our scripts. When we set the bindings with engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE), how do you think it affects the accessibility of our data in JavaScript?
I guess it makes those variables available in the JavaScript context?
Exactly! The variables become accessible to the JavaScript code. Let's take an example where we sum two numbers. If x=10 and y=20, when we run print('Sum = ' + (x + y));, what do you expect the output will be?
It should print 'Sum = 30'!
Exactly! That's how we can dynamically interact with our JavaScript using Java variables.
Unlock the classroom podcast
The transcript is free to read. A free account plays the conversation back.
Bindings can be useful in many scenarios, like controlling the flow of scripts based on Java application's state. Can someone think of a situation we might need to use bindings?
Maybe if we have settings configured in Java that we want to affect how the script executes?
Exactly! For instance, if a user selects settings in a Java application, we could bind those values to a script that makes decisions based on those settings. This leads us to dynamic, configurable applications!
That sounds really powerful!
It is! Remember, the strength of scripting with bindings lies in its flexibility and the ability to modify behaviors on the fly. Alright, let’s recap what we learned today about bindings!
Overview
Short Summary
This section discusses how to pass variables from Java to scripts using Bindings in Java's scripting API.
Medium Summary
It highlights the use of the Bindings object in the Nashorn JavaScript engine to facilitate communication between Java and JavaScript. The section also provides examples demonstrating how to pass variables and utilize them in scripts.
Detailed Summary
Working with Variables and Bindings in Java's Scripting API
In the Java Scripting API, particularly with the Nashorn JavaScript engine, developers can seamlessly pass variables from Java to embedded scripts using a special object known as Bindings. Bindings functions like a map, allowing developers to define key-value pairs which can be accessed by the script being executed.
Here's how it works: First, create an instance of Bindings using the createBindings() method of the ScriptEngine. Then, values can be put into the bindings using the put() method to define the variables needed by the script. Once the Bindings are set up with the required variables, developers can specify the scope in which these bindings should be available (commonly the ScriptContext.ENGINE_SCOPE), ensuring the script can retrieve these external values.
For example, if x and y are integers in Java, they can be put into the bindings object. In the script, you can easily sum these numbers and display the result. This capability is crucial for dynamically integrating values from Java into the JavaScript context and enhancing the interactivity of applications.
Reference YouTube Videos
Audio Book
Unlock the audio lesson
The script is above and free to read. A free account plays it back, in the voice you pick.
Create a free accountYou can pass variables from Java to the script using the Bindings object:
Bindings bindings = engine.createBindings();
bindings.put("x", 10);
bindings.put("y", 20);
engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);Detailed Explanation
This chunk explains how to pass variables from Java to a JavaScript script by using the Bindings object. First, you create an instance of Bindings with engine.createBindings(). Then, you use the put method to add key-value pairs to the bindings. For example, bindings.put("x", 10) assigns the value 10 to the variable 'x'. After adding all necessary variables, you set the created bindings in the script engine's context using engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE).
Examples & Analogies
Think of Bindings as a box where you can store items (variables). Before sending the box to a friend who will use it (the script), you put in the things they need (variable names and values). This way, when they open the box (when the script runs), they can find exactly what they need to do their job.
Unlock the audio lesson
The script is above and free to read. A free account plays it back, in the voice you pick.
Create a free accountengine.eval("print('Sum = ' + (x + y));");Detailed Explanation
In this part, you see how to use the variables that were passed into the script. With engine.eval(), you can execute a piece of JavaScript code, in which you can use x and y that were defined before. The code here prints the sum of x and y using JavaScript syntax: print('Sum = ' + (x + y));. The + is used for string concatenation, so it combines the string 'Sum = ' with the evaluated sum of x and y.
Examples & Analogies
Imagine you've just sent a calculator (the script) to your friend using the box (Bindings) filled with numbers (variables). When your friend opens the box and uses those numbers to perform calculations, they can tell you the result of their work. In this case, they tell you 'Sum = 30', which is the sum of the numbers you provided.
--
Key concepts
Core takeaways and short definitions to help you quickly recall the key ideas from this section.
- Bindings:
A key-value map in Java's scripting API for passing variables to scripts.
- Script Engine:
The engine that executes the scripts, allowing interaction with Java.
- Dynamic Applications:
Applications that modify behavior based on user inputs or states.
Examples
Step-by-step examples to apply the section's ideas and test your understanding.
Creating bindings and adding variables:
Bindings bindings = engine.createBindings();
bindings.put("x", 10);
bindings.put("y", 20);
Using bindings in a script:
engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
engine.eval("print('Sum = ' + (x + y));");
Memory aids
Imagine a bridge (Bindings) connecting Java land to JavaScript city, facilitating the flow of data across the river of execution.