Operators in Java

Java enables users to develop and deploy applications on the Internet for servers, desktop computers, and small hand-held devices. Java is a general purpose programming language. So it has all those operators and constructs that other general purpose programming languages have. Following is the detail of the operators in Java:

The Unary Operators: ++    —   +    –     ~     !    (type)

Increment / decrement operators

  • ++ and – Operators are the increment and decrement operators respectively.
  • + and - are sign operators.
  • ! Operator is the not operators.
  • (type) Operator returns the type of an object.

Bitwise inversion operator (~)

~ Operator is applied to integral types. It converts all the 1 bits in a binary value to 0s and all the 0 bits to 1s. For example, 01100001 is converted to 10011110

The Arithmetic Operators:  +   –   *   /   %

  • + Operator is used to add two operands
  • - Operator is used to subtract the right operand from the left operand
  • * Operator is used to multiply the two operands
  • / Operator is used to divide the left operand by right operand
  • % (Modulo) Operator is used to get the remainder by dividing the left operand by right operand

The Shift Operators: <<    >>   >>>

Shifting is used for efficient multiplication and division by powers of two. It takes the binary representation of the number and move the bit pattern either left or right following some rules that achieves the multiplication or division of the numbers.
Syntax: value << num

  • << (Left Shift Operator) shifts the bits to left and adds a 0 on the right (least significant bit)
  • >> (Right Shift Operator) shifts the bits to right and adds a 0 on the left (most significant bit) but 1 in case of signed numbers
  • >>> (Unsigned Right Shift Operator) shifts the bits to right and adds a 0 on the left (most significant bit)

The Comparison Operators: <  <=  >  >=   ==   !=   instanceof

These operators always evaluate to boolean result. These can be broadly categorized based on the resulting value as: Ordinal ( <  <=  >  >=) , Equality ( ==  ! =) and Object (instanceof).

  • < (Less than Operator) compares the two numeric values and returns true if left value is less than right value. Otherwise it returns false.
  • <= (Less than equal to Operator) compares the two numeric values and returns true if left value is less than or equal to right value. Otherwise it returns false.
  • (Greater than Operator) compares the two numeric values and returns true if left value is greater than right value. Otherwise it returns false.
  • >= (Greater than equal to Operator) compares the two numeric values and returns true if left value is greater than or equal to right value. Otherwise it returns false.
  • == (Equal to equal to Operator) compares the two numeric values and returns true if both are equal. Otherwise it returns false.
  • != (Not equal to Operator) compares the two numeric values and returns true if both the values are not equal. Otherwise it returns false.
  • instanceof Operator compares the left identifier with the right Class Name and returns true if left identifier is an object of right Class Name type. Otherwise it returns false.

Bitwise Operators: &   ^   |

These operators can be used with both integral and boolean type. With boolean values, Bitwise Operators return true or false ( both operands must be boolean)

  • & (Bitwise AND Operator) compares each bit of the two integer values and returns the new integer value based on AND Operation on bits.
  • | (Bitwise OR Operator) compares each bit of the two integer values and returns a new integer value based on OR Operation on bits.
  • ^ (Exclusive OR Operator) compares each bit of the two integer values and returns a new integer value based on XOR Operation on the bits.

Logical Operators: &&   ||

  • && (Logical AND Operator) compares two boolean values and returns a true value if both the boolean values are true. Otherwise it returns false.
  • || (Logical OR Operator) compares two boolean values and returns false if both the boolean values are false. Otherwise it returns true.

Ternary Operator:  ?

  • ? : (Ternary Operator) is a compact version of if then else construct.
  • For example: (boolean expression) ? value1 : value2 ;
  • For example: a = (x>y) ? x : y;
  • x and y should be of compatible type and a should be assignment compatible with both x and y.

Assignment Operator: =

  • op = ( Arithmetic assignment Operator) where op can be any binary non-boolean operator.
  • e.g  x *= y; is equivalent to  x = x * y ;

Loops and Decisions

For loops

For loop is used when the number of iterations are already known

for(init_expression ; test_condition ; iteration_expression)

{      //can be multiple statements;    }

While loops

while(test_condition)
{ //can be multiple statements;}

While loop executes as long as the test expression between parentheses is true. Make sure the statements within the body of the while loop eventually result in making the test condition as false.

do-while loops

Similar to while loop except that loop block is executed at least once, even if the test condition is false from the start.

do
{   
    //can be multiple statements;
}
while(test-condition);

if statement

If allows you to execute one group of statements depending on the result of a given comparison.

if(test_condition )
{
    //can be multiple statements;
}

If the test condition is true, the statements in the body of if are executed otherwise not.

if-else construct

Else provides a second choice of statement block that is executed when the test condition in if statement is false.

if(test_condition)
{              
    Statements; //executed when condition true
}
else
{
     Statements; //executed when condition false
}

switch construct

A switch construct is used to select from multiple choices based on a set of fixed values for a given expression.

switch(expression) 
{              
case 1:
  statements;        
  break;
case 2:
  statements ;       
  break;
default:
  statements ;     
  break;
}

Character Data Type

char letter = 'A'; (ASCII)      
char numChar = '4'; (ASCII)
char letter = '\u0041'; (Unicode hexadecimal)
char numChar = '\u0034'; (Unicode hexadecimal)

NOTE: The increment and decrement operators can also be used on char variables to get the next or preceding Unicode character. For example, the following statements display character b.

char ch = 'a';
System.out.println(++ch);

Java characters use Unicode, a 16-bit encoding scheme established by the Unicode Consortium to support the interchange, processing, and display of written texts in the world’s diverse languages. Unicode takes two bytes, preceded by \u, expressed in four hexadecimal numbers that run from ‘\u0000′ to ‘\uFFFF’.

Tagged with: JAVAObject OrientedProgramming
 

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.