Skip to main content

Javascript Number is converted to a java String with trailing ".0"

10 replies [Last post]
robjd
Offline
Joined: 2008-09-25

Plugin: 1.6.0_10-rc2-b32
IE7

Hi,

Has anyone encountered the following problem?

I have a Javascript variable containing an integer value e,g. x = Number("3") which is passed to an applet method expecting a String parameter. In version 1.6.0_07 and prior versions the String would be converted to "3", but in 1.6.0_10-rc2-b32 it is converted to "3.0".

We can workaround this issue by applying the String() function before calling the applet or overloading our applet methods to receive an int and then explicitly converting to a String, but this will involve coding and re-testing.

Does anyone know if this new behaviour is what we should expect by a JavaScript Number to java String conversion? Should this be raised as a bug or do we need to make the necessary application code changes?

Thanks
Rob

Some example code:

Test1.java

import java.applet.Applet;
public class Test1 extends Applet {
public void init() {
}
public String stringTest(String str) {
String out = "Java String value =<"+ str + ">";
return(out);
}
}

index.html

var x;
x = Number("3");
document.write("javascript value=" +x + "," + document.test1.stringTest(x));

Using JRE 1.6.0_10-rc2-b32
returns:
javascript value=3,Java String value =<3.0>

Using JRE 1.6.0_07
returns:
javascript value=3,Java String value =<3>

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
magnum
Offline
Joined: 2003-09-20

... Which means it is not coming from Java 6, nor update 10 ;-)

robjd
Offline
Joined: 2008-09-25

I am using jscript version 5.7 if this helps. The problem may be in the javascript engine but the trailing ".0" on the String conversion only manifests itself when I toggle between jre 1.6.0_07 and 1.6.0_10 from the java console. I don't think this is changing the javascript engine but please correct me if I am wrong.

robjd
Offline
Joined: 2008-09-25

Just some additional information that may help:

jscript version is 5.7.0.16535

If I set the value of a variable as:
x=Number(3);
then it is converted correctly as a java String to "3" in 1.6.0_10.

But if set the value of a variable (with quotes around the 3) as:
x=Number("3");
then it is converted incorrectly as a java String to "3.0" in 1.6.0_10.

Both result in "typeof x" to return "number" but the objects are obviously slightly different otherwise java would convert them to the same value.

If any one knows some JavaScript code that would allow to drill down into the structure/contents of this JavaScript object it might give us a clue as to why the java applet interprets them differently.

This simple example simulates what is happening in our application. The applet communicating with our middleware returns integer values from the DB as strings. We use the JavaScript Number function in order to perform numeric operations and then we pass back this Number value to the applet, receiving it as a java String and later converting back to an integer. In update 10, the applet tries to convert a value like "3.0" back to an integer and fails with NumberFormatException.

I can't say this is a perfect design but it's given us no problems for many years until this latest update 10 release. Do we need to be more careful with our type definitions and re-code our application?

Thanks
Rob

rogyeu
Offline
Joined: 2006-07-30

Thanks for the information.

In your JavaScript testcase, I assume you mean:
"String out = str;"
instead of:
"String out = "Java String value =";

The behavior of Number to String conversion is not really specified.

Actually, I would not rely on the behavior of implicit casting (Number -> String). In fact, if change your Java method to:
String stringTest(int i) {
String out = Integer.toString(i);
return out;
}
I will get 3 instead 3.0.

We have a CR 656480 for this issue and we are working on it. In addition, 6741935 in build 33 will probably improve the behavior to what the you expect.

Message was edited by: rogyeu

robjd
Offline
Joined: 2008-09-25

Thanks very much for your help. It's good to know that someone is aware of this and working on it.

Somehow my example java code was corrupted during the post into the forum. I guess it doesn't like the particular sequence of ",< or > characters.

I can see that after hitting the Preview button the line is corrupted.
This is the same code with brackets instead:

String out = "Java String value =("+ str + ")";

anyway thanks for correcting the code and reproducing the problem.

To avoid writing a new java method and thus changing many lines of JavaScript, I did try overloading the java method, creating a new method with the same name to take an int and doing an explicit conversion to a String as you suggest. That works fine for when an integer or string value is passed, but it truncates to an integer when a float value is passed and prior versions (e.g. 1.6.0_07) fail with the error "java.lang.Exception: Unable to select a method, ambiguous parameters".

It looks like 1.6.0_07 and 1.6.0_10 are quite different in the way they handle JavaScript to Java parameter passing.

robjd
Offline
Joined: 2008-09-25

This problem is fixed with the b33 release

demonduck
Offline
Joined: 2008-03-14

Say it isn't so!

That would break a bunch of my code. I haven't tested that with u10.

Now I'm afraid to look. But which browsers and OS are you using?

Maybe it's not in Java but in the JavaScript engine.

robjd
Offline
Joined: 2008-09-25

I can reproduce the problem on both IE6 and IE7. OS is XP Pro SP2.

To reproduce the different behaviour between plugins, I toggle the JRE version (between 1.6.0_07 and 1.6.0_10) from the Java console using the Java tab and "View" option and then Advanced tab "Java-Plug-in". I think this only impacts the Java version, not JavaScript.

demonduck
Offline
Joined: 2008-03-14

Arrggghh! I'll do some tests on my code tomorrow.

More things seem to be breaking than getting fixed in u10. Maybe
they should stop development for a while and figure out what they
are doing....

magnum
Offline
Joined: 2003-09-20

In fact it is a "problem" coming from Rhino itself (Javascript engine). I encountered this same problem with Java 5 + Rhino, without JDK encapsulation, with the mozilla Rhino jar library.