Section G.5. Constants and Literals


G.5. Constants and Literals

Literals are explicit values inserted into IDL code. Sometimes literals are used to specify a default value for interface attributes 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 value 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 spec> <identifier> = <value>; 

where <type spec> 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 it can be a complex expression combining multiple literals using logical or mathematical operators. You could 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 (*), and the logical and bitwise operators (|, &, ^, ||, &&, etc.) are supported by IDL.

G.5.1. Mapping Constants to Java

If an IDL constant is declared within an interface definition, then the constant is mapped to a public static final static member on the corresponding Java interface.

If the IDL constant is declared outside of an interface definition, then 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. So this IDL constant declaration:

 // IDL const float PI = 3.1416; interface GeometricOperators {   const long maxDims = 3;     ... 

causes the generation of a Java GeometricOperators interface:

 public interface GeometricOperators extends foobarOperations,   org.omg.CORBA.Object, org.omg.CORBA.portable.IDLEntity {   public static final int maxVals = (int)((int)-3);   ... 

as well as a separate interface to hold the IDL constant PI that is declared outside of any interface:

 // Java public final class PI {     public static final float value = (float) (3.1416D); } 

In your Java code, you can reference the value of PI using PI.value, and the value of the interface-specific constant using GeometricOperators.maxDims.

G.5.2. Boolean Literals

There 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 into the boolean values TRue and false.

G.5.3. Numeric Literals

G.5.3.1. Integer literals

An integer value in IDL can be declared in decimal, octal, or hexadecimal notation. Any sequence of digits that doesn't 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.

G.5.3.2. Floating-point literals

A 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.31416e1 3E19 .0003413 

G.5.3.3. Fixed-point literals

A 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 

G.5.3.4. Mapping numeric literals to Java

Numeric literals are mapped by taking into account the context in which they are used. Typically a literal initializes a constant, and 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 = 2000; const float largeFloatVal = 2000; 

are mapped by Sun's idlj compiler to these Java declarations:

 // Java public static final short largeVal = (short) (2000); public static final float largeFloatVal = (float) (2000); 

Sun's idlj compiler does some type and range checking on the IDL literal before converting it to its Java form and inserting it into the cast operation shown previously. For example, if we change the first constant declaration to:

 // IDL const short largeVal = 2e5; 

the idlj compiler emits an error saying that the value is too large for a short variable.

G.5.4. Character Literals

A character literal is a character specification enclosed in single quotes (e.g., 'a'). Character literals can be specified only with 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 or the single- and double-quote characters that are used to delimit string and character literals . These characters are specified with escape sequences, which start with a backslash character. Table G-3 lists the escape sequences supported by IDL and the nonprintable characters they represent.

Table G-3. IDL escape sequences

Escape sequence

Meaning

\a

Alert

\\

Backslash

\b

Backspace

\r

Carriage return

\"

Double quote

\f

Formfeed

\x## (e.g., \x4e)

Hexadecimal number

\n

Newline

\### (e.g., \012)

Octal number

\?

Question mark

\'

Single quote

\t

Tab

\v

Vertical tab


Character literals, including the escape sequences listed in Table G-3, are converted unchanged into Java literals.

G.5.5. String Literals

A 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, then they are concatenated. So in this example:

 // IDL const string acctHolder = "Jim " "Farley"; 

the generated Java code is:

 // 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 G-3).



Java Enterprise in a Nutshell
Java Enterprise in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596101422
EAN: 2147483647
Year: 2004
Pages: 269

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net