Created
February 4, 2015 22:51
-
-
Save krohne/4474c55c10d1df0b5fb4 to your computer and use it in GitHub Desktop.
Convert Java String object to JavaScript String primitive in Rhino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The good part: | |
// String(javaString).valueOf() | |
// Follow the same pattern to convert other Java types to corresponding JavaScript primitives | |
var javaString = new java.lang.String("test"); | |
print('javaString:', javaString); | |
print('typeof javaString:', typeof javaString); // Object | |
print('javaString instanceof String:', (javaString instanceof String)); // true (really a java.lang.String object) | |
print('String(javaString).valueOf() === "test":', String(javaString).valueOf() === "test"); // true: Converted to JS primitive |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@krohne: Thanks a ton! Have spent hours searching for a solution to java-js conversion.