0% found this document useful (0 votes)
1 views9 pages

operator in java

The document provides an overview of various operators in Java, categorized into arithmetic, relational, logical, and assignment operators. It explains the usage and behavior of these operators, including examples of arithmetic operations, compound assignments, and logical evaluations. Additionally, it covers operator precedence and the use of parentheses for clarifying expressions.

Uploaded by

shivapur.cs
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)
1 views9 pages

operator in java

The document provides an overview of various operators in Java, categorized into arithmetic, relational, logical, and assignment operators. It explains the usage and behavior of these operators, including examples of arithmetic operations, compound assignments, and logical evaluations. Additionally, it covers operator precedence and the use of parentheses for clarifying expressions.

Uploaded by

shivapur.cs
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
You are on page 1/ 9

Operators

Java provides rich set of operators, mainly divided into four groups viz. arithme c, bitwise, rela onal
and logical. These operators are discussed here.

Arithme c Operators
Arithme c operators are used in mathema cal expressions in the same way that they are used in
algebra. The following table lists the arithme c operators:

Operator Meaning

+ Addi on

- Subtrac on
* Mul plica on

/ Division

% Modulus

++ Increment

-- Decrement

+= Addi on assignment

-= Subtrac on assignment

*= Mul plica on assignment

/= Division assignment
%= Modulus assignment

The operands of the arithme c operators must be of a numeric type. You cannot use them on
Boolean types, but you can use them on char types, since the char type in Java is a subset of int.
Note down following few points about various operators:

 Basic arithme c operators like +, -, * and / behave as expected for numeric data.
 The – symbol can be used as unary operator to negate a variable.
 If / is operated on two integer operands, then we will get only integral part of the result by
trunca ng the frac onal part.

public class Main {


public static void main(String[] args) {
int a = 10;
int b = 5;
// Addition
int sum = a + b;
System.out.println("a + b = " + sum);

// Subtraction
int difference = a - b;
System.out.println("a - b = " + difference);

// Multiplication
int product = a * b;
System.out.println("a * b = " + product);

// Division
int quotient = a / b;
System.out.println("a / b = " + quotient);

}
}
This program declares two integer variables a and b and performs arithmetic operations on them
using the following operators:

 + for addition
 - for subtraction
 * for multiplication
 / for division

The output of the program is:


a + b = 15
a-b=5
a * b = 50
a/b=2

MODULUS OPERATOR
 The % operator returns the remainder a er division. It can be applied on integer and floa ng-
point types. For example,

int x=57;

double y= 32.8;

System.out.println(“on integer “ + x%10); OUTPUT 7

System.out.println(“on double “ + y%10); OUTPUT 2.8


ARITHMETIC COMPOUND ASSIGNMENT OPERATOR
 Compound assignment operators like += will perform arithme c opera on with assignment.
 Syntax

var op = expression

Arithme c compound assignment operator example a+=2;

is same as a=a+2;

//Demonstrate of Arithme c compound assignment operator


public class CompoundAssignmentExample {
public static void main (String [] args) {
int a = 5;
int b = 10;
int c = 7;
int d = 20;
int e = 17;

a += 3; // equivalent to a = a + 3;
b -= 4; // equivalent to b = b - 4;
c *= 2; // equivalent to c = c * 2;
d /= 5; // equivalent to d = d / 5;
e %= 5; // equivalent to e = e % 5;

System.out.println ("a: " + a); // Output: a: 8


System.out.println ("b: " + b); // Output: b: 6
System.out.println ("c: " + c); // Output: c: 14
System.out.println ("d: " + d); // Output: d: 4
System.out.println ("e: " + e); // Output: e: 2
}
}
INCREMENT DECREMENT OPERATOR
 Increment/decrement operators (++ and -- ) will increase/decrease the operand by 1. That is,

a++; a=a+1;

b--; b=b-1;

 The ++ and -- operators can be used either as pre-increment/decrement or post-


increment/decrement operator. For example,
x= 5;
y=x++; //post increment
Now, value of x (that is 5) is assigned to y first, and x is then incremented to become 6.
x= 5;
y=++x; //pre-increment
Now, x is incremented to 6 and then 6 is assigned to y.
//Demonstrate program of increment decrement operator.

class IncrementDecrement {
public sta c void main(String[] args) {
int x = 10;
System.out.println("Ini al value of x: " + x);

// Pre-increment operator
++x;
System.out.println("Value of x a er pre-increment: " + x);

// Post-increment operator
x++;
System.out.println("Value of x a er post-increment: " + x);

// Pre-decrement operator
--x;
System.out.println("Value of x a er pre-decrement: " + x);

// Post-decrement operator
x--;
System.out.println("Value of x a er post-decrement: " + x);
}
}
OUTPUT
Ini al value of x: 10
Value of x a er pre-increment: 11
Value of x a er post-increment: 12
Value of x a er pre-decrement: 11
Value of x a er post-decrement: 10

