Table of ContentsObjects EverywhereStrings

Basic Syntax

You've taken a good look at objects and their importance in the Java landscape, but you're actually getting ahead of yourself. When it comes down to it, you need to follow the rules of the language (or the syntax) to code in Java. In the next few sections, you'll take a quick tour of the basic syntax that makes up the language.

Comments

Java has two methods for adding comments to your code. The simplest method uses the // token to tell the compiler to ignore the rest of the line. Here's an example you've seen before:

Ship myShip;
myShip.move(100, 200);   // ERROR! Object not constructed.

Alternatively, you can use a block comment, using the /* and */ tokens. This comment type can span multiple lines. For example:

/*
Ship myShip;
myShip.move(100, 200);  // ERROR! Object not constructed.
*/

Note that you cannot nest block comments; therefore, the following code is illegal:

/*
Ship myShip;
myShip.move(100, 200);   /* error!!! */
*/

Primitive Types

As shown in Table A.1, Java gives you a nice selection of primitive types from which to choose. Unlike many other languages, Java guarantees these primitive types to be the same size across any operating system. Therefore, you can safely assume an integer type will always use two bytes, no matter where it runs.

Table Table A.1. Primitive Types

Type

Description

Size

Range

Min/Max Values

boolean

Boolean value (true or false)

N/A

N/A

True or false

byte

Single-byte integer

8-bit (1 byte)

(27) to 271

127 to 128

char

Unicode character

16-bit (2 bytes)

0 to 2161

0 to 65535

short

Short integer

16-bit (2 bytes)

0 to 2161

32768 to 32767

int

Regular integer

32-bit (4 bytes)

(231) to 2311

2,147,483,648 to 2,147,483,467

long

Long integer

64-bit (8 bytes)

(263) to 2631

9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

float

Single-precision floating point

32-bit (4 bytes)

(263) to 2631

double

Double-precision floating point

64-bit (8 bytes)

(263) to 2631


As you might have noticed in Table A.1, there are no unsigned typesthey just don't exist in Java.

Literals

In Java there are six types of literals availableintegers, longs, floats, characters, strings, and booleans. You can use a literal, also known as a constant, to create a value directly inside your code. I'll start with the simplest literalinteger.

An integer literal represents an integer value in decimal, hexadecimal (base 16), or octal (base 8). You can use a decimal constant simply by placing the integer value in your code. For an octal value, place a 0 before the number; for a hexadecimal number, use 0x or 0X. Here's all three in action; note that all of these statements are equivalent:

int a = 100;       // decimal
int a = 0144;      // octal
int a = 0x64;      // hexidecimal

To use a long literal, place an L after the number. For example, the following are all valid long numerical constants:

long a = 100L;
long a = 1234L;
long a = 0xBA12L;

You can use a floating-point literal in a similar fashion by placing an F after the number instead of an L.You can also use the constant E for an exponent and D for a double-precision number. Here are some examples:

float a = 100F;
float a = 1.23F;
float a = 1234.5678e+22F;

NOTE

Note

CLDC 1.0 (the most common J2ME profile) does not support floating point values.

Character literals let you enter a character value directly into your code by placing it between single quotes. For example:

char c = 'a';

Table A.2 lists some special character literals available in Java. To use a special character, you must precede it with a backslash (\).

Table Table A.2. Special Characters

Character

Use

\n

Newline

\t

Tab

\b

Backspace

\r

Return

\f

Form feed

\ddd

Octal value

\'

Single quote

\"

Double quote

\\

Backslash


A string literal is very similar to a character literal, except you can place multiple characters between double quotes. For example:

String hi = "Hello there";

You can place any of the Java special characters inside a string.

String hi = "\tHello\n\"there\"";
System.out.println(hi);

That code would produce the following output:

    Hello
"There"

Finally, boolean literals are available in the form of true and false.You can use these directly in your code to assign a value. For example:

boolean a = true;

Declaring Primitive Types

Now that you know what primitive types are available, take a look at how to use them. To declare a variable using one of these types, use the keyword corresponding to that type, followed by an identifier for your new variable. You can use any combination of letters or numbers (as well as the underscore character) as an identifier; however, you can't start it with a number. Obviously, you also can't use reserved words or visible method names as identifiers.

NOTE

Tip

Use identifiers (variable names) to provide a clear indication to the intent of the variable, even if it makes the name a little long. For example, use speed for the speed of a vehicle, rather than just s. You'll find that in the long run, your code is more organized and easier to understand (especially after you haven't looked at it for six months). The same tip applies to method and class names.

