- + - Addition. It may be used to add two string objects.
- * - Multiplication
- / - Division
- % - Modulus
- ++ - Increment
- -- - Decrement
- == - Equal
- != - Not equal
- < - Less than
- > - Greater than
- <= - Less than or equal to
- >= - Greater than or equal to
- << - Shift bits left by a specified amount.
- >> - Shift bits right by a specified amount.
- & - AND
- && - Logical (Bitwise) AND
- | - OR
- || - Logical (Bitwise) OR
- ^ - XOR
"Casting is the process of producing a new value that has a different type than its source. You don't change the value of a variable when it's cast. Instead, you create a new variable of the desired type. There are three forms of casts and conversions to talk about in this section:"
- "Casting between primitive types, such as int to float or float to double." Syntax:
"(int)(x / y); "
- "Casting from an instance of a class to an instance of another class. Instances of classes also can be cast to instances of other classes, with one restriction: The source and destination classes must be related by inheritance. One class must be a subclass of the other. " An instance of a subclass may be used where the superclass is expected without casting. The same works for instances of superclasses being used where a subclass is expected but there is a loss of precision. If the superclass is cast to the subclass cast using the syntax above, the methods of the subclass are then available.
- "Converting primitive types to objects and then extracting primitive values from those objects. One thing you can't do under any circumstance is cast from an object to a primitive data type, or vice versa. As an alternative, the java.lang package includes classes that correspond to each primitive data type: Integer, Float, boolean, and so on. Note that the class names have an initial capital letter and the primitive types have lowercase names. The following statement creates an instance of the Integer class with the value 4403:
Integer dataCount = new Integer(4403);
Once you have an object created in this manner, you can use it as you would any object. When you want to use that value again as a primitive value, there are methods for that as well. For example, if you wanted to get an int value from a dataCount object, the following statement would be used:
int newCount = dataCount.intValue(); // returns 4403
Another test that might be useful is the instanceof operator. instanceof has two operands: an object on the left and a class name on the right. The expression returns true or false based on whether the object is an instance of the named class or any of that class’s subclasses:
“swordfish” instanceof String // true
Point pt = new Point(10, 10);
pt instanceof String // false
The instanceof operator can also be used for interfaces; if an object implements an interface, the instanceof operator with that interface name on the right side returns true.