0% found this document useful (0 votes)
2 views10 pages

JavaTutorial 51 60

The document provides an overview of various operators in Java, including binary, logical, assignment, and miscellaneous operators, along with examples for each type. It also explains operator precedence and introduces looping mechanisms such as while, do...while, for, and enhanced for loops. The document concludes with examples demonstrating the use of these operators and loops in Java programming.

Uploaded by

amitbod021998
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

JavaTutorial 51 60

The document provides an overview of various operators in Java, including binary, logical, assignment, and miscellaneous operators, along with examples for each type. It also explains operator precedence and introduces looping mechanisms such as while, do...while, for, and enhanced for loops. The document concludes with examples demonstrating the use of these operators and loops in Java programming.

Uploaded by

amitbod021998
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Operator Description Example

Binary AND Operator copies a bit to the result if it


& (A & B) will give 12 which is 0000 1100
exists in both operands.

Binary OR Operator copies a bit if it exists in either


| (A | B) will give 61 which is 0011 1101
operand.

Binary XOR Operator copies the bit if it is set in


^ (A ^ B) will give 49 which is 0011 0001
one operand but not both.

Binary Ones Complement Operator is unary and


~ (~A ) will give -60 which is 1100 0011
has the effect of 'flipping' bits.

Binary Left Shift Operator. The left operands value


<< is moved left by the number of bits specified by A << 2 will give 240 which is 1111 0000
the right operand.

Binary Right Shift Operator. The left operands


>> value is moved right by the number of bits A >> 2 will give 15 which is 1111
specified by the right operand.

Shift right zero fill operator. The left operands


value is moved right by the number of bits
>>> A >>>2 will give 15 which is 0000 1111
specified by the right operand and shifted values
are filled up with zeros.

Example
The following simple example program demonstrates the bitwise operators. Copy and paste the following Java
program in [Link] file and compile and run this program:

public class Test{

public static void main(String args[]){


int a =60; /* 60 = 0011 1100 */
int b =13; /* 13 = 0000 1101 */
int c =0;

c = a & b;/* 12 = 0000 1100 */


[Link]("a & b = "+ c );

c = a | b;/* 61 = 0011 1101 */


[Link]("a | b = "+ c );

c = a ^ b;/* 49 = 0011 0001 */


[Link]("a ^ b = "+ c );

c =~a;/*-61 = 1100 0011 */


[Link]("~a = "+ c );

c = a <<2;/* 240 = 1111 0000 */


[Link]("a << 2 = "+ c );

c = a >>2;/* 215 = 1111 */


[Link]("a >> 2 = "+ c );

c = a >>>2;/* 215 = 0000 1111 */


[Link]("a >>> 2 = "+ c );
}
}

TUTORIALS POINT
Simply Easy Learning
This would produce the following result:

a & b =12
a | b =61
a ^ b =49
~a =-61
a <<2=240
a >>15
a >>>15

The Logical Operators:


The following table lists the logical operators:

Assume Boolean variables A holds true and variable B holds false, then:

Operator Description Example

Called Logical AND operator. If both the operands are non-zero, then the
&& (A && B) is false.
condition becomes true.

Called Logical OR Operator. If any of the two operands are non-zero,


|| (A || B) is true.
then the condition becomes true.

Called Logical NOT Operator. Use to reverses the logical state of its
! !(A && B) is true.
operand. If a condition is true then Logical NOT operator will make false.

Example
The following simple example program demonstrates the logical operators. Copy and paste the following Java
program in [Link] file and compile and run this program:

public class Test{

public static void main(String args[]){


boolean a =true;
boolean b =false;

[Link]("a && b = "+(a&&b));

[Link]("a || b = "+(a||b));

[Link]("!(a && b) = "+!(a && b));


}
}

This would produce the following result:

a && b =false
a || b =true
!(a && b)=true

The Assignment Operators:


There are following assignment operators supported by Java language:

Operator Description Example

TUTORIALS POINT
Simply Easy Learning
Simple assignment operator, Assigns values
= C = A + B will assign value of A + B into C
from right side operands to left side operand

Add AND assignment operator, It adds right