NOTE :-That in C/C++, the % operator cannot be used on float or double and should be used only
on integer variable.

Rela onal Operators


The rela onal operators determine the rela onship between two operands. Specifically, they
determine equality and ordering among operands. Following table lists the rela onal operators
supported by Java.

Operator Meaning
== Equal to (or comparison)
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

The outcome of these opera ons is a boolean value. Any type in Java, including integers, floa ng-
point numbers, characters, and Booleans can be compared using the equality test, ==, and the
inequality test, !=. Only numeric types can be compared using the ordering operators. That is, only
integer, floa ng-point, and character operands may be compared to see which is greater or less than
the other. For example, the following code fragment is perfectly valid:

int a = 4;

int b = 1;

boolean c = a < b;

In this case, the result of a<b (which is false) is stored in c.

//Example program
class RelationalOperators {
public static void main(String[] args) {
int num1 = 5, num2 = 10;
System.out.println("num1 > num2: " + (num1 > num2));
System.out.println("num1 < num2: " + (num1 < num2));
System.out.println("num1 >= num2: " + (num1 >= num2));
System.out.println("num1 <= num2: " + (num1 <= num2));
System.out.println("num1 == num2: " + (num1 == num2));
System.out.println("num1 != num2: " + (num1 != num2));
}
}

The output of the program will be:


num1 > num2: false
num1 < num2: true
num1 >= num2: false
num1 <= num2: true
num1 == num2: false
num1 != num2: true

Boolean 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 Meaning

& 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
The truth table is given below for few opera ons:

Program Demonstra on of Boolean Logical operators

class BoolLogic

public sta c void main(String args[])

{
int h= 12;
int i= 9;
int j = 0b1010;
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;
h &= 5; // equivalent to h = h & 5;

i |= 6; // equivalent to i = i | 6;

j ^= 0b1100; // equivalent to j = j ^ 12;

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

boolean h = b & (a=!a);

System.out.println("b & (a=!a) =" +h);

System.out.println("New a is "+a);
System.out.println ("h: " + h);
System.out.println ("i: " + i);
System.out.println ("j: " + j);
}

The output would be –

a = true

b = false

a|b = true

a&b = false

a^b = true

!a&b|a&!b = true

!a = false

b & (a=!a) =false

New a is false
h: 4
i: 15
j: 6

Short-Circuit Logical Operators


The short-circuit AND (&&) and OR (||) operators will not evaluate the second operand if the first is
decisive. For example,
public class ShortCircuitAndOperatorExample {
public static void main(String[] args) {
int a = 10;
int b = 5;
if (a > 5 && b < 10) {
System.out.println("Both conditions are true.");
} else {
System.out.println("At least one condition is false.");
}
}
}
Output:
Both condi ons are true.

In this program, we have variables "a" and "b". We use the "&&" operator to test if "a" is more than five and "b"
is less than 10. If each situa on is true, then we print "Both situa ons are true." Otherwise, we print "At least
one condi on is false."

The Assignment Operator


The assignment operator is the single equal sign, =. It has this general form:

var = expression;

Here, the type of var must be compa ble with the type of expression. It allows you to create a chain
of assignments. For example, consider this fragment:

int x, y, z;

x = y = z = 100; // set x, y, and z to 100

This fragment sets the variables x, y, and z to 100 using a single statement. This works because the =
is an operator that yields the value of the right-hand expression. Thus, the value of z = 100 is 100,
which is then assigned to y, which in turn is assigned to x. Using a “chain of assignment” is an easy
way to set a group of variables to a common value.

The ? : Operator
Java supports ternary operator which some mes can be used as an alterna ve for if-then-else
statement.

The general form is

var = expression1 ? expression2 : expression3;

Here, expression1 is evaluated first and it must return Boolean type. If it results true, then value of
expression2 is executed else expression3 is executed

//Example program

Int feb=28;

String res;

Res=(feb==29)? ”leap year” : ”not leap year”

Output:

not leap year

Operator Precedence
Following table describes the precedence of operators. Though parenthesis, square brackets etc. are

separators, they do behave like operators in expressions. Operators at same precedence level will be
evaluated from le to right, whichever comes first.

Using Parentheses
Parentheses always make the expression within them to execute first. This is necessary some mes.
For example,

a= b – c * d;

Here, c and d are mul plied first and then the result is subtracted from b. If we want subtrac on first,
we should use parenthesis like

a= (b-c)*d;

Some mes, parenthesis is useful for clarifying the meaning of an expression and for making readers
to understand the code. For example,

a | 4 + c >> b & 7 can be wri en as (a | (((4 + c) >> b) & 7))

In such situa ons, though parenthesis seems to be redundant, it existence will not reduce the

performance of the program.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy