Primitive Data Types and Literals

Treescript defines eight primitive types. Variables that are declared as a primitive type are not objects. They are only placeholders to store primitive values. The eight primitive types are byte, short, int, long, float, double, char, and boolean.

 

The byte, short, int, and long types represent 8-, 16-, 32-, and 64-bit integer values. The literal values of these types are written using positive or negative decimal, hexadecimal, or octal integers. Hexadecimal values are preceded by 0x or 0X and use letters a through f (upper or lower case) to represent the digits 10 through 15. Octal numbers are preceded by 0. Long decimal values have an l or L appended to the end of the number.

 

The float and double types represent 32- and 64-bit floating-point numbers. float numbers have the f or F suffix, double numbers have d or D. If no suffix is provided, the default double type is assumed. Floating-point numbers may be written in any of the following four forms:

 

·      digits . optional-digits optional-exponent-part suffix

·      . optional-digits optional-exponent-part suffix

·      digits exponent-part suffix

·      NaN

 

The suffix is optional. It consists of f, F, d, or D, as described previously.

 

The exponent part is optional in the first two forms but required in the third form. It consists of an e or E followed by a signed integer. It is used to identify the exponent of 10 of the number written in scientific notation. For example, 1000000.0 could be represented as 1.0E6.

 

The special value NaN is used to represent the value "not a number," which occurs as the result of undefined mathematical operations such as division by zero.

 

The char type represents 16-bit Unicode characters. Unicode is a 16-bit superset of the ASCII character set that provides many foreign-language characters. A single character is specified by putting the character within single quotes (‘). There are two exceptions: single quote (‘) and backslash (\). The backslash character (\) is used as an escape code to represent special character values. The character escape codes are:

 

Escape Code

Character

\b

Backspace

\t

Tab

\n

Linefeed

\f

Form feed

\r

Carriage return

Single quote

\\

Backslash

 

The backslash can also be followed by an 8-bit octal value, or by u or U followed by a four-digit hexadecimal value. The four-digit value is used to specify the value of Unicode characters.

 

The boolean type represents the logical values true and false.

 

String literals are also provided by Treescript, although strings are not primitive values. Strings consist of characters enclosed by single quotes (‘). The character escape codes may be used within strings. In case the string literal consists of a single character, the context is used to determine whether it’s a string literal or a character constant.

 

The literal value nil is used to identify the fact that an object is not assigned to a value. It may be used with any variable that is not of a primitive data type.