| Java Type | JS Type | Clarification |
|---|---|---|
| boolean | boolean | |
| byte | number | |
| char | number | |
| short | number | |
| int | number | |
| long | goog.math.Long | |
| float | number | |
| double | number | |
| java.lang.Boolean | boolean | null | See Boxing section below |
| java.lang.Byte | java.lang.Byte | null | |
| java.lang.Character | java.lang.Character | null | |
| java.lang.Short | java.lang.Short | null | |
| java.lang.Integer | java.lang.Integer | null | |
| java.lang.Long | java.lang.Long | null | |
| java.lang.Float | java.lang.Float | null | |
| java.lang.Double | number | null | See Boxing section below |
| java.lang.String | string | null | See Boxing section below |
| java.lang.Object | * | See Boxing section below |
Boxed types add a noticeable amount of performance overhead to JavaScript. To eliminate this overhead, J2CL never uses boxed representation for Double, String and Boolean. Double becomes a JavaScript number and Boolean becomes a JavaScript boolean. It‘s not possible to use plain numbers for boxed Integers because it leads to ambiguity. For example, the instanceof operator wouldn’t be able to distinguish between Integer and Double:
Object foo = new Double(42) // If both Integer and Double were represented with plain js number then there would be no way // to perform this runtime check for the type. if (foo instanceof Integer) { x--; } else if (foo instanceof Double) { x++; }
Note that no such ambiguity exists for primitive ints, which is why they can be represented using a native JavaScript number.
JavaScript natively supports strings and J2CL automatically unboxes Strings. So there is a one-to-one mapping between Java String and the native JavaScript string type.
JavaScript does not natively support integers. J2CL emulates integers using the native JavaScript number type: a 64-bit double. Note that boxed Integers remain boxed, unlike Double and Boolean.
JavaScript natively supports doubles and J2CL automatically unboxes Doubles. So there is a one-to-one mapping between Java Double/double and the native JavaScript number type.
JavaScript does not natively support longs. J2CL emulates longs using the Closure long library: goog.math.Long
JavaScript natively supports booleans and J2CL automatically unboxes Booleans. So there is a one-to-one mapping between Java Boolean/boolean and the native JavaScript boolean type.
From J2CL's perspective null and undefined are the same type. If you check if a value is equal to null then it will return true if the value is null or undefined.
TODO(rluble): complete the following table
toString() | equals() | hashcode() | getClass() | |
|---|---|---|---|---|
| Arrays | ||||
| Native arrays | ||||
| JsFunction | ||||
| JsFunction | ||||
| : implementation : : : : : |
java.lang.Object[] are not stamped. new Object[]{ "a" } in Java and ["a"] in JS are effectively same.@JsConstructor are required to have a @JsConstructor.@JsConstructor is implicitly a @JsConstructor.