Operators
Previous | Home | Next |
Operators are used to manipulate primitive data types.java provide a set of operators that can be used to perform an action on one or two operands.an operator that can operate on one operator is called unary operand and an
operator which can operate on two operands is called binary operator.
java operators are divided in different categories.
Assignment, arithmetic ,relational ,logical,bit wise,compound assignment, and conditional
Assignment operator is =
Arithmetic operators are +, - ,/, *, %, ++, --
Relational operators > ,<, >= ,<= ,!=, ==
Logical operators. &&, | | , !.
Compound assignment operators +=, -=, *=, /=, %=,<<==,>>==.
Conditional operators ?:
Assignment operator
int i=4;
int j=8; //assignment operator = is used to assign the value to the variables i,and j ,the values assigning are 4and 8 respectively.
Arithmetic operators
int i=9;
int j=0;
int x;
x= i+j; // operator is used to add the two integer values.
Relation operators
int x=10;
int y=21;
if(x>y) // relation check between x and y.
{
system .out.println ( "the greater no is:" + x);
}
else
{
system .out. println ("the greater no is :" + y);
}
Conditional operators
int x = 8;
int y = 9;
if (x== 1) && (y==2)
{
system .out.println ("the value of x is 8 AND value of y is 9");
}
Assignment Operators
public class AssignmentOperators
{
public AssignmentOperators()
{
// Assigning Primitive Values
int i, j, k;
i = 10; // j gets the value 10.
j = 5; // j gets the value 5. Previous value is overwritten.
k = i+j; // k gets the value 15.
System.out.println("The value of i is : " + i);
System.out.println("The value of j is : " + j);
System.out.println(" The value of k is:"+ k);
Public static void main( String arg[] )
{
new Assignmentoperator()
}
}
Arithmetic Operators
public class ArithmeticOperators
{
public ArithmeticOperators()
{
int x = 20 y = 10, z ;
z=x+y;
System.out.println("+ operator resulted in " +z);
public static void main(String arg[])
{
new Arithmeticoperators();
}
}
Relational Operators
public class RelationalOperators
{
public RelationalOperators()
{
int x = 20 y = 10, z ;
z=x+y;
System.out.println(" x > y: " +(x>y));
System.out.println(" x < y :" + (x<y));
System .out.println(" x>= y :"+ (x>=));
public static void main (String arg[])
{
new Relationaloperators();
}
}
Logical operators
public class LogicalOperators
{
public LogicalOperators()
{
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));
public static void main (String arg[])
{
new Logicaloperators();
}
}
Previous | Home | Next |