Java Operators

As in JavaScript, operators are an important part of programming in Java. Here's an example adding two values using the Java + operator:

public class app
{
    public static void main(String[] args)
    {
        int int1 = 130, int2 = 250, sum;

        sum = int1 + int2;

        System.out.println(int1 + " + " + int2 +
            " = " + sum);
    }
}

Here are the results of this code:

%java app
130 + 250 = 380

So what operators are available in Java? Table 10.1 contains all of them—note that JavaScript shares nearly all of them as well.

Table 10.1. Java Operators
OperatorOperation Performed
++Increment
--Decrement
=Assignment
==Equal to
+Addition
+=Addition assignment
-Subtraction
-=Subtraction assignment
*Multiplication
*=Multiplication assignment
/Division
/=Division assignment
<Less than
<=Less than or equal to
<<Shift left
<<=Shift left assignment
>Greater than
>=Greater than or equal to
>>Shift right
>>=Shift right assignment
>>>Shift right with zero fill
>>>=Shift right zero fill assignment
^Logical Xor
^=Bitwise Xor assignment
|Logical Or
||Short-circuit Or
|=Bitwise Or assignment
~Bitwise unary Not
!Logical unary Not
!=Not equal to
&Logical And
&&Short-circuit And
&=Bitwise And assignment
?:Ternary if…else
%Modulus
%=Modulus assignment

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.104.72