T h e Logical Operators :
T
he following table lists the logical operators :
Assume Boole an variable
s A holds true and
variable B holds false , the n:
Operator
|
Description
|
Example
|
&&
|
Called Logical AND operator. If both the operands are non-zero, the n the condition becomes true .
|
(A && B) is false .
|
||
|
Called Logical OR Operator. If any of the two operands are non-zero, the n the condition be comes true .
|
(A || B) is true .
|
!
|
Called Logical NOT Operator. Us e to re verses the logical state of its operand. If a condition is true the n Logical NOT operator will make false .
|
!(A && B) is true .
|
Boolean Logical Operators :
The Boolean logical
operators shown here operate only on boolean operands. All
of the binary
logical operators combine two boolean 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
The Assignment Operator
The assignment
operator is the single equal sign, =. The assignment operator works in Java
much as it does in any other computer language. It has this general form:
var = expression;
Here, the type of
var must be compatible with the type of expression. For example, consider
this fragment:
int x, y, z;
x = y = z = 100; //
set x, y, and z to 100
This fragment sets
the variables x, y, and z to 100 using a single statement.
Misc Operators
There are few other operators supported by Java Language.Conditional Operator ( ? : ):
Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide which value should be assigned to the variable. The operator is written as:variable x = (expression) ? value if true : value if falseFollowing is the example:
public class Test {
public static void main(String args[]){
int a , b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
}
This would produce the following result:Value of b is : 30
Value of b is : 20
No comments:
Post a Comment