KEMBAR78
Operators in java | PPTX
Operators in Java
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Practice Questions
Arithmetic Operators
• Arithmetic operators are used in mathematical
like addition, subtraction etc. The following table
lists the arithmetic operators:
Operator Description
+ Additive operator (also used for String
concatenation)
- Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator
public static void main(String[] a)
{
System.out.println("Arithmetic operator");
System.out.println("the + operator value for 1 + 3 "+(1+3));
System.out.println("the - operator value for 1 + 3 "+(1-3));
System.out.println("the * operator value for 1 + 3 "+(1*3));
System.out.println("the / operator value for 1 + 3 "+(1/3));
System.out.println("the % operator value for 1 + 3 "+(1%3));
}
Do and See the output
Goto<<slide1>>
Unary Operators
• The unary operators require only one operand; they
perform various operations such as
incrementing/decrementing a value by one, negating an
expression, or inverting the value of a boolean.
Operator Description
+ Unary plus operator; indicates positive
value (numbers are positive without this,
however)
- Unary minus operator; negates an expression
++ Increment operator; increments a value by 1
-- Decrement operator; decrements a value by 1
! Logical complement operator; inverts the value
of a boolean
public static void main(String[] a)
{
int post_increment=1;
int post_decrement=2;
int pre_increment=3;
int pre_decrement=4;
post_increment++; post_decrement--; ++pre_increment; --pre_decrement;
boolean boolean_variable=true;
System.out.println("Unary operator");
System.out.println("the + operator value for +1::"+(+1));
System.out.println("the - operator value for -1::"+(-1));
System.out.println("the ++ operator value for 1++::"+
(post_increment));
System.out.println("the -- operator value for 2--::"+
(post_decrement));
System.out.println("the ++ operator value for ++3::"+
(pre_increment));
System.out.println("the -- operator value for --4::"+
(pre_decrement));
System.out.println("the ! operator value for !true::"+(!
boolean_variable));
}
Do and See the output
GoTo<<SLide1>>
Relational Operators
• An operator that compares two values . There are following relational
operators supported by Java language
Operator Description
== Checks if the value of two operands are equal or not, if
yes then condition becomes true.
!= Checks if the value of two operands are equal or not, if
values are not equal then condition becomes true.
> Checks if the value of left operand is greater than the
value of right operand, if yes then condition becomes
true.
< Checks if the value of left operand is less than the
value of right operand, if yes then condition becomes
true.
>= Checks if the value of left operand is greater than or
equal to the value of right operand, if yes then
condition becomes true.
<= Checks if the value of left operand is less than or equal
to the value of right operand, if yes then condition becomes
true.
public static void main(String[] y)
{
System.out.println("Relational operators");
System.out.println("the == operator is 1==1::"+(1==1));
System.out.println("the != operator is 1!=1::"+(1!=1));
System.out.println("the > operator is 1>2::"+(1>2));
System.out.println("the < operator is 1<2::"+(1<2));
System.out.println("the <= operator is 1<=2::"+(1<=2));
System.out.println("the >= operator is 1>=2::"+(1>=2));
}
Do and See the output
GoTo<<Slide1>>
Logical Operators
• This logical operator use for join two conditions
Operator Description
&& Called Logical AND operator. If both the
operands are non zero then then condition
becomes true.
|| Called Logical OR Operator. If any of the two
operands are non zero then then condition
becomes true.
! Called Logical NOT Operator. Use to reverses
the logical state of its operand. If a condition is
true then Logical NOT operator will make false.
.
public static void main(String[] y)
{
int x=10, Y=20;
boolean a=false;
if(x<20&&Y>10)
{
System.out.println(" the && if condition true"); }
else
{ System.out.println(" the && if condition false"); }
if(x<=10||x!=10)
{
System.out.println(" the || if condition true"); }
else
{ System.out.println(" the || if condition false");}
if(!a)
{ System.out.println(" the ! if condition true"); }
else
{ System.out.println(" the !if condition false"); }
}
Do and See the output
GoTo<<Slide1>>
Bitwise Operators
• Bitwise operators are used to change individual bits in an operand. Bitwise
operator works on bits(0 or 1) and perform bit by bit operation.
Operator Description
& Binary AND Operator copies a bit to the result if it exists in
both operands.
| Binary OR Operator copies a bit if it exists in ei0ther
operand.
^ Binary XOR Operator copies the bit if it is set in one operand
but not both.
~ Binary Ones Complement Operator is unary and has the
effect of 'flipping' bits.
<< Binary Left Shift Operator. The left operands value is moved
left by the number of bits specified by the right
operand.
>> Binary Right Shift Operator. The left operands value is
moved right by the number of bits specified by the right
operand.
>>> Shift right zero fill operator. The left operands value is moved right
by the number of bits specified by the right operand and
shifted values are filled up with zeros.
public static void main(String[] y)
{
int a1=12;
int a2=2;
int a3=0;
int a4=16;
/*8421
* 12=1100
* 1=0001
* & both bits are then it is 1 else it is 0
*/
System.out.println("& operator is "+(a1& a2));
System.out.println("| operator is "+(a1| a2));
System.out.println("~ operator is "+(~a1));
System.out.println("<< operator is "+(a1<<1));
System.out.println(">> operator is "+(a1>>1));
}
• Do and See the output
• GoTo<<Slide1>>
Assignment Operators
• Assigning a value to the destination variable
Operator Description
= Simple assignment operator, Assigns values from right side
operands to left side operand
+= Add AND assignment operator, It adds right operand to the left
operand and assign the result to left operand
-= Subtract AND assignment operator, It subtracts right operand from
the left operand and assign the result to left operand
*= Multiply AND assignment operator, It multiplies right operand with
the left operand and assign the result to left operand
/= Divide AND assignment operator, It divides left operand with the
right operand and assign the result to left operand
%= Modulus AND assignment operator, It takes modulus using two
operands and assign the result to left operand
<<= Left shift AND assignment operator
>>= Right shift AND assignment operator
&= Bitwise AND assignment operator
^= Bitwise exclusive OR and assignment operator
|= Bitwise inclusive OR and assignment operator
public static void main(String[] y)
{
System.out.println("assignment operator");
int a5=3;
System.out.println("the value a5 += is"+(a5+=1));
System.out.println("the value a5 -= is"+(a5-=1));
System.out.println("the value a5 -= is"+(a5<<=2));
}
• Do and See the output
• GoTo<<Slide1>>
/** following code prints i%j value is 2 what should be modify to get value 8 */
int i=45+42-48-5;
int j=5+5-8+2;
System.out.println("the value of i%j::"+i%j);
/** the following code compile errors debug the code and rectify the problem*/
int i=(2(+5-8) (+5-5)+10)*2; System.out.println("the value of i is"+i);
/**what should be changed to obtain the value 40.0 to the x value*/
double x=Math.rint(40.6);System.out.println(x);