+= operand to the left operand and assign the C += A is equivalent to C = C + A
result to left operand

Subtract AND assignment operator, It


-= subtracts right operand from the left operand C -= A is equivalent to C = C - A
and assign the result to left operand

Multiply AND assignment operator, It multiplies


*= right operand with the left operand and assign C *= A is equivalent to C = C * A
the result to left operand

Divide AND assignment operator, It divides left


/= operand with the right operand and assign the C /= A is equivalent to C = C / A
result to left operand

Modulus AND assignment operator, It takes


%= modulus using two operands and assign the C %= A is equivalent to C = C % A
result to left operand

<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2

>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2

&= Bitwise AND assignment operator C &= 2 is same as C = C & 2

^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2

|= bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2

Example:
The following simple example program demonstrates the assignment operators. Copy and paste the following Java
program in [Link] file and compile and run this program:

public class Test{

public static void main(String args[]){


int a =10;
int b =20;
int c =0;

c = a + b;
[Link]("c = a + b = "+ c );

c += a ;
[Link]("c += a = "+ c );

c -= a ;
[Link]("c -= a = "+ c );

c *= a ;
[Link]("c *= a = "+ c );

a =10;
c =15;
c /= a ;
[Link]("c /= a = "+ c );

TUTORIALS POINT
Simply Easy Learning
a =10;
c =15;
c %= a ;
[Link]("c %= a = "+ c );

c <<=2;
[Link]("c <<= 2 = "+ c );

c >>=2;
[Link]("c >>= 2 = "+ c );

c >>=2;
[Link]("c >>= a = "+ c );

c &= a ;
[Link]("c &= 2 = "+ c );

c ^= a ;
[Link]("c ^= a = "+ c );

c |= a ;
[Link]("c |= a = "+ c );
}
}

This would produce the following result:

c = a + b =30
c += a =40
c -= a =30
c *= a =300
c /= a =1
c %= a =5
c <<=2=20
c >>=2=5
c >>=2=1
c &= a =0
c ^= a =10
c |= a =10

Misc 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 iftrue: value iffalse

Following is the example:

public class Test{

public static void main(String args[]){


int a , b;

TUTORIALS POINT
Simply Easy Learning
a =10;
b =(a ==1)?20:30;
[Link]("Value of b is : "+ b );

b =(a ==10)?20:30;
[Link]("Value of b is : "+ b );
}
}

This would produce the following result:

Value of b is:30
Value of b is:20

instanceof Operator:
This operator is used only for object reference variables. The operator checks whether the object is of a particular
type(class type or interface type). instanceof operator is wriiten as:

(Object reference variable ) instanceof (class/interface type)

If the object referred by the variable on the left side of the operator passes the IS-A check for the class/interface
type on the right side, then the result will be true. Following is the example:

String name = “James”;


boolean result = name instanceof String;
// This will return true since name is type of String

This operator will still return true if the object being compared is the assignment compatible with the type on the
right. Following is one more example:

classVehicle{}

public class CarextendsVehicle{


public static void main(String args[]){
Vehicle a =newCar();
boolean result = a instanceofCar;
[Link](result);
}
}

This would produce the following result:

true

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.

TUTORIALS POINT
Simply Easy Learning
Category Operator Associativity

Postfix () [] . (dot operator) Left to right

Unary ++ - - ! ~ Right to left

Multiplicative */% Left to right

Additive +- Left to right

Shift >>>>><< Left to right

Relational >>= <<= Left to right

Equality == != Left to right

Bitwise AND & Left to right

Bitwise XOR ^ Left to right

Bitwise OR | Left to right

Logical AND && Left to right

Logical OR || Left to right

Conditional ?: Right to left

Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left

Comma , Left to right

What is Next?
Next chapter would explain about loop control in Java programming. The chapter will describe various types of
loops and how these loops can be used in Java program development and for what purposes they are being used.

TUTORIALS POINT
Simply Easy Learning
9
CHAPTER

Java Loop Control

T here may be a situation when we need to execute a block of code several number of times and is often

referred to as a loop.

Java has very flexible three looping mechanisms. You can use one of the following three loops:

 while Loop

 do...while Loop

 for Loop

