Java Reference
In-Depth Information
typically, the __FILE__ , __DIR__ , and __LINE__ global properties are used for
debugging purposes. You can also use __DIR__ property to load scripts from locations
relative to the currently running script file.
Tip
Built-in Global Functions
Nashorn defines built-in global functions. I have discussed one of such functions, named
eval() that is used to evaluate a script stored in a string. In this section, I will discuss
some more built-in global functions.
The parseInt( ) Function
The parseInt() function is used parse an integer from a string. It has the following
signature:
parseInt(string, radix)
The function returns an integer by parsing the specified string . If radix is specified,
it is considered the base of the integer in string . The leading spaces in string are
ignored. If radix is not specified, undefined , or 0, radix is assumed to be 10, except when
string starts with 0x or 0X (ignoring the leading spaces) in which case radix is assumed
to be 16. If radix is less than 2 or greater than 36, the function returns NaN .
The parseInt() function parses only the leading portion of string that can be a
valid digit in an integer represented in the specified radix . When it encounters an invalid
character, it stops parsing and returns the so far parsed integer value. It returns NaN , if
no leading characters can be parsed into an integer. Notice that you do not get any error
when string contains invalid characters. The function always returns a value.
the parseInt() function in nashorn works similar to the Integer.parseInt()
method in Java, except that the former is very lenient and does not throw an exception.
Tip
The following snippet of code shows the output of calling the parseInt() function
with different arguments:
printf("parseInt('%s') = %d", "1969", parseInt('1969'));
printf("parseInt('%s') = %d", " 1969", parseInt(' 1969'));
printf("parseInt('%s') = %d", " 1969 Hello", parseInt(' 1969 Hello'));
printf("parseInt('%s') = %s", "Hello1969", parseInt('Hello1969'));
printf("parseInt('%s') = %d", "0x1969", parseInt('0x1969'));
printf("parseInt('%s', 16) = %d", "0x1969", parseInt('0x1969', 16));
printf("parseInt('%s', 2) = %d", "1001001", parseInt('1001001', 2));
 
 
Search WWH ::




Custom Search