Monday, November 3, 2008

More on Literals & Variables

Escape Sequence Description

 

\ddd                Octal character (ddd)

\uxxxx Hexadecimal UNICODE character (xxxx)

\’                      Single quote

\”                      Double quote

\\                      Backslash

\r                      Carriage return

\n                     New line (also known as line feed)

\f                      Form feed

\t                      Tab

\b                     Backspace

 

 

String Literals

String literals in Java are specified like they are in most other languages—by enclosing a sequence of characters between a pair of double quotes.

 

Examples of string literals are

 

“Hello World”

“two\nlines”

“\”This is in quotes\””

 

The escape sequences and octal/hexadecimal notations that were defined for character literals work the same way inside of string literals. One important thing to note about Java strings is that they must begin and end on the same line. There is no line-continuation escape sequence as there is in other languages.

 

As you may know, in some other languages, including C/C++, strings are implemented as arrays of characters. However, this is not the case in Java. Strings are actually object types. As you will see later in this book, because Java implements strings as objects, Java includes extensive string-handling capabilities that are both powerful and easy to use.

 

Variables

The variable is the basic unit of storage in a Java program. A variable is defined by the combination of an identifier, a type, and an optional initializer. In addition, all variables have a scope, which defines their visibility, and a lifetime. These elements are examined next.

 

Declaring a Variable

 

In Java, all variables must be declared before they can be used. The basic form of a variable declaration is shown here:

 

type identifier [ = value][, identifier [= value] ...] ;

 

The type is one of Java’s atomic types, or the name of a class or interface. (Class and interface types are discussed later in Part I of this book.) The identifier is the name of the variable. You can initialize the variable by specifying an equal sign and a value. Keep in mind that the initialization expression must result in a value of the same (or compatible) type as that specified for the variable. To declare more than one variable of the specified type, use a comma-separated list.

THE

Here are several examples of variable declarations of various types. Note that some include an initialization.

 

int a, b, c;             // declares three ints, a, b, and c.

int d = 3, e, f = 5;    // declares three more ints,  

                       //initializing d and f.

byte z = 22;          // initializes z.

double pi = 3.14159; // declares an approximation of pi.

char x = 'x';       // the variable x has the value 'x'.

 

The identifiers that you choose have nothing intrinsic in their names that indicates their type. Many readers will remember when FORTRAN predefined all identifiers from I through N to be of type INTEGER while all other identifiers were REAL. Java allows any properly formed identifier to have any declared type.

 

 

Dynamic Initialization

 

Although the preceding examples have used only constants as initializers, Java allows variables to be initialized dynamically, using any expression valid at the time the variable is declared.

 

For example, here is a short program that computes the length of the hypotenuse of a right triangle given the lengths of its two opposing sides:

 

// Demonstrate dynamic initialization.

class DynInit {

public static void main(String args[]) {

double a = 3.0, b = 4.0;

// c is dynamically initialized

double c = Math.sqrt(a * a + b * b);

System.out.println("Hypotenuse is " + c);

}

}

 

Here, three local variables—a, b,and c—are declared. The first two, a and b, are initialized by constants. However, c is initialized dynamically to the length of the hypotenuse (using the Pythagorean theorem). The program uses another of Java’s built-in methods, sqrt( ), which is a member of the Math class, to compute the square root of its argument. The key point here is that the initialization expression may use any element valid at the time of the initialization, including calls to methods, other variables, or literals.

 

No comments: