Skip to main content

Operators:




Expression
                        The Expression is the valid combination of operators and operands. Operators are one which perform the operation and the operands are one on which the operation is performed.
                        e..g.
                                    3*4+5 is an expression .
                                    Operators are * and + , and Operands are 3,4 and 5.


Operators in Java:

·         Arithmetic Operators
·         Relational Operators
·         Logical Operators
·         Assignment Operators
·         Conditional Operator
·         Increment and Decrement Operator
·         Bitwise Operators
·         Special Operators

Priority Of Operators
{}       
()
                        !
                        *,/,%
                        +,-
                        >,<,>=,<=
                        ==,!=
                        &&
                        ||
                        =

(i) Arithmetic Operators:
These Operators are used to perform the calculation
Java has five arithmetic operators:

Operator                                                                                                          Description
           
                  +                                                                                                                Addition
                   -                                                                                                                Subtraction
                   *                                                                                                               Multiplication
                    /                                                                                                               Division
                  %                                                                                                               Modulus
           
The behaviour of the +,-,*,/ operator  is same e as c,c++. The modulus operator can be applied to integer as well as floating-point type while in case of c/c++ it can be applied to integer type only.
Important Rule Regarding calculating the Modulus:

            To calculate the modulus, ignore the sign and calculate the remainder and the sign of the resultant value will depends on the sign of the left operand in the expression.

            Example:
                                                int i=5%2;
                                                int j=-5%2;
                                                int k=5%-2;
                                                int m=-5%-2;

            Answer:
                                                i=1
                                                j=-1
                                                k=1
                                                m=-1

(ii) Relational Operators:

                        The Relational operators are used for comparing the two values and the result of the comparison is a Boolean value i.e. true or false.

            Java supports the following ordinal comparisons / relational operators.
            Operator                                                                                              Description                                    >                                                                                        Greater Than
                 <                                                                                         Less Than
                 >=                                                                                       Greater than or equal to
                 <=                                                                                       Less than or equal to
                 ==                                                                                       Equal to
                  !=                                                                                       Not equal to

These operators are used to compare ordinal data types. Data types where values have numeric order.      

(iii) Logical Operators:

            There are three types of Logical Operators:

                        (a) Logical AND(&&)
                        (b) Logical OR(||)
                        (c) Logical NOT(!)

(a) Logical AND (&&)

                        The Logical AND (&&) operators is used to combine two conditions and the result of the operation is true if and only if both of the conditions are true.

The truth table of the Logical AND
            Condition1                  Condition2                              Condition1&&Condition2
            false                             false                                                     false
            false                             true                                                      false
            true                              false                                                    false
            true                              true                                                      true
           

            It is also known as the Short Circuit Logical AND Operator because if the first condition hold false it will not check the second condition.

(b) Logical OR(||)

                        The Logical OR (||) operators are used to combine two conditions and the result of the operation is true if any of the one or both conditions are true.

The truth table of the Logical OR

            Condition1                              Condition2                              Condition1||Condition2
            false                                         false                                        false
            false                                         true                                          true
            true                                          false                                         true
            true                                          true                                          true

            It is also known as the Short Circuit Logical OR Operator because if the first condition hold true it will not check the second condition.

(c) Logical NOT(!)

            This operator is used to reverse the condition and if the condition is true then the condition will become false and if the condition is false then the condition will become true.
The truth table of the Logical NOT(!)
            Condition                                                                                            !Condition
            false                                                                                                     true
            true                                                                                                      false
           
(iv) Assignment Operator:
            The = operators is known as the Assignment Operators and it is used to assign the value into the variable.
Syntax: <Variable> = <expression>              
Example:
            a=2
                                    It will assign the value 2 into the variable a

            a==2
                                    It will compare whether the value stored in the variable a is equal to 2 or not.

(v) Increment and Decrement Operators:

++ Operator is known as Increment Operator and it is used to increase the value of variable by 1.
There are two types of Increment Operators:
           
            (i) Pre-Increment Operator
           
            (ii) Post-Increment Operator

(i) Pre-Increment Operator
                        In this case, first the increment operation is performed and then the other operation will take place.
                       
            Consider the following example:
                                   
                                    i=6;

                                    j=++i;

                                    So, the above statement is equivalent to ,
                                                i=i+1==> i=6+1=7

                                                j=i;  ==> j=7

            (ii) Post-Increment Operator
                        In this case, first the other operation is performed, and then the increment will take place.

                        Consider the following example:

                                    i=6

                                    j=i++;
The above mentioned statement is equivalent to

                                                j=i; ==> j=6

                                                i=i+1 ==> i=6+1==> 7

Consider the following code,

                                    int a=4;

                                    int b,c;

                                    b=2*(a++);

                                    c=2*(++a);

Find out the final value of a, b, and c?

                        a=6  ,   b=8   ,   c=12              


y=2;
y=++y*y++*++y;

Decrement Operators:
            (i) Pre- Decrement Operators:
            (ii) Post- Decrement Operators:

(vi) Bitwise Operators:

Java's bitwise operators operate on individual bits of integer (int and long) values. If an operand is shorter than an int, it is promoted to int before doing the operations.
            The bitwise operators operates on the individuals bits of the numbers. To perform the operators, we             have to convert them into their binary equivalents.
                        List of the Bitwise Operators:

                                    (i) One's Complement
                                    (ii) Bitwise AND(&)
                                    (iii) Bitwise OR(|)
                                    (iv) Bitwise XOR(^)
                                    (v) Bitwise Arithmetic Right Shift (>>)
                                    (vi) Bitwise Unsigned Right Shift (>>>)
                                    (vii) Bitwise Left Shift

Comments

Popular posts from this blog

Menu bar example with applet viewer in java

      Menu bar +applet viewer example-3 import java.applet.*; import java.awt.*;  /*   <applet code="MenuDemo" height=200 width=200>   </applet> */ public class MenuDemo extends Applet {             public void init( )             {                         Menus m = new Menus("Menu Bar Demo");                                    m.resize(200,200);       m.show();             } } class Menus extends Frame {             Menus(String s)             {                         super(s);                         Me...

Java question

  Java ques interview Ques:-List any five features of Java?  Ans :-Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multi-threaded Ques . write full form of jdbc & odbc ?  Ans :-  JDBC -java database connectivity.             ODBC - open database connectivity Ques:- what are types of packages ? Ans: 1. Pre-defined packages 2. User defined packages Ques :-Define class?  Ans:- A class is a blue print from which individual objects are created. A class can contain fields and methods to describe the behavior of an object. Ques:- Denfie string bulider? Ans :- Use StringBuilder whenever possible because it is faster than StringBuffer. But, if thread safety is necessary then use StringBuffer objects. Ques :- What is Singleton class?  Ans:Singleton class control object creation, limiting the number to one but allowing the flexibility to create m...

MVC Example 1

                        Swing MVC Example in java Java Swing first programs In this chapter, we will program our first programs in Swing toolkit. The examples are going  to be very simple. We will cover some basic functionality. Our first example In our first example, we will show a basic window. import javax.swing.JFrame; public class Simple extends JFrame { public Simple() { setSize(300, 200); setTitle("Simple"); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { Simple simple = new Simple(); simple.setVisible(true); } } While this code is very small, the application window can do quite a lot. It can be resized, maximized, minimized. All the complexity that comes with it has been hidden from the application programmer. import javax.swing.JFrame; Here we import the JFrame widget. It is a toplevel container, which is used for plac...