|
|
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-elsestatement[]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 newCreates a new object or a new array instanceofDetermines 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 anif-elsestatement:Theop1 ? op2 : op3?:operator returnsop2ifop1is true or returnsop3ifop1is false.For information about the
if-elsestatement, 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: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:float[] arrayOfFloats = new float[10];Note that array indices begin at 0. ArraysarrayOfFloats[6];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.
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, 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 newIntegerobject from theIntegerclass in thejava.langpackage:For a detailed explanation about creating objects using a statement like the previous, refer to Creating ObjectsInteger anInteger = new Integer(10);in the next lesson. To learn about creating arrays, refer to Arrays
.
The instanceof Operator
Theinstanceofoperator tests whether its first operand is an instance of its second.op1 instanceof op2op1must be the name of an object andop2must 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 |