Operators in java

  • 1.
    Operators in Java ArithmeticOperators Relational Operators Bitwise Operators Logical Operators Assignment Operators Practice Questions
  • 2.
    Arithmetic Operators • Arithmeticoperators are used in mathematical like addition, subtraction etc. The following table lists the arithmetic operators: Operator Description + Additive operator (also used for String concatenation) - Subtraction operator * Multiplication operator / Division operator % Remainder operator
  • 3.
    public static voidmain(String[] a) { System.out.println("Arithmetic operator"); System.out.println("the + operator value for 1 + 3 "+(1+3)); System.out.println("the - operator value for 1 + 3 "+(1-3)); System.out.println("the * operator value for 1 + 3 "+(1*3)); System.out.println("the / operator value for 1 + 3 "+(1/3)); System.out.println("the % operator value for 1 + 3 "+(1%3)); } Do and See the output Goto<<slide1>>
  • 4.
    Unary Operators • Theunary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean. Operator Description + Unary plus operator; indicates positive value (numbers are positive without this, however) - Unary minus operator; negates an expression ++ Increment operator; increments a value by 1 -- Decrement operator; decrements a value by 1 ! Logical complement operator; inverts the value of a boolean
  • 5.
    public static voidmain(String[] a) { int post_increment=1; int post_decrement=2; int pre_increment=3; int pre_decrement=4; post_increment++; post_decrement--; ++pre_increment; --pre_decrement; boolean boolean_variable=true; System.out.println("Unary operator"); System.out.println("the + operator value for +1::"+(+1)); System.out.println("the - operator value for -1::"+(-1)); System.out.println("the ++ operator value for 1++::"+ (post_increment)); System.out.println("the -- operator value for 2--::"+ (post_decrement)); System.out.println("the ++ operator value for ++3::"+ (pre_increment)); System.out.println("the -- operator value for --4::"+ (pre_decrement)); System.out.println("the ! operator value for !true::"+(! boolean_variable)); } Do and See the output GoTo<<SLide1>>
  • 6.
    Relational Operators • Anoperator that compares two values . There are following relational operators supported by Java language Operator Description == Checks if the value of two operands are equal or not, if yes then condition becomes true. != Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.
  • 7.
    public static voidmain(String[] y) { System.out.println("Relational operators"); System.out.println("the == operator is 1==1::"+(1==1)); System.out.println("the != operator is 1!=1::"+(1!=1)); System.out.println("the > operator is 1>2::"+(1>2)); System.out.println("the < operator is 1<2::"+(1<2)); System.out.println("the <= operator is 1<=2::"+(1<=2)); System.out.println("the >= operator is 1>=2::"+(1>=2)); } Do and See the output GoTo<<Slide1>>
  • 8.
    Logical Operators • Thislogical operator use for join two conditions Operator Description && Called Logical AND operator. If both the operands are non zero then then condition becomes true. || Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. ! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. .
  • 9.
    public static voidmain(String[] y) { int x=10, Y=20; boolean a=false; if(x<20&&Y>10) { System.out.println(" the && if condition true"); } else { System.out.println(" the && if condition false"); } if(x<=10||x!=10) { System.out.println(" the || if condition true"); } else { System.out.println(" the || if condition false");} if(!a) { System.out.println(" the ! if condition true"); } else { System.out.println(" the !if condition false"); } } Do and See the output GoTo<<Slide1>>
  • 10.
    Bitwise Operators • Bitwiseoperators are used to change individual bits in an operand. Bitwise operator works on bits(0 or 1) and perform bit by bit operation. Operator Description & Binary AND Operator copies a bit to the result if it exists in both operands. | Binary OR Operator copies a bit if it exists in ei0ther operand. ^ Binary XOR Operator copies the bit if it is set in one operand but not both. ~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. >>> Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros.
  • 11.
    public static voidmain(String[] y) { int a1=12; int a2=2; int a3=0; int a4=16; /*8421 * 12=1100 * 1=0001 * & both bits are then it is 1 else it is 0 */ System.out.println("& operator is "+(a1& a2)); System.out.println("| operator is "+(a1| a2)); System.out.println("~ operator is "+(~a1)); System.out.println("<< operator is "+(a1<<1)); System.out.println(">> operator is "+(a1>>1)); } • Do and See the output • GoTo<<Slide1>>
  • 12.
    Assignment Operators • Assigninga value to the destination variable Operator Description = Simple assignment operator, Assigns values from right side operands to left side operand += Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand -= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand *= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand /= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand %= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand <<= Left shift AND assignment operator >>= Right shift AND assignment operator &= Bitwise AND assignment operator ^= Bitwise exclusive OR and assignment operator |= Bitwise inclusive OR and assignment operator
  • 13.
    public static voidmain(String[] y) { System.out.println("assignment operator"); int a5=3; System.out.println("the value a5 += is"+(a5+=1)); System.out.println("the value a5 -= is"+(a5-=1)); System.out.println("the value a5 -= is"+(a5<<=2)); } • Do and See the output • GoTo<<Slide1>>
  • 14.
    /** following codeprints i%j value is 2 what should be modify to get value 8 */ int i=45+42-48-5; int j=5+5-8+2; System.out.println("the value of i%j::"+i%j); /** the following code compile errors debug the code and rectify the problem*/ int i=(2(+5-8) (+5-5)+10)*2; System.out.println("the value of i is"+i); /**what should be changed to obtain the value 40.0 to the x value*/ double x=Math.rint(40.6);System.out.println(x);