10.5. Constants and LiteralsLiterals are explicit values inserted into IDL code. Sometimes a literal is used to specify a default value for an interface attribute or to declare the value for a constant. Literals can be boolean (TRUE or FALSE), numeric (integer, floating point, or fixed point), or character-based (a single character or a string). Literals are most often used in IDL to initialize the values of constants. Constants are named variables that are restricted from being modified after being initialized. In IDL, a constant is declared using the syntax: // IDL const type identifier = value; where type is any valid basic data type or declared interface type, identifier is any valid IDL identifier, and value is any IDL expression that evaluates to a literal value. The initialization expression can be a simple literal or a complex expression combining multiple literals using logical or mathematical operators. You can declare a few useful numeric constants as follows, for example: // IDL const float half = 1 / 2; const float quarter = 1 / 4; Most of the operators present in C/C++, such as addition (+), subtraction (-), multiplication (*), division (/), and the logical and bitwise operators (|, &, ^, ||, &&, etc.) are supported by IDL. 10.5.1. Mapping Constants to JavaIf an IDL constant is declared within an interface definition, the constant is mapped to a public final static member on the corresponding Java interface. If the IDL constant is declared outside an interface definition, a Java interface is created to hold the constant value as a public static final value. The generated interface has the same name as the IDL identifier given to the constant, and the static class member has the name value. Consider the following IDL constant declaration: // IDL const float PI = 3.14159; interface GeometricOperators { ... This causes the generation of the following Java interface: // Java public final class PI { public static final float value = (float) (3.14159D); } In your Java code, you can reference the constant value using PI.value. 10.5.2. Boolean LiteralsThere are two boolean literals (naturally) in IDL. They are specified using the keywords TRUE and FALSE. Their IDL type is boolean. In Java, they are mapped to the boolean values true and false. 10.5.3. Numeric LiteralsInteger literals, floating-point literals, and fixed-point literals comprise numeric literals in IDL. 10.5.3.1. Integer literalsAn integer value in IDL can be declared in decimal, octal, or hexadecimal notation. Any sequence of digits that does not start with a zero is considered a decimal integer value. If the sequence is all digits but starts with a zero, it's assumed to be an octal value. If the literal starts with 0X or 0x, it's taken to be a hexadecimal value. 10.5.3.2. Floating-point literalsA floating-point literal is a decimal integer, optionally followed by a decimal point and a fractional component, and/or by the letter e or E followed by an exponent expressed as a decimal integer. Either the fractional component (with the decimal point) or the exponent (with the e or E) must be present for the literal to be interpreted as a floating-point value and not an integer. Similarly, either the initial integer component or the decimal point must be present. So, for example, these are valid floating-point literals: 2.34 0.314159e1 3E19 .0003413 10.5.3.3. Fixed-point literalsA fixed-point literal consists of a decimal integer, optionally followed by a decimal point and fractional component (expressed as a decimal value), followed by the letter d or D. Either the integer component or the fractional component must be present. The decimal point is optional. The trailing d or D must be present in order for the literal to be interpreted as a fixed-point value. The following are all valid fixed-point literals: 1.50d .025d 1.333D 12d 10.5.3.4. Mapping numeric literals to JavaNumeric literals are mapped by taking into account the context in which they are used. Typically, a literal initializes a constant, so the declared type of the constant has to be checked to determine whether the literal is valid for the type and how it should be mapped to a Java literal. For example, these two similar IDL constant declarations: // IDL const short largeVal = 2e5; const float largeFloatVal = 2e5; are mapped by Sun's idltojava compiler to these Java declarations: // Java public static final short largeVal = (short) (2e5D); public static final float largeFloatVal = (float) (2e5D); Sun's idltojava compiler doesn't do any type checking on the IDL literal before converting it to its Java form and inserting it into the cast operation shown previously. So it is possible for the idltojava compiler to generate invalid Java code. For example: // IDL const float literalTest = TRUE; is converted without warning by idltojava to: // Java public static final float literalTest = (float)(true); 10.5.4. Character LiteralsA character literal is a character specification enclosed in single quotes (e.g., 'a'). Character literals can be specified using only elements of the ISO 8859-1 character set. Some characters need to be specified with a sequence of more than one character. These include characters that are nonprintable and the single- and double-quote characters that delimit string and character literals. These characters are specified with escape sequences, which start with a backslash character (\). Table 10-3 lists the escape sequences supported by IDL and the nonprintable characters they represent. Table 10-3. IDL Escape Sequences
Character literals, including the escape sequences listed in Table 10-3, are converted unchanged into Java literals. 10.5.5. String LiteralsA string literal is a sequence of characters delimited by double quote (") characters. If two string literals are adjacent to each other in an IDL file, they are concatenated. So, in this example: // IDL const string acctHolder = "Jim" "Farley"; the generated Java code is:[3]
// Java public static final String acctHolder = "Jim Farley"; If you want to use the double-quote character in a string literal, you have to use its escape sequence (see Table 10-3). Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|