Lecture 03 Operators in Java-3437
Lecture 03 Operators in Java-3437
Introduction
Operators are specific symbols in Java that are the basic building blocks of any language
used to perform operations on variables and values. Java provides different operators that
can be categorised into other groups based on functionality. Here are some operators
discussed below.
1. Arithmetic Operators
Types:
● ‘+’ Addition: adds two numbers and concatenates two string literals.
String a= "hello";
String b ="world";
String sentence =a+b //Assigns value "helloworld" to sentence
Note - '+' operator works in two ways while using numbers it performs
addition while with Strings it performs concatenation.
As the name suggests, assignment operators assign values to variables that are
fundamental to programming. The right side operand of the assignment operator is a value,
and the left side operand is a variable. Assignment operators follow right-side associativity,
i.e., expressions are evaluated from right to left.
int x = 4;
int x = x + 5; //value of x will change from 4 to 10.
x -= 2; // x = 2
x *= 4; // x = 16
x /= 2; // x = 2
3. Precedence:
Precedence determines the order in which operators are evaluated in an expression. When
an expression contains multiple operators, Java follows a specific set of rules to determine
which operation should first be performed. Below is the table with decreasing order of
precedence, i.e. the operator above the other operators is given a higher preference than the
one below.
Operators Precedence
Multiplicative * / %
Additive + -
Equality == !=
Bitwise Exclusive OR ^
Bitwise Inclusive OR |
Logical OR ||
Ternary ?:
4. Implicit Conversions:
Implicit conversion, also known as type promotion, is the automatic conversion of one data
type to another by the Java compiler without writing any extra instructing code. We’ll now
see some string operations depicting conversion.
● Implicit conversion with strings occurs when we combine strings with other data
types, like numbers.
● Java can automatically convert these non-string data types into strings, so they play
nicely together.
Here, the Java compiler identified that we were mixing the variables of different data types;
therefore, it converted int to string implicitly, concatenated with the string literals and stored it
in ‘sentence’.
int n1 = 10;
double n2 = 3.5;
Note - Implicit conversion is only allowed while converting shorter datatypes to larger data
types as it is possible to accommodate smaller data types to larger ones, which is also
known as widening.
class Conversion{
public static void main(String[] args) {
long a = 2.5;
int b = 4;
int c = a+b;
}
}
5. Type Casting:
Typecasting refers to converting one data type to another. Two common types of typecasting
are implicit and explicit casting.
● Implicit: Implicit type casting refers to converting a small data type into a larger one
by Java to prevent data loss. It is also known as widening or automatic type
conversion.
int a= 5;
double b= smaller; // Implicit casting from int to double
● Explicit: Explicit type casting refers to converting a large data type into a small one.
Explicit type casting may result in data loss and requires the programmer to specify
the type conversion explicitly. It is also known as narrowing or manual type
conversion.
double a= 10.5;
int b= (int) larger; // Explicit casting from double to int
● Use implicit casting when converting to a larger data type to prevent data loss or
perform operations involving mixed data types.
● Use explicit casting when converting to a smaller data type, but be cautious about
potential data loss. Explicit casting allows you to control the conversion.
● Explicit casting requires manual intervention and may result in data loss.
● Choose the appropriate casting method based on your data type conversion needs.
6. Unary Operators:
In Java, unary operators are those operators that perform operations on a single operand
used to manipulate values in different ways.
● Unary Plus (+) Operator: This operator doesn't change any value; it just states that
the number is positive.
int x = +5;
● Unary Minus (-) Operator: The minus operator negates the value. It turns positive
numbers into negative ones and vice versa.
int x = -5;
● Increment Operator (++): The operator increases the value of a variable by 1. It can
be used in two ways: pre-increment and post-increment.
int a = 10;
System.out.println(a++);
int a = 10;
System.out.println(++a);
//increments the value of 'a' by 1 first and then prints 'a' i.e. 11.
● Decrement Operator (--): The decrement operator decrements the value by 1. Just
like the increment operator, it can also be used in two ways, i.e. pre-decrement and
post-decrement
int a = 10;
System.out.println(a--);
int a = 10;
System.out.println(--a);
//decrements the value of 'a' by 1 first and then prints 'a' i.e. 9.
● Logical Not Operator (!): The logical not operator reverses the value of a boolean
expression. True becomes false and vice-versa.
//prints false
7. Bitwise Operators:
Bitwise operators are used in Java to manipulate individual data bits in integers or longs.
These operators allow you to perform low-level operations on binary representations of data.
● The Bitwise AND operator ( ‘&’ ): The bitwise AND operator (&) performs a bitwise
AND operation between corresponding bits of two integers. It results in a 1 only if
both bits being compared are 1. For example,
● Right shift operator ( ‘<<’ ): The right shift operator (>>) shifts the bits of an integer
to the right by a specified number of positions. It effectively divides the number by 2,
raised to the power of the shift amount.
8. Relational Operators:
Relational operators in Java are used to compare two values and determine their
relationship. These operators return a boolean result, indicating whether the specified
condition is true or false.
● Equality Operator (‘==’): The equality operator compares two values for equality by
checking if the values on both sides are equal and returns ‘true’ if the values are
equal and ‘false’ otherwise.
int x = 5;
int y = 5;
boolean isEqual = (x == y); // isEqual = true
● Inequality Operator(!=): The inequality operator ( != ) checks if two values are not
equal and returns true if the values are different and false if they are equal.
int a = 10;
int b = 5;
boolean isNotEqual = (a != b); // isNotEqual = true
● Greater Than Operator (>): The greater than operator ( > ) checks if the value on
the left is greater than the value on the right. Returns true if the condition is met;
otherwise, it returns false.
int num1 = 8;
int num2 = 5;
boolean isGreaterThan = (num1 > num2); // isGreaterThan = true
● Less Than Operator (‘<’): The less than operator checks if the value on the left is
less than the value on the right and returns true if the condition is true; otherwise, it
returns false.
● Greater Than or Equal To Operator (>=): The greater than or equal to operator
checks if the value on the left is greater than or equal to the value on the right and
returns true if the condition is true. Otherwise, it returns false.
● Less Than or Equal To Operator (<=): The less than or equal operator checks if
the value on the left is less than or equal to the right and returns true if the condition
is true; otherwise, it returns false.
Logical operators in Java are used to perform logical operations on boolean values or
conditions. They help make decisions and control the flow of your program based on the
truth values of expressions.
● The logical AND operator combines two boolean expressions and returns true if both
expressions on its left and right sides are valid.
● If any of the expressions is false, the result will be false.
2. Logical OR Operator( || ):
● The logical OR operator combines two boolean expressions and returns true if at
least one of the expressions on its left and right sides is true.
● If both expressions are false, the result will be false.
In conclusion, Java provides a variety of operators for performing different operations within
Java programs. Here's a summary of the operators we studied:
These operators are fundamental to Java programming and are essential for performing
calculations, making decisions, and controlling the flow of your code. Mastery of these
operators is crucial for developing efficient and functional Java applications.