Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Chapter 3 - Java Basic Operators

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
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Chapter 3 - Java Basic Operators

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
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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
10.Member 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;

System.out.println("\n Addition Result: " + result);

result = num1 - num2;

System.out.println("\n Substraction Result: " + result);

result = num1 * num2;

System.out.println("\n Multiplication Result: " + result);

result = num1 / num2;

System.out.println("\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

System.out.println("\n Addition Result: " + result);

By using System.out.println 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;

System.out.println("\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();

System.out.println(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();

customer.customerName;

customer.showCustomerList();

You might also like