There's no real performance or size penalty for using full-length names. Although they do occupy more space in a class file, the process of obfuscating replaces long names with optimized ones. So leave obfuscation to the obfuscator.

Here are some examples of some different types of declarations:

int a;
long a;
float a;
boolean a;

The compiler will automatically assign a default value based on the variable's primitive type. For numerical values this will be 0; the boolean type defaults to false.

If you're not satisfied with the default values (damn, you're picky), you can provide an initial value using the assignment operator and a constant matching the type. (I'll talk more about assignment a little later.) For example:

int a = 100;
long a = 100l;
float a = 100f;
boolean a = true;

Also keep in mind that identifiers are case-sensitive, so a and A can refer to different variables.

Basic Operators

There are four types of basic operators available in Javaassignment, arithmetic, unary, and conditional. In the next few sections, you'll take a closer look at each of these operator types.

Assignment Operators

You've seen the most basic assignment operator, equals, used in quite a few examples already. For primitive types, equals does a by-value assignment of one variable to another. For example:

int a = 1;
int b = a;
a = b;

This code results in both a and b being equal to 1. For object types, the operator will assign the reference to the object, rather than to a copy of the object.

You can also chain equals assignment calls. For example, the following code assigns the value of 1 to all four variables:

a = b = c = d = 1;

Java also has a series of convenience assignment operators (+=, -=, *=, and /=) that do an assignment and a mathematical operation simultaneously. For example:

int a = 2;
int b = 4;
a += 2;   // a now 4
b *= 2;   // b now 8 

Arithmetic and Unary Operators

The Java arithmetic and unary operators (+, -, *, /, and %) let you carry out basic mathematical operations on your variables. You can also chain these operators. For example:

int a = 1 + 2 + 3;   // a total of 6

The typical order of precedence also applies; therefore addition (+) and subtraction (-) have a lower precedence than multiplication (*), division (/), and mod (%). Thus 2 + 3 * 5 equates to 17, not 25. You can modify this order of precedence using brackets. For example, (2 + 3) * 5 equates to 25.

A unary operator lets you carry out an operation on a single element. The simplest of these operators are plus (+) and minus (-), which invert a number. For example:

int a = -2;
int b = -a;       // b equals 2

The other unary operators let you easily increment (++) and decrement (--) a number. When you are using these operators within a larger statement, you can use the placement of the operator to specify whether to carry out the increment or decrement before or after the next statement. For example:

int a = 0;
if (a++ > 0)
    System.out.println("a=" + a);

In this case, there will be no output because the increment occurs after the statement evaluation. However, the following code will produce output:

int a = 0;
if (++a > 0)
    System.out.println("a=" + a);

Before I move on, there's one other operator you should take note ofthe type caster. Sometimes you need to use this operator to give the compiler a little slap in the head. Take a look at an example, and then I'll explain what's happening in it.

int a = 10;
int b = 4;
float c = a / b;
    System.out.println("c=" + c);

Now, you know that 10 divided by 4 is 2.5. Unfortunately, if you run this code the output will be 2.0. Confused? Here's what's happening. The divide-by statement involves two integers, so the result has to be another integer. Therefore, before you end up with the float result (c), a temporary variable is created and assigned to the integer value. This value is later implicitly cast into the final float value for the result. With me so far? The problem is that implicit conversion to an integer results in the loss of the floating-point portion of the result. For clarity, you can imagine that the statement actually reads:

float c = (int) a / b;

To get around the problem, you use the type cast operator to force the result type of the operation (the operator is the "(float)" in brackets). For example:

float c = (float) a / b;

Bit-Manipulation Operators

Java provides the operators you need to practice the black art of bit manipulationAND (&), inclusive OR (|), exclusive OR (^), shift left (<<), shift right (>>), zero shift right (>>>), and complement (~). You can only carry out bitwise operations on byte, char, int, long, or short types.

The bitwise AND operator generates an integer result by comparing each of the bits of two integers to each other. If both compared bits are 1, the result for that bit position will be 1; otherwise, the result will be a 0 in that position. Bitwise inclusive OR is similar except that the result is 0 only if both bits in the original values are 0; otherwise, the result is 1. Similarly, an exclusive OR requires that the bits be different (one equal to 0 and one equal to 1).

The three bitwise shift operators let you move (or shift) the bits left or right by a certain number of spaces. For example, the following code shifts the bits representing the value binary 1 (00000001) four positions to the left, placing zeros in the new positions. The result is binary 16 (00010000).

int i = 1 << 4;

The bitwise complement operator simply toggles the bits in any number from 0 to 1 and vice versa.

Expression Operators

The final set of operators is a little different; you use them as part of an expression, usually relating to a condition in an if, for, while or other statement. The first operators, known as equality operators, are == and !=.These operators let you compare two expressions as equal or not equal. The result is a boolean (true or false). For example:

boolean b1 = 10 == 10;     // true
boolean b2 = 10 != 10;     // false

The result from this example will be that b1 is equal to true and b2 is equal to false.

You can also use relational expression operators (>, <, >=, and <=) in a similar way to compare two values. For example:

boolean b1 = 10 > 10;     // false
boolean b2 = 10 >= 10;    // true
boolean b3 = 5 < 10;      // true
boolean b4 = 5 <= 4;      // false

The final set of expression operators lets you carry out the Boolean operations AND (&&), OR (||), and NOT (!). These nifty little buggers (also known as logical operators) let you join two expressions, resulting in the combination of the two and carrying out a Boolean evaluation in the process. For example:

boolean b1 = 15 > 10 && 5 == 5;   // true
boolean b2 = 10 > 10 || 5 == 5;   // true

In the first case, the use of an AND operator means that both expressions must evaluate to true for the result to be true. The second statement uses OR, so only one of the statements needs to be true for the result to be true.

Take a look at another example, but this time something weird is going to happen.

int i=0;
boolean b2 = i == 1 || i++ > 0;

The problem here is that in Java, an expression evaluation will short-circuit as soon as the result is certain. So in this example, you'll never see the i++ executed because the i == 1 will always fail. (Since the statement was an OR, the failure of the first condition means the entire expression will also fail, so there's no point executing the other conditions.) Avoid relying on the execution of code within potentially short-circuited evaluations.

