Data types in Nashorn has two categories: Primitive types and Object types.
Primitive types include the following five data types:
The Undefined type has only one value: undefined
.
A variable declared in Nashorn without assigning a value has the value undefined
.
We can also assign the value undefined
to a variable explicitly.
And we can compare another value with undefined
.
The following code shows how to use the value undefined
:
In the following line, a is initialized to undefined implicitly.
var a;
b is initilaized to undefined explicitly.
var b = undefined;
Print out the value of a and b.
print("a is", a) print("b is", b);
Compare the value of a and b.
if (a == undefined) { print("a is undefined") } if (b == undefined) { print("b is undefined") }
The Null type has only one value that is called null
.
And null
value is used for null object.
The following code shows how to use the value null
:
var person = null; print("person is", person);
Nashorn does not distinguish between integers and floating-point numbers.
It uses Number
to represent both types of numeric values.
We can represent number literals in decimal, hexadecimal, octal, and scientific notations.
Nashorn defines three special values of number type:
In scripts, these special values are represented by NaN
,
+Infinity
, and -Infinity
, respectively.
The positive infinity value can also be represented as simply Infinity.
The following code shows how to use number literals and special number type values:
var intNumber = 100; // An integer of type Number var salary = 1234.678; // A floating-point number of type Number var hexNumber = 0x0061; // Same as 97 is decimal var octalNumber = 0141; // Same 97 in decimal var scientificNumber = 0.97E2; // Same 97 in decimal var notANumber = NaN; var posInfinity = Infinity; var negInfinity = -Infinity;
The Boolean type represents a logical value: true
or false
.
Nashorn has two literals, true and false, of the Boolean type:
var isDone= true; print("isDone=", isDone);