Java programming-Operators
Arithmatic Operators-Used in mathematical expression-Operands must be numeric//demonstration of Arithmatic Operatorsclass BasicMath {public static void main(String args[]) {// arithmetic using integersSystem.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);}}Increment or DecrementIncrement operator increases by 1Decrement operator decreases by 1Bitwise OperatorsCan be applied to integer typesLong,int,short,char and byteOperator 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^= Bitwise exclusive OR assignment>>= Shift right assignment>>>= Shift right zero fill assignment<<= Shift left assignment Bitwise Logical Operators The bitwise logical operators are &, |, ^, and ~. The following table shows the outcomeof each operation. In the discussion that follows, keep in mind that the bitwise operatorsare applied to each individual bit within each operand.A B A | B A & B A ^ B ~A0 0 0 0 0 11 0 1 0 1 00 1 1 0 1 11 1 1 1 0 0The Bitwise NOTAlso called the bitwise complement, the unary NOT operator, ~, inverts all of the bits ofits operand. For example, the number 42, which has the following bit pattern:00101010becomes11010101after the NOT operator is applied.The Bitwise ANDThe AND operator, &, produces a 1 bit if both operands are also 1. A zero is producedin all other cases. Here is an example:00101010 42&00001111 15--------------00001010 10The Bitwise ORThe 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 47THE JAVA LANGUAGEThe Bitwise XORThe XOR operator, ^, combines bits such that if exactly one operand is 1, then the resultis 1. Otherwise, the result is zero. The following example shows the effect of the ^. Thisexample also demonstrates a useful attribute of the XOR operation. Notice how the bitpattern of 42 is inverted wherever the second operand has a 1 bit. Wherever the secondoperand has a 0 bit, the first operand is unchanged. You will find this property usefulwhen performing some types of bit manipulations.00101010 42^00001111 15-------------00100101 37Using the Bitwise Logical OperatorsThe following program demonstrates the bitwise logical operators:// Demonstrate the bitwise logical operators.class BitLogic {public static void main(String args[]) {String binary[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111","1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};int a = 3; // 0 + 2 + 1 or 0011 in binaryint b = 6; // 4 + 2 + 0 or 0110 in binaryint c = a | b;int d = a & b;int e = a ^ b;int f = (~a & b) | (a & ~b);int g = ~a & 0x0f;System.out.println(" a = " + binary[a]);System.out.println(" b = " + binary[b]);System.out.println(" a|b = " + binary[c]);System.out.println(" a&b = " + binary[d]);System.out.println(" a^b = " + binary[e]);System.out.println("~a&b|a&~b = " + binary[f]);System.out.println(" ~a = " + binary[g]);}}Output:- Here is the output from this program:a = 0011b = 0110a|b = 0111a&b = 0010a^b = 0101~a&b|a&~b = 0101~a = 1100Relational OperatorsThe relational operators determine the relationship that one operand has to the other.Specifically, they determine equality and ordering. The relational operators areshown here:Operator Result== Equal to!= Not equal to> Greater than< Less than>= Greater than or equal to<=Less than or equal toBoolean Logical OperatorsThe Boolean logical operators shown here operate only on boolean operands. Allof the binary logical operators combine two boolean values to form a resultantboolean 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-elseThe Assignment OperatorThe assignment operator is the single equal sign, =. The assignmentoperator works in Java much as it does in any other computer language. It has thisgeneral form:var = expression;Here, the type of var must be compatible with the type of expression.The ? OperatorJava includes a special ternary (three-way) operator that can replace certain types ofif-then-else statements. This operator is the ?, and it works in Java much like it doesin C, C++, and C#. It can seem somewhat confusing at first, but the ? can be used veryeffectively once mastered. The ? has this general form:expression1 ? expression2 : expression3// Demonstrate ?.class Ternary {public static void main(String args[]) {int i, k;i = 10;k = i < 0 ? -i : i; // get absolute value of iSystem.out.print("Absolute value of ");System.out.println(i + " is " + k);i = -10;k = i < 0 ? -i : i; // get absolute value of iSystem.out.print("Absolute value of ");System.out.println(i + " is " + k);}}Operator precedencePrecedence of the Java OperatorsHighest( ) [ ] .++ – – ~ !* / %+ –>> >>> <<> >= < <=== !=&^|&&||?:= op=Lowest
Description
An operator is a symbol that operates on one or more arguments to produce a result. The Hello World program is so simple it doesn't use any operators, but almost all other programs you write will.
Operator Purpose
+ addition of numbers, concatenation of Strings
+= add and assign numbers, concatenate and assign Strings
- subtraction
-= subtract and assign
* multiplication
*= multiply and assign
/ division
/= divide and assign
% take remainder
%= take remainder and assign
++ increment by one
-- decrement by one
> greater than
>= greater than or equal to
< less than
<= less than or equal to
! boolean NOT
!= not equal to
&& boolean AND
|| boolean OR
== boolean equals
= assignment
~ bitwise NOT
?: conditional
instanceof type checking
| bitwise OR
|= bitwise OR and assign
^ bitwise XOR
^= bitwise XOR and assign
& bitwise AND
&= bitwise AND and assign
>> shift bits right with sign extension
>>= shift bits right with sign extension and assign
<< shift bits left
<<= shift bits left and assign
>>> unsigned bit shift right
>>>= unsigned bit shift right and assign
Presentation Transcript
Your Facebook Friends on WizIQ