Friday 1 March 2013

JAVA OPERATORS


Operator is a symbol that performs a specified operation between the data items.  Java provides the following different types of operators.

a) Arithmetic operators:
                  Operator              Meaning

                        +                      Addition
-                                              Subtraction
*                      Multiplication
/                       Division
%                    Modulus

Example:

class arope
{
            public static void main(String[] args)
            {
                        int a,b,sum,sub,mul,div,mod;
                        a=Integer.parseInt(args[0]);
                        b=Integer.parseInt(args[1]);
                        sum=a+b;
                        sub=a-b;
                        mul=a*b;
                        div=a/b;
                        mod=a%b;
                        System.out.println("Addition="+sum);
                        System.out.println("Subtraction="+sub);
                        System.out.println("Multiplication="+mul);
                        System.out.println("Division="+div);
                        System.out.println("Remainder="+mod);
            }
}

b) Arithmetic Assignment Operators:
            Syntax:            variable operator = expression
                                                Which is equivalent to

                                    variable = variable operator expression


                    Operator                        Example         Equivalent Expression

                        + =                   a + = 4                         a = a + 4
                        - =                    a - =  5                         a = a – 5
                        * =                   a * = 3                         a = a * 3
                        / =                    a / = 8                          a = a / 8
                        % =                 a % = 5                       a = a % 5


c) Assignment Operator: ‘=’ is an assignment operator used to assign the value to a variable.
            Syntax:            variable = value;
            Example:         a = 7;        


d) Increment and Decrement Operators: ‘++’ and ‘--' are Java’s increment and decrement operators.  The increment operator increased its operand by one.  The decrement operator decreased its operand by one.
Example:

class a
{
            public static void main(String[] args)
            {
            int x=4;
            System.out.println("x++:"+ ++x);   //O/P:  5
            int y=10;
            System.out.println("x++:"+ y++);   //O/P:  5
            int p=5;
            int t=--p+3;
            System.out.println("t :"+t);              //O/P:  7
            }
}
e) Relational Operators: Relational Operators determines the relationship between the two operands.
      Operator                                Meaning                                     Example
            = =                                     equal to                                         A= =B
            !=                                       Not equal to                                 A!=B
            >                                        Greater than                                A>B
            <                                        Less than                                      A<B
            >=                                      Greater than or equal to             A>=B
            <=                                      Less than or equal to                   A<=B

f) Logical Operators: Logical operators are used to check for compound conditions, which are formed, by the combination of two or more relational expressions.  Java provides the following logical operators and these are follows truth tables and produce Boolean values.

            Operators:      Logical AND &&
                                    Logical OR ||
                                    Logical NOT !

Truth table for Logical AND(&&):
            Op1                 Op2                 Op1 && Op2
            T                     T                                 T
            T                     F                                  F
            F                      T                                 F
            F                      F                                  F
Truth table for Logical OR(||):
            Op1                 Op2                 Op1 && Op2
            T                     T                                 T
            T                     F                                  T
            F                      T                                 T
            F                      F                                  F
Truth table for Logical NOT(!):
            Op1                 !Op1
            T                     F
            F                      T

g) Conditional Operator: Java has a decision operator called conditional operator.  “? :” are conditional operators.  These operators are also called ternary operators.
            Syntax:            testconditon ? expression1 : expression2
Here, if the testcondition is true, then expression1 is evaluated otherwise, expression2 is evaluated.
            Example:         max = (a>b) ? a : b;

h) Bit-wise Operators: The following bit-wise operators are provided by Java.  These operators act upon the individual bits on their operands.

                              Operator                            Meaning         

                                    ~                      Bit-wise unary NOT
                                    &                     Bit-wise AND
                                    |                       Bit-wise OR
                                    ^                      Bit-wise exclusive OR
                                    >>                    Shift right
                                    <<                    Shift left
                                    |=                     Bit-wise OR Assignment
                                    ^=                    Bit-wise exclusive OR Assignment
                                    >>=                 Shift right Assignment
                                    <<=                 Shift left Assignment

            These operators act upon the individual bits on their operands.  Bit-wise Logical Operators (&, |, ^ and ~) follows the following truth tables

         Op1     Op2                  Op1|Op2   Op1&Op2       Op1^Op2            ~Op1
            0          0                            0                0                      0                      1
            1          0                            1                0                      1                      0
            0          1                            1                0                      1                      1
            1          1                            1                1                      0                      0



javax.swing: It is package contains an important class called JOptionPane class.  This class is used to display dialog boxes.  It provides two important predefined methods showInputDialog and showMessageDialog called static methods.  Such methods are always used their class name followed by a dot operator.

ShowInputDialog: showInputDialog is a method of class JOptionPane.  It is a static method and the general format is as
            Syntax:           JOptionPane.showInputDialog(argument);
The argument indicates to the user what to do in the text field.
            Example:        JOptionPane.showInputDialog(“Enter a value”);
 When we perform this operation the input dialog box is available as


 










The users types characters in the text field then clicks the ok button, it returns string to the program.


showMessageDialog: showMessageDialog is method of class JOptionPane.  It is a static method.  The general format of showMessageDialog is in two ways.
            Syntax1: JOptionPane.showMessageDialog(null,argument1);
The first argument must be null keyword.  The second argument is the string that is required to display.  The title bar of message dialog contains the string “Message” to indicate that the dialog is presenting a message to the user.  The default icon is INFORMATION_MESSAGE.
            Example: JOptionPane.showMessageDialog(null,”value=”+x);


 








Syntax2: JOptionPane.showMessageDialog(null,argument2,argument3,argument4)
The first argument must be null keyword.  The second argument is the message to display.  The third argument is the string to display in the title bar of the dialog.  The fourth argument is a value indicating the type of message dialog to display.
            For argument4 some of the message dialogs are
                        JOptionPane.PLAN_MESSAGE
                        JOptionPane.ERROR_MESSAGE
                        JOptionPane.INFORMATION_MESSAGE
                        JOptionPane.WARNING_MESSAGE
                        JOptionPane.QUESTION_MESSAGE
            Example:
JOptionPane.showMessageDialog(null,”value=”+x,”output”, JOptionPane.ERROR_MESSAGE)






No comments:

Post a Comment