The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Language Basics

Other Operators

The following table lists the other operators that the Java programming language supports.

Operator Description
?: Shortcut if-else statement
[] Used to declare arrays, create arrays, and access array elements
. Used to form qualified names
( params ) Delimits a comma-separated list of parameters
( type ) Casts (converts) a value to the specified type
new Creates a new object or a new array
instanceof Determines whether its first operand is an instance of its second operand

Shortcut if-else Statement

The ?: operator is a conditional operator that is short-hand for an if-else statement:
op1 ? op2 : op3
The ?: operator returns op2 if op1 is true or returns op3 if op1 is false.

For information about the if-else statement, refer to The if/else Statements(in the Learning the Java Language trail).

The [ ] Operator

You use square brackets to declare arrays, to create arrays, and to access a particular element in an array. Here's an example of an array declaration:
float[] arrayOfFloats = new float[10];
The previous code declares an array that can hold ten floating point numbers. Here's how you would access the 7th item in that array:
arrayOfFloats[6];
Note that array indices begin at 0. Arrays(in the Learning the Java Language trail) contains more information about arrays.

The . Operator

The dot (.) operator accesses instance members of an object or class members of a class. You will learn more about this operator in the next lesson, Classes and Inheritance(in the Learning the Java Language trail).

The () Operator

When declaring or calling a method, you list the method's arguments between ( and ). You can specify an empty argument list by using () with nothing between them. The next lesson, Classes and Inheritance(in the Learning the Java Language trail), covers methods.

The (type) Operator

Casts (or "converts") a value to the specified type.

The new Operator

You use the new operator to create a new object or a new array. Here's an example of creating a new Integer object from the Integer class in the java.lang package:
Integer anInteger = new Integer(10);
For a detailed explanation about creating objects using a statement like the previous, refer to Creating Objects(in the Learning the Java Language trail) in the next lesson. To learn about creating arrays, refer to Arrays(in the Learning the Java Language trail).

The instanceof Operator

The instanceof operator tests whether its first operand is an instance of its second.
op1 instanceof op2
op1 must be the name of an object and op2 must be the name of a class. An object is considered to be an instance of a class if that object directly or indirectly descends from that class.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.