Before you move on, there's one other logical operator ...NOT.(No,that's not a joke; there really is a NOT operator.) This one is a little special; it basically inverts the Boolean result of any singular expression. Here's an example:

boolean b1 = !(15 > 10)        // false (true inverted)

Statements and Blocks

Before you look at some of the other syntax, I'd like to clear up what I mean by the term statement. Like in C, a statement can contain multiple code elements separated by a semicolon (;). Here's an example of four valid statements:

int a = 0;
a = a + 1;;
int b = a;

Notice I said four statements, even though there are only three lines. That's because the second line actually contains two statements. (Notice the extra semicolon; this is considered to be an empty statement, and it is quite valid.)

You can group statements into a block simply by enclosing the code in braces ({ and }). For example:

{
    int a = 0;
    a = a + 1;
    int b = a;
}

A block can appear inside another block, thus creating a nested block.

{
    {
        int a = 0;
        a = a + 1;
        b = a;
    }

    int b = 0;
    b++;
}

Nesting blocks have another effect of which you should be aware. Any declarations (classes, methods, or variables) made within a block are not visible to any outer blocks. Because of this, you might sometimes need to adjust where you declare a variable to ensure that it's within the correct scope. Here's an example of an incorrect placement:

{
    while( true )
    {
        int i;       // oops, declared in the loop block
        i++;
        if (i > 10) break;
    }
}

Unfortunately, this while loop will spin forever because the variable i is re-declared on every pass. The correct code has the variable declaration outside of the while loop block.

{
    int i;       // that's more like it!

    while( true )
    {
        i++;
        if (i > 10) break;
    }
}

Conditionals

Conditional statements in Java let you control the flow of execution of your code. These statements include if, while, do, for, switch, and the ternary operator. I will start with the simplestif.

The if Statement

The if conditional statement tests whether the result of an evaluation is true. This is where those expression operators really kick in. Here's an example:

if (10 > 15)
    System.out.println("True!");
System.out.println("Hello");

Because if only controls the following statement or block, the output of the preceding code is "Hello," not "True!" (because 10 is never greater than 15). I've indented the code to show which statements are subject to this condition. Keep in mind that this indenting has nothing to do with what's actually going on; it's just used to make the code clearer.

You can use any expression inside the brackets, including a joined one; the result can then be any value statement or block. For example:

if (10 > 15 && 10 > 9)
{
    System.out.println("One good statement...");
    System.out.println("deserves another.");
}

