Skip to content

confusion about js string vs java string #2153

@heywiki

Description

@heywiki

When i return a string from a java object i get a java string. I remember that in older versions of rhino this was causing trouble, so we
converted them with + "" to js strings. Now i try to figure out if + "" is still necessary to code with the returned string like it would be a
native rhino js string (currently we are using 1.8).

What i recognized is that i can now use "==" to compare them, don't know if that was always possible, but
if i call functions on them i would suspect that only java functions would work. Interestingly also js functions work on the returned
string. Could you clarify or point me to a documentation on how to handle java strings in rhino?

var pname = jpage.name();  //jpage is an Java object embedded in rhino, therefore it returns a java.lang.String
pl ("|pname|" + pname); //RhinoStringTest
pl("|cname()|" + cname(pname)); //java.lang.String (cname is helper function that gets class name, if obj is a java obj)
pl("|typeof|" + typeof pname);  //object

Comparing (assume pname is "RhinoStringTest")

if (pname == "RinoStringTest")      //matches...
if (pname === "RinoStringTest")      //does not match: java.lang.String vs object

Calling functions

pl("|split()|" + pname.split("."));    //should work since java has a split function, as has js
pl("|slice()|" + pname.slice(0,2));  //should NOT work since java string does NOT hava a splice function, but it WORKS??????

So the last line is actually causing the confusion. What is happening here. Is the object internally converted to a js object? If so,
should then === also work? It would be inconsintant if the one works, the other not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Java InteropIssues related to the interaction between Java and JavaScriptdocsIssues containing stuff that ought to be documented

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions