Thursday, 7 September 2017

Java Opearators



Java operators are symbols that is used to perform mathematical or logical manipulations. Java is rich with built-in operators.
There are many types of operators available in Java such as:

                - Arithmetic Operators
                - Relational Operators
                - Logical Operators
                - Bitwise Operators
                - Assignment Operators
                - Miscellnous  Operators.

Arithmetic Operators 

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators: 



                                                  Operator                              Description

                                         +                                  Addition

                                         *                                  Multiplication

                                                                           Subtraction (also unary minus)
                                                                                                                                                               
                                          /                                  Division
 
                                         %                                 Modulus

                                        ++                                 Increment

                                         +=                                Addition assignment

                                         –=                                Subtraction assignment

                                         *=                                 Multiplication assignment

                                         /=                                 Division assignment

                                         %=                               Modulus assignment
 
                                          – –                              Decrement

 Example :

// Demonstrate the basic arithmetic operators.
class BasicMath
{

public static void main(String args[])
{
// arithmetic using integers

System.out.println("Integer Arithmetic");
int a = 1 + 1;
int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);
// arithmetic using doubles
System.out.println("\nFloating Point Arithmetic");
double da = 1 + 1;
double db = da * 3;
double dc = db / 4;
double dd = dc - a;
double de = -dd;
System.out.println("da = " + da);
System.out.println("db = " + db);
System.out.println("dc = " + dc);
System.out.println("dd = " + dd);
System.out.println("de = " + de);
}
}


Relational Operators

The relational operators determine the relationship that one operand has to the other.

Specifically, they determine equality and ordering. The relational operators are shown here:

     Operator
Description
==
Is equal to

!=
Is not equal to

Greater than

Less than

>=
Greater than or equal to
<=

Less than or equal to







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

 Example :



// Demonstrate the boolean logical operators.
class BoolLogic
{
        public static void main(String args[])
        {
           boolean a = true;
           boolean b = false;
           boolean c = a | b;
           boolean d = a & b;
           boolean e = a ^ b;
           boolean f = (!a & b) | (a & !b);
           boolean g = !a;
           System.out.println(" a = " + a);
           System.out.println(" b = " + b);
           System.out.println(" a|b = " + c);
           System.out.println(" a&b = " + d);
           System.out.println(" a^b = " + e);
           System.out.println("!a&b|a&!b = " + f);
           System.out.println(" !a = " + g);
        }

}
 
Bitwise Operators

 Operator


Description
<< 


Binary Left Shift Operator
>> 


Binary Right Shift Operator
>>> 


Shift right zero fill operator
~

     
Binary Ones Complement Operator
&


Binary AND Operator
^


Binary XOR Operator
|


Binary OR Operator

Assignment Operators

Assignment operator supported by Java are as follows:


Operator
Description

=
assigns values from right side operands to left side operand

+=
adds right operand to the left operand and assign the result to left

-=
subtracts right operand from the left operand and assign the result to left operand

*=
mutiply left operand with the right operand and assign the result to left operand

/=
divides left operand with the right operand and assign the result to left operand

%=
calculate modulus using two operands and assign the result to left operand.


Miscellaneous 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 false
Following is an example −

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 );
   }
}
 

Precedence of Java Operators

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator −
For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3 * 2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.



 thejavaxpert blog, thejavaxpert.blogspot.com, Piyush Dabhi blog, Dabhi Piyush blog, TheJavaXpert blogger owner name,  Java all info, java master, java manual, java Expert, Java Best information, Java New blog, JAVA, Piyush, Dabhi, piyush blog, Pkdabhi,  Gurukul blog, BCA, BCA best knowledge, IT, IT knowledge,  language, OOP concept, Full OOP languages name, All java program, Core java, Core Java Interview, Java Knowledge, JAVA language, Full Object oriented languages, Java nice info, Java best knowledge, Java Master, Java  java,, My java, javazone, Java game programming, java programs, java GUI, Java Blog, Top 10 java blog, javaxpert is the best blog, Java new blog list,  java top 10 blog, java top website, java top web, There are many types of operators in java which are given below:  Arithmetic Operator, shift Operator, Relational Operator, Bitwise Operator, Logical Operator, Ternary Operator and. Assignment Operator. opearotor of javajava,


































































































































































































































































































































































































































































































































































Popular posts