As of Java 5, the enhanced for loop was introduced. This is mainly used for Arrays.

The while Loop:


A while loop is a control structure that allows you to repeat a task a certain number of times.

Syntax:
The syntax of a while loop is:

while(Boolean_expression)
{
//Statements
}

When executing, if the boolean_expression result is true, then the actions inside the loop will be executed. This will
continue as long as the expression result is true.
Here, key point of the while loop is that the loop might not ever run. When the expression is tested and the result is
false, the loop body will be skipped and the first statement after the while loop will be executed.

Example:
public class Test{

public static void main(String args[]){


int x =10;

while( x <20){
[Link]("value of x : "+ x );

TUTORIALS POINT
Simply Easy Learning
x++;
[Link]("\n");
}
}
}

This would produce the following result:

value of x :10
value of x :11
value of x :12
value of x :13
value of x :14
value of x :15
value of x :16
value of x :17
value of x :18
value of x :19

The do...while Loop:


A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.

Syntax:
The syntax of a do...while loop is:

do
{
//Statements
}while(Boolean_expression);

Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once
before the Boolean is tested.
If the Boolean expression is true, the flow of control jumps back up to do, and the statements in the loop execute
again. This process repeats until the Boolean expression is false.

Example:
public class Test{

public static void main(String args[]){


int x =10;

do{
[Link]("value of x : "+ x );
x++;
[Link]("\n");
}while( x <20);
}
}

This would produce the following result:

value of x :10
value of x :11
value of x :12
value of x :13

TUTORIALS POINT
Simply Easy Learning
value of x :14
value of x :15
value of x :16
value of x :17
value of x :18
value of x :19

The for Loop:


A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific
number of times.

A for loop is useful when you know how many times a task is to be repeated.

Syntax:
The syntax of a for loop is:

for(initialization;Boolean_expression; update)
{
//Statements
}

Here is the flow of control in a for loop:

 The initialization step is executed first, and only once. This step allows you to declare and initialize any loop
control variables. You are not required to put a statement here, as long as a semicolon appears.

 Next, the Boolean expression is evaluated. If it is true, the body of the loop is executed. If it is false, the body
of the loop does not execute and flow of control jumps to the next statement past the for loop.

 After the body of the for loop executes, the flow of control jumps back up to the update statement. This
statement allows you to update any loop control variables. This statement can be left blank, as long as a
semicolon appears after the Boolean expression.

 The Boolean expression is now evaluated again. If it is true, the loop executes and the process repeats itself
(body of loop, then update step,then Boolean expression). After the Boolean expression is false, the for loop
terminates.

Example:
public class Test{

public static void main(String args[]){

for(int x =10; x <20; x = x+1){


[Link]("value of x : "+ x );
[Link]("\n");
}
}
}

This would produce the following result:

value of x :10
value of x :11
value of x :12
value of x :13

TUTORIALS POINT
Simply Easy Learning
value of x :14
value of x :15
value of x :16
value of x :17
value of x :18
value of x :19

Enhanced for loop in Java:


As of Java 5, the enhanced for loop was introduced. This is mainly used for Arrays.

Syntax:
The syntax of enhanced for loop is:

for(declaration : expression)
{
//Statements
}

 Declaration: The newly declared block variable, which is of a type compatible with the elements of the array
you are accessing. The variable will be available within the for block and its value would be the same as the
current array element.
 Expression: This evaluates to the array you need to loop through. The expression can be an array variable or
method call that returns an array.

Example:
public class Test{

public static void main(String args[]){


int[] numbers ={10,20,30,40,50};

for(int x : numbers ){
[Link](x);
[Link](",");
}
[Link]("\n");
String[] names ={"James","Larry","Tom","Lacy"};
for(String name : names ){
[Link]( name );
[Link](",");
}
}
}

This would produce the following result:

10,20,30,40,50,
James,Larry,Tom,Lacy,

The break Keyword:


The break keyword is used to stop the entire loop. The break keyword must be used inside any loop or a switch
statement.

TUTORIALS POINT
Simply Easy Learning

You might also like