0% found this document useful (0 votes)
84 views7 pages

Java Basic Operators Explained

The document discusses the different types of operators in Java, including arithmetic, relational, logical, bitwise, and assignment operators. It provides examples of using each operator and explains their functionality. The chapter also covers unary operators, conditional operators, instanceof operator, and the dot operator for accessing members of a class.
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)
84 views7 pages

Java Basic Operators Explained

The document discusses the different types of operators in Java, including arithmetic, relational, logical, bitwise, and assignment operators. It provides examples of using each operator and explains their functionality. The chapter also covers unary operators, conditional operators, instanceof operator, and the dot operator for accessing members of a class.
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

Chapter 3- Java Basic Operators

Operator in java is a symbol that is used to perform operations.


1. Arithmetic Operators
2. Unary Arithmetic Operators
3. Relational Operators
4. Logical Operators
5. Bitwise Operators
6. Assignment Operators
7. Compound Assignment Operators
8. Conditional Operator
9. instanceof Operator
[Link] Selection or Dot Operator

3.1 Arithmetic Operators


Java arithmetic operators are used to perform addition, subtraction,
multiplication and division. They act as basic mathematical operations.

Operator Name Description Example

+ Addition Adds together two values x+y

- Subtraction Subtracts one value from another x-y

* Multiplication Multiplies two values x*y

/ Division Divides one value by another x/y

% Modulus Returns the division remainder x % y

Arithmetic Application Program:


public class ArithmeticApplication {

public static void main(String[] args) {

int num1 = 20, num2 = 10, result;

result = num1 + num2;

[Link]("\n Addition Result: " + result);

result = num1 - num2;

[Link]("\n Substraction Result: " + result);

result = num1 * num2;

[Link]("\n Multiplication Result: " + result);

result = num1 / num2;

[Link]("\n Division Result: " + result);

Output:

Addition Result: 30

Substraction Result: 10

Multiplication Result: 200

Division Result: 2

Description: Here class name is ArithmeticApplication and it start first letter


Capital. Inside main method created 3 variables- num1, num2 and result declared
in int datatype.
result = num1 + num2;

here addition of 2 number data stored in result variable

[Link]("\n Addition Result: " + result);

By using [Link] will display the result in Java console.

3.2 Unary Arithmetic Operators


The Java unary operators require only one operand. Unary operators are used to
perform various operations i.e.:

incrementing/decrementing a value by one

++ Increment Increases the value of a variable by 1

Ex. ++x

negating an expression

-- Decrement Decreases the value of a variable by 1

Ex. --x

inverting the value of a boolean

3.3 Relational Operators


These operators are used to check for relations like equality, greater than, less
than. They return boolean result after the comparison and are extensively used in
looping statements as well as conditional if else statements

== Equal to x == y
!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y

3.4 Logical Operators

These operators are used to perform “logical AND” and “logical OR” operation,
i.e. the function similar to AND gate and OR gate in digital electronics. One thing
to keep in mind is the second condition is not evaluated if the first one is false, i.e.
it has a short-circuiting effect. Used extensively to test for several conditions for
making a decision.

&& Logical and Returns true if both statements are true

Ex. x < 5 && x < 10

|| Logical or Returns true if one of the statements is true

Ex. x < 5 || x < 4

! Logical not Reverse the result, returns false if the result is true

Ex. !(x < 5 && x < 10)

3.5 Bitwise Operators


These operators are used to perform manipulation of individual bits of a number.
They can be used with any of the integer types. They are used when performing
update and query operations of Binary indexed tree.

Operators Symbol Uses


Bitwise AND & op1 & op2

Bitwise exclusive OR ^ op1 ^ op2

Bitwise inclusive OR | op1 | op2

Bitwise Compliment ~ ~ op

Bitwise left shift << op1 << op2

Bitwise right shift >> op1 >> op2

Unsigned Right Shift Operator >>> op >>> number of places to shift

3.6 Assignment Operators


‘=’ Assignment operator is used to assign a value to any variable. It has a right to
left associativity, i.e value given on right hand side of operator is assigned to the
variable on the left and therefore right hand side value must be declared before
using it or should be a constant.

3.7 Compound Assignment Operators


In many cases assignment operator can be combined with other operators to
build a shorter version of statement called Compound Statement. For example,
instead of a = a+5, we can write a += 5.

3.8 Conditional Operator


The Java Conditional Operator selects one of two expressions for evaluation,
which is based on the value of the first operands. It is also called ternary operator
because it takes three arguments.

Syntax: expression1? expression2:expression3;

public class ConditionalOperator {


public static void main(String[] args) {

int n1 = 5, n2 = 11, n3 = 7;

[Link]("\n Largest Number: " + ((n1 > n2) ? (n1 > n3 ? n1


: n3) : (n2 > n3 ? n2 : n3)));

Output:

Largest Number: 11

3.9 instanceof Operator


The Java instanceof Operator is used to determining whether this object belongs
to this particular (class or subclass or interface) or not.

class Employee {

public class InstanceOfOperator {

public static void main(String[] args) {


Employee e1 = new Employee();

[Link](e1 instanceof Employee);

Output:

true

3.10 Member Selection or Dot Operator


A dot operator is used to refer members of a class using classname or object

Example: Customer customer = new Customer();

[Link];

[Link]();

Common questions

Powered by AI

Java bitwise operators perform operations at the bit level, allowing manipulation of binary representations. They include AND (&), OR (|), XOR (^), complement (~), left shift (<<), and right shift (>>). These operators are significant in optimizing performance, encoding, decoding, or compressing data, as they directly manipulate the raw binary data .

The conditional operator, or ternary operator, is useful for concise conditional assignments. It evaluates a boolean expression and returns one of two values. For instance, to find the larger of two numbers: `int max = (a > b) ? a : b;`. This operator is particularly useful for simplifying if-else conditions into single-line statements .

The instanceof operator checks if an object belongs to a specific class or interface. It's particularly useful for preventing class cast exceptions. For example, if an object `Employee e1` needs to be checked, `e1 instanceof Employee` returns true, ensuring `e1` is indeed an instance of the `Employee` class before casting or accessing its properties .

Short-circuiting in Java logical operators affects evaluation by potentially skipping the evaluation of the second operand. In && (logical AND), if the first operand is false, the second is not evaluated since the operation's result is already determined. Similarly, in || (logical OR), if the first operand is true, the second is not evaluated .

Compound assignment operators in Java combine two operations into one, such as += for addition and assignment. They streamline coding by reducing redundancy, making code easier to read and write. They are often preferred for conciseness and clarity, as they succinctly convey the modification of a variable .

The assignment operator in Java displays right-to-left associativity by assigning the right-hand side value to the left-hand side variable. This means the value or expression on the right must be evaluated and ready before assignment. For example, `int x = 5;` stores the value on the right in the variable on the left .

Relational operators in Java check the relationship between two operands and return a boolean value. They are crucial in control structures, assisting in decision-making processes. For example, the '>' operator checks if an operand is greater than another, as used in if statements: if (x > y). This will execute the block of code only when x is greater than y .

Arithmetic operators in Java perform basic mathematical functions like addition, subtraction, multiplication, and division on two operands. For example, in a program, when these operators are applied to integers num1 = 20 and num2 = 10, the results are as follows: addition gives 30, subtraction results in 10, multiplication yields 200, and division produces 2 .

The member selection or dot operator (.) is used in Java to access the members of a class, such as methods and variables. For instance, given an object `Customer customer`, the operator allows access to a method like `customer.showCustomerList()`, or a variable such as `customer.customerName`, indicating their association with the `customer` object .

Java's unary arithmetic operators require only one operand and are used to increment, decrement, or negate a value. The increment operator (++), for instance, adds one to a variable, as in ++x, while the decrement operator (--) subtracts one, as in --x. These operations modify the variable value by one .

You might also like