JAVA OPERATORS
They are used to manipulate primitive data types. Java operators can be classified as unary, binary, or ternary—meaning taking one, two, or three arguments, respectively. A unary operator may appear
before (prefix) its argument or after (postfix) its argument. A binary or ternary operator appears between its arguments.
before (prefix) its argument or after (postfix) its argument. A binary or ternary operator appears between its arguments.
Operators in java fall into 8 different categories:
Java operators fall into eight different categories: assignment, arithmetic, relational, logical, bitwise, compound assignment, conditional, and type.
Assignment Operators = Arithmetic Operators - + * / % ++ -- Relational Operators > < >= <= == != Logical Operators && || & | ! ^ Bit wise Operator & | ^ >> >>> Compound Assignment Operators += -= *= /= %= <<= >>= >>>= Conditional Operator ?:
Java has eight different operator types: assignment, arithmetic, relational, logical, bitwise, compound assignment, conditional, and type.
ASSIGNMENT OPERATORS
The java assignment operator statement has the following syntax:
variable> = <expression>
If the value already exists in the variable it is overwritten by the assignment operator (=).
public class AssignmentOperatorsDemo {
public AssignmentOperatorsDemo() {
// Assigning Primitive Values
int j, k;
j = 10; // j gets the value 10.
j = 5; // j gets the value 5. Previous value is overwritten.
k = j; // k gets the value 5.
System.out.println("j is : " + j);
System.out.println("k is : " + k);
// Assigning References
Integer i1 = new Integer("1");
Integer i2 = new Integer("2");
System.out.println("i1 is : " + i1);
System.out.println("i2 is : " + i2);
i1 = i2;
System.out.println("i1 is : " + i1);
System.out.println("i2 is : " + i2);
// Multiple Assignments
k = j = 10; // (k = (j = 10))
System.out.println("j is : " + j);
System.out.println("k is : " + k);
}
public static void main(String args[]) {
new AssignmentOperatorsDemo();
}
}
ARITHMETIC OPERATORS
Java provides eight Arithmetic operators. They are for addition, subtraction, multiplication, division, modulo (or remainder), increment (or add 1), decrement (or subtract 1), and negation. An example program is shown below that demonstrates the different arithmetic operators in java.
The binary operator + is overloaded in the sense that the operation performed is determined by the type of the operands. When one of the operands is a String object, the other operand is implicitly converted to its string representation and string concatenation is performed.
String message = 100 + “Messages”; //”100 Messages”
public class ArithmeticOperatorsDemo {
public ArithmeticOperatorsDemo() {
int x, y = 10, z = 5;
x = y + z;
System.out.println("+ operator resulted in " + x);
x = y - z;
System.out.println("- operator resulted in " + x);
x = y * z;
System.out.println("* operator resulted in " + x);
x = y / z;
System.out.println("/ operator resulted in " + x);
x = y % z;
System.out.println("% operator resulted in " + x);
x = y++;
System.out.println("Postfix ++ operator resulted in " + x);
x = ++z;
System.out.println("Prefix ++ operator resulted in " + x);
x = -y;
System.out.println("Unary operator resulted in " + x);
// Some examples of special Cases
int tooBig = Integer.MAX_VALUE + 1; // -2147483648 which is
// Integer.MIN_VALUE.
int tooSmall = Integer.MIN_VALUE - 1; // 2147483647 which is
// Integer.MAX_VALUE.
System.out.println("tooBig becomes " + tooBig);
System.out.println("tooSmall becomes " + tooSmall);
System.out.println(4.0 / 0.0); // Prints: Infinity
System.out.println(-4.0 / 0.0); // Prints: -Infinity
System.out.println(0.0 / 0.0); // Prints: NaN
double d1 = 12 / 8; // result: 1 by integer division.
// d1 gets the value 1.0.
double d2 = 12.0F / 8; // result: 1.5
System.out.println("d1 is " + d1);
System.out.println("d2 iss " + d2);
}
public static void main(String args[]) {
new ArithmeticOperatorsDemo();
}
}
Download ArithmeticOperatorsDemo Source code
RELATIONAL OPERATORS
Relational operators in Java are used to compare 2 or more objects. Java provides six relational operators:
greater than (>), less than (<), greater than or equal (>=), less than or equal (<=), equal (==), and not equal (!=).
All relational operators are binary operators, and their operands are numeric expressions.
Binary numeric promotion is applied to the operands of these operators. The evaluation results in a boolean value. Relational operators have precedence lower than arithmetic operators, but higher than that of the assignment operators. An example program is shown below that demonstrates the different relational operators in java.
public class RelationalOperatorsDemo { public RelationalOperatorsDemo( ) { int x = 10, y = 5; System.out.println("x > y : "+(x > y)); System.out.println("x < y : "+(x < y)); System.out.println("x >= y : "+(x >= y)); System.out.println("x <= y : "+(x <= y)); System.out.println("x == y : "+(x == y)); System.out.println("x != y : "+(x != y)); } public static void main(String args[]){ new RelationalOperatorsDemo(); } }
Download RelationalOperatorsDemo Source code
LOGICAL OPERATORS
Logical operators return a true or false value based on the state of the Variables. There are six logical, or boolean, operators. They are AND, conditional AND, OR, conditional OR, exclusive OR, and NOT. Each argument to a logical operator must be a boolean data type, and the result is always a boolean data type. An example program is shown below that demonstrates the different Logical operators in java.
public class LogicalOperatorsDemo {
public LogicalOperatorsDemo() {
boolean x = true;
boolean y = false;
System.out.println("x & y : " + (x & y));
System.out.println("x && y : " + (x && y));
System.out.println("x | y : " + (x | y));
System.out.println("x || y: " + (x || y));
System.out.println("x ^ y : " + (x ^ y));
System.out.println("!x : " + (!x));
}
public static void main(String args[]) {
new LogicalOperatorsDemo();
}
}
Download LogicalOperatorsDemo Source code
Given that x and y represent boolean expressions, the boolean logical operators are defined in the Table below.
BITWISE OPERATORS
Java provides Bit wise operators to manipulate the contents of variables at the bit level.
These variables must be of numeric data type ( char, short, int, or long). Java provides seven bitwise
operators. They are AND, OR, Exclusive-OR, Complement, Left-shift, Signed Right-shift, and Unsigned Right-shift. An example program is shown below that demonstrates the different Bit wise operators in java.
public class BitwiseOperatorsDemo { public BitwiseOperatorsDemo() { int x = 0xFAEF; //1 1 1 1 1 0 1 0 1 1 1 0 1 1 1 1 int y = 0xF8E9; //1 1 1 1 1 0 0 0 1 1 1 0 1 0 0 1 int z; System.out.println("x & y : " + (x & y)); System.out.println("x | y : " + (x | y)); System.out.println("x ^ y : " + (x ^ y)); System.out.println("~x : " + (~x)); System.out.println("x << y : " + (x << y)); System.out.println("x >> y : " + (x >> y)); System.out.println("x >>> y : " + (x >>> y)); //There is no unsigned left shift operator } public static void main(String args[]) new BitwiseOperatorsDemo(); }
The result of applying bitwise operators between two corresponding bits in the operands is shown in the Table below.
Output
/*
* The below program demonstrates bitwise operators keeping in mind operator precedence
* Operator Precedence starting with the highest is -> |, ^, &
*/
public class BitwisePrecedenceEx {
public static void main(String[] args) {
int a = 1 | 2 ^ 3 & 5;
int b = ((1 | 2) ^ 3) & 5;
int c = 1 | (2 ^ (3 & 5));
System.out.print(a + "," + b + "," + c);
}
}
Download BitwiseOperatorsDemo2 Source code
COMPOUND OPERATORS
compound operators perform shortcuts in common programming operations. Java has eleven compound assignment operators.
Syntax:
= argument2.
The above statement is the same as, argument1 = argument1 operator argument2. An example program is shown below that demonstrates the different Compound operators in java.
public class CompoundOperatorsDemo {
public CompoundOperatorsDemo() {
int x = 0, y = 5;
x += 3;
System.out.println("x : " + x);
y *= x;
System.out.println("y : " + y);
/*Similarly other operators can be applied as shortcuts. Other
compound assignment operators include boolean logical,
bitwise and shift operators*/
}
public static void main(String args[]) {
new CompoundOperatorsDemo();
}
}
Download CompoundOperatorsDemo Source code
CONDITIONAL OPERATORS
The Conditional operator is the only ternary (operator takes three arguments) operator in Java. The operator evaluates the first argument and, if true, evaluates the second argument. If the first argument evaluates to false, then the third argument is evaluated. The conditional operator is the expression equivalent of the if-else statement. The conditional expression can be nested and the conditional operator associates from right to left: (a?b?c?d:e:f:g) evaluates as (a?(b?(c?d:e):f):g)
An example program is shown below that demonstrates the Ternary operator in java.
public class TernaryOperatorsDemo {
public TernaryOperatorsDemo() {
int x = 10, y = 12, z = 0;
z = x > y ? x : y;
System.out.println("z : " + z);
}
public static void main(String args[]) {
new TernaryOperatorsDemo();
}
}
Download TernaryOperatorsDemo Source code
/*
* The following programs shows that when no explicit parenthesis is used then the
* conditional operatorevaluation is from right to left
*/
public class BooleanEx1 {
static String m1(boolean b) {
return b ? "T" : "F";
}
public static void main(String[] args) {
boolean t1 = false ? false : true ? false : true ? false : true;
boolean t2 = false ? false
: (true ? false : (true ? false : true));
boolean t3 = ((false ? false : true) ? false : true) ? false
: true;
System.out.println(m1(t1) + m1(t2) + m1(t3));
}
}
Output
FFT
Download TernaryOperatorsDemo2 Source code
Type conversion allows a value to be changed from one primitive data type to another. Conversion can occur explicitly, as specified in
the program, or implicitly, by Java itself. Java allows both type widening and type narrowing conversions.
In java Conversions can occur by the following ways:
- Using a cast operator (explicit promotion)
- Using an arithmetic operator is used with arguments of different data types (arithmetic promotion)
- A value of one type is assigned to a variable of a different type (assignment promotion)
OPERATOR PRECEDENCE
The order in which operators are applied is known as precedence. Operators with a higher precedence are applied before operators with a lower precedence. The operator precedence order of Java is shown below. Operators at the top of the table are applied before operators lower down in the table. If two operators have the same precedence, they are applied in the order they appear in a statement.
That is, from left to right. You can use parentheses to override the default precedence.
Example
In an operation such as,
result = 4 + 5 * 3
First (5 * 3) is evaluated and the result is added to 4 giving the Final Result value as 19. Note that ‘*’ takes higher precedence than ‘+’ according to chart shown above. This kind of precedence of one operator over another applies to all the operators.
QUIZ
1. How to generate a random number between 1 to x, x being a whole number greater than 1
Ans: double result = x * Math.random();
No comments:
Post a Comment