Primitive Converter¶
JavetPrimitiveConverter is the basic converter that only takes care of primitive values. It is the base class of all other converters and handles the most common type conversions. It is optimized for performance with type checks ordered by statistical frequency.
From Java to JavaScript¶
Java |
JavaScript |
|---|---|
boolean/Boolean |
boolean |
byte/Byte |
int |
char/Character |
string |
double/Double |
number |
float/Float |
number |
int/Integer |
int |
long/Long/BigInteger |
bigint |
short/Short |
int |
String |
string |
ZonedDateTime |
Date |
null |
null |
Any unrecognized Java type is converted to undefined.
Optional Types¶
Optional, OptionalInt, OptionalDouble, and OptionalLong are supported. If the optional is present, the inner value is converted recursively. If empty, the result is null.
// Optional with value → converted inner value
converter.toV8Value(v8Runtime, Optional.of(123)); // → V8ValueInteger(123)
converter.toV8Value(v8Runtime, OptionalLong.of(100L)); // → V8ValueLong(100)
// Empty optional → null
converter.toV8Value(v8Runtime, Optional.empty()); // → V8ValueNull
converter.toV8Value(v8Runtime, OptionalInt.empty()); // → V8ValueNull
From JavaScript to Java¶
JavaScript |
Java |
|---|---|
boolean |
boolean |
int |
int |
bigint |
long/BigInteger |
number |
double |
string |
String |
Date |
ZonedDateTime |
null |
null |
undefined |
null |
Any unrecognized V8 value type (objects, arrays, functions, etc.) is returned as the raw V8Value instance.
Note
JavetPrimitiveConverteris typically not used directly. UseJavetObjectConverter(the default) for most use cases.If
bigintfalls in the range oflong, it is converted tolong. Otherwise, it is converted toBigInteger.Both
nullandundefinedfrom JavaScript are converted to Javanull.
Numeric Widening and Narrowing¶
When a JavaScript value is passed to a Java callback method, Javet performs automatic type coercion:
intandbigintcan be narrowed tobyte,short, or widened tolong,float,double.A single-character
stringcan be converted tochar. Multi-character strings are silently truncated to the first character.Both boxed (
Integer) and unboxed (int) parameter types are supported in callbacks.