Arithmetic
Operators
Arithmetic operators are used in mathematical expressions in
the same way that they are used in algebra. The following table lists the
arithmetic operators:
Operator
Result
+ Addition
– Subtraction (also unary minus)
* Multiplication
/ Division
% Modulus
++ Increment
+= Addition assignment
–= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
– – Decrement
The operands of the arithmetic operators must be of a numeric
type. You cannot use them on boolean types, but you can use them on char types,
since the char type in Java is, essentially, a subset of int.
The Basic
Arithmetic Operators
The basic arithmetic operations—addition, subtraction,
multiplication, and division— all behave as you would expect for all numeric
types. The minus operator also has a unary form which negates its single
operand. Remember that when the division operator is applied to an integer
type, there will be no fractional component attached to the result. The
following simple example program demonstrates the arithmeticoperators. It also illustrates the
difference between floating-point division and integer division.
// Demonstrate the basic arithmetic operators.
class BasicMath {
public static void main(String args[]) {
// arithmetic using integers
System.out.println("Integer Arithmetic");
int a = 1 + 1;
int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);
// arithmetic using doubles
System.out.println("\nFloating Point Arithmetic");
double da = 1 + 1;
double db = da * 3;
double dc = db / 4;
double dd = dc - a;
double de = -dd;
System.out.println("da = " + da);
System.out.println("db = " + db);
System.out.println("dc = " + dc);
System.out.println("dd = " + dd);
System.out.println("de = " + de);
}
}
When you run this program, you will see the following output:
Integer Arithmetic
a = 2
b = 6
c = 1
d = -1
e = 1
Floating Point Arithmetic
da = 2.0
db = 6.0
dc = 1.5
dd = -0.5
de = 0.5
The Modulus
Operator
The modulus operator,%, returns the remainder of a division
operation. It can be applied to floating-point types as well as integer types.
(This differs from C/C++, in which the% can only be applied to integer types.)
The following example program demonstrates the %:
// Demonstrate the % operator.
class Modulus {
public static void main(String args[]) {
int x = 42;
double y = 42.25;
System.out.println("x mod 10 = " + x % 10);
System.out.println("y mod 10 = " + y % 10);
}
}
When you run this program you will get the following output:
x mod 10 = 2
y mod 10 = 2.25
Arithmetic
Assignment Operators
Java provides special operators that can be used to combine an
arithmetic operation with an assignment.
As you probably know, statements like the following are quite common in
programming:
a = a + 4;
The Bitwise
Operators
Java defines several bitwise operators which can be applied to
the integer types,long,int, short, char , and byte . These operators act upon
the individual bits of their operands.
They are summarized in the following table:
Operator
Result
~ Bitwise unary NOT
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
>> Shift right
>>> Shift right zero fill
<< Shift left
&= Bitwise AND assignment
|= Bitwise OR assignment
Operator Result
^= Bitwise exclusive OR assignment
>>= Shift right assignment
>>>= Shift right zero fill assignment
<<= Shift left assignment
The Bitwise
Logical Operators
The bitwise logical operators are &, |, ^, and ~. The
following table shows the outcome of each operation. In the discussion that
follows, keep in mind that the bitwise operators are applied to each individual
bit within each operand.
AB A | B A & B A ^ B ~A
00 0 0 0 1
10 1 0 1 0
01 1 0 1 1
11 1 1 0 0
The Bitwise
NOT
Also called the bitwise complement,the unary NOT operator,~,
inverts all of the bits of its operand. For example, the number 42, which has
the following bit pattern:
00101010
becomes
11010101
after the NOT operator is applied.
The Bitwise AND
The AND operator, &, produces a 1 bit if both operands are
also 1. A zero is produced
in all other cases. Here is an example:
00101010 42
&00001111 15
--------------
00001010 10
The Bitwise
OR
The OR operator, |, combines bits such that if either of the
bits in the operands is a 1,then the resultant bit is a 1, as shown here:
00101010 42
| 00001111 15
--------------
00101111 47
The Bitwise
XOR
The XOR operator,^, combines bits such that if exactly one
operand is 1, then the result is 1. Otherwise, the result is zero. The
following example shows the effect of the ^. This example also demonstrates a
useful attribute of the XOR operation. Notice how the bit pattern of 42 is inverted
wherever the second operand has a 1 bit. Wherever the second operand has a 0
bit, the first operand is unchanged. You will find this property useful when
performing some types of bit manipulations.
00101010 42
^00001111 15
-------------
00100101 37
Relational
Operators
The relational operators determine the relationship that one
operand has to the other.Specifically, they determine equality and ordering.
The relational operators are shown here:
Operator
Result
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Boolean
Logical Operators
The Boolean logical operators shown here operate only on
boolean operands. All of the binary logical operators combine twoboolean values
to form a resultant boolean value.
Operator
Result
& Logical AND
| Logical OR
^ Logical XOR (exclusive OR)
|| Short-circuit OR
&& Short-circuit AND
! Logical unary NOT
&= AND assignment
|= OR assignment
^= XOR assignment
== Equal to
!= Not equal to
?: Ternary if-then-else
No comments:
Post a Comment