NOTE

Caution

A common mistake for new coders is to place a semicolon at the end of a conditional statement. For example:

        if (10 > 15);        // oops
            System.out.println("True!");

If you were to execute this code, the string "True!" will always be output, which is not the intention of the code. The culprit is the extra semicolon at the end of the if line. The compiler treats this as an empty statement by the compiler; therefore, the result of the if is to execute an empty statement (which is the same as doing nothing). Execution then carries on to the next statement (println) as normal.

Optionally, you can use an else following the if statement to execute code if the expression result was false. Here's another example:

if (10 > 15)
    System.out.println("True!");
else
    System.out.println("False!");

The do and while Statements

The while statement is much cooler than the if statement.It lets you execute the same statement or block multiple times by a process known as conditional looping.Think of it like an if statement that keeps executing until the expression result is false. For example:

while (2 > 1)
    System.out.println("True!");

Of course, this code is not a good idea because the condition never evaluates to false. You'll just keep seeing an endless stream of "True!" (which is not my idea of fun). Here's a better, far more typical example:

int i = 0;
while (i < 10)
{
    System.out.println("i=" + i);
    i++;
}

This condition will output a limited number of strings before falling through to the next statement.

The do conditional is very similar to while; it just moves the condition to the end of the statement. For example:

int i = 0;
do
{
    System.out.println("i=" + i);
    i++;
} while (i < 10)

You primarily use the do statement when you want to execute a conditioned statement at least once. It is rarely used, however, because the for conditional is a more robust solution.

The for Statement

Java provides a more advanced conditionalthe for statement. The nice thing about using for is that it can initialize, iterate, and test for an ending condition all in a single line. I've rewritten the while loop from the previous example using the more succinct for loop.

for (int i=0; i < 10; i++)
    System.out.println("i=" + i);

Nice,huh? As you can see, for lets you provide three simple statements. (No, you can't put blocks in there.) The initializer is executed before the loop begins; the conditional is executed before each iteration through the loop to test whether the loop is complete; and an update statement is executed after each loop through but before the next condition test.

The switch Statement

The switch condition is for when you have many integer comparison cases. Using switch can dramatically reduce the required code for this type of operation. For example, here's the code to compare four integer values using if statements:

if (i == 1)
    System.out.println("i=1");
if (i == 2)
    System.out.println("i=2");
if (i == 3)
    System.out.println("i=3");
if (i == 4)
    System.out.println("i=4");

Here's the equivalent code using a switch statement:

switch(i)
{
    case 1:
        System.out.println("i=1");
        break;
    case 2:
        System.out.println("i=2");
        break;
    case 3:
        System.out.println("i=3");
        break;
    case 4:
        System.out.println("i=4");
        break;
}

Notice that at the end of each case line, I've included the keyword break. This is one of the nice things about a switch statement; if you leave the break out of the code, the next case is also executed. For example:

switch(i)
{
    case 1:
        System.out.println("Got one");
        break;
    case 2:
    case 3:
    case 4:
        System.out.println("i is 2, 3 or 4");
        break;
    default:
        System.out.println("i is something else");
}

You can also use the default keyword in a switch statement. That code is executed if no case matches.

The Ternary Operator

The ternary operator (?:) is a shortcut for evaluating a Boolean expression. Depending on the outcome, it will return one of two different results. For example:

int i=5;
String result = "Your number is " +
                ( (i <= 5) ? "less than or equal to" : "greater than") +
                " 5";

This is effectively the same as doing:

int i=5;
String result = "Your number is ";
if (i <= 5)
    result += "less than or equal to";
else
    result += "greater than";

result += " 5";

As you can see, the ternary operator lets you wrap up a conditional inside another statement.

The break keyword

As you briefly saw with the switch statement, the break keyword lets you exit a current block. Here's an example of a break used to exit an endless while loop:

int i = 0;
while(true)
{
    if (i > 10)
        break; // jump to exit point
    i++;
} // break exit point

You can also use break in conjunction with label much like a traditional goto statement. You can place a label almost anywhere, and then directly jump to it using the break statement. For example:

start_again:
int i = 0;
while(true)
{
    if (i > 10)
        break start_again;
    i++;
}

Unfortunately, your code is back to endlessly looping. As soon as the case to break is executed, you jump back up outside of the while statement and then execution starts all over again. I'll leave you to fix that.

    Table of ContentsObjects EverywhereStrings