In the previous chapter you've seen how a TOM program is built from files, which contain classes and instances, which contain methods. This chapter explains the basics of methods, what you can make them do, and how to write that down in TOM.
Everything in a method body is an expression. We'll start with the simplest kind of expression: constants.
An example of a rather simple expression is 1 + 2. When executed its value is 3, just like you'd expect. Both the 1 and 2 themselves are expressions too. They are the simplest of expressions, and called constants. As with all expressions, constants have a type. The type of 1 is int.
int is one of the numeric TOM types. Table 2-1 lists all numeric types available in TOM. If you're familiar with C, you'll recognize most of them. The difference is that in TOM the precision, range and signedness of the types are fully defined: there is no difference between machines. Furthermore, the char and short C types have been replaced by byte and char respectively, for reasons which will become apparent soon.
There are several kinds of numeric constants, each with a specific type. Constants like 1, 123456, 0377, and 0xff are of type int. A leading zero, as in 0377, denotes a number in octal notation; 0xff with its leading 0x denotes a number in hexadecimal notation. The case of the hexadecimal digits is ignored, thus 0Xff, 0xFf, 0XFF, and 0xff are all equal.
Table 2-1. Numeric types
|byte||8 bit unsigned integer|
|char||16 bit unsigned integer|
|int||32 bit signed integer|
|long||64 bit signed integer|
|float||single precision floating point|
|double||double precision floating point|
An integer constant suffixed with l or L is of type long. Thus 0L is a 0 typed long. If an integer constant, which is not an explicit long, is too large to be held by an int, its type will be long. If even a long can not hold the value, an error is issued by the compiler.
A byte constant is written as a character enclosed in single quote characters. Thus, 'a' is a byte with a value of 97; 97 being the ASCII value of the letter `a'. The quote itself can be escaped using a `\' (backslash). Thus, '\'' is a byte with a value of 39. To get a backslash, it too must be escaped---'\\'.
Not all byte values can be entered as a character constant, simply because not all ASCII values translate to printable (and typeable!) characters. Such characters can be entered by escaping their octal value. Thus, '\041' is the capital letter A.
In general, it is awkward to have to remember numeric values for often-used non-printable characters. Table 2-2 lists the shorthands of important character constants which stand for some of the unprintable ASCII values.
Table 2-2. Important non-printable characters
A float constant is a number which includes a decimal point, an exponent, or both. Thus 1.0, 1e23, and 4.2e1 are all floating point constants of type float.
Floating point constants of type double must have an exponent part and have `d' as the exponent indicator. Thus 1d is a floating point 1 of type double.
A floating point constant which seems to be a float, but the value of which is too large to be held by a float, is also taken to be a double. If the value of a constant is too large to even fit a double, the compiler will issue an error.