The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: Learning the Java Language
Lesson: Language Basics

## Other Operators

### 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.

### 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 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, Interfaces, and Packages.

### 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, Interfaces, and Packages, covers methods.

### The (type) Operator

> [PENDING: cover casting (implicit as well as explicit) here]

### 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 next lesson. To learn about creating arrays, refer to Arrays.

### 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.

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form