Operators in java with Example Programs

Price: INR 8,999.00
Price: INR 14,999.00
Was: INR 16,999.00

Operators in Java are used to perform the operation on operands and produce a value. An operand could be anything like primitive, non-primitive and object.


Assignment operator

The assignment operator (=) is a way of copying a value on the right-hand side, to a variable on the left-hand side. The right-hand value can be constant, variable, or expression that produces a value, while the left-hand side must be a distinctly named Variable. For example, we may assign a value 40 to a variable “i” in the following way:-

i = 40;

However, vice versa is not possible you can’t use it like  40 = i; it’s not acceptable in Java.


Arithmetic operators

Arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/) and the modulus operator (%) which produces the remainder from an integer division.

The following provides an example of the use of arithmetic operators:-


Unary operators

Unary operators work on only one operand. The increment operator (++) increments a value by 1, while the decrement operator (–) decreases a value by 1. The increment and decrement operators, in addition to modifying a variable in Java, also produce the value of the modified variable as a result.

Prefix – In prefix operation is performed first then result produced. (i.e ++i)

Postfix – In postfix first result process then operation performed. (i.e i++)

Finally, the logical operator (!) inverts the value of a boolean expression. You may find this referred to as the logical or conditional NOT (!) operator.Here is an example that demonstrates the use of unary operators:-


Relational operators in Java

Relational operators evaluate the values of two operands and produce a boolean result i.e.true or false. They include less than (<), greater than (>), less than or equal to (<=), greater than or equal to (>=), equivalence (==), and nonequivalence (!=). With the exception of the equivalence and nonequivalence operators, all other relational operators work with all primitives except boolean.


The equivalence and non-equivalence operators, in addition to working with all primitives, also work with objects.


Conditional Operators

The Conditional are AND (&&) and OR (||), produce a boolean result of true or false based on the logical relationship of its arguments.

Ternary if-else operator in Java

The ternary operator “?:” is the only operator to take three operands. It is a conditional operator that provides a shorter syntax for the if..then..else statement. The first operand is a boolean expression; if the expression is true then the value of the second operand is returned otherwise the value of the third operand is returned:

boolean expression ? value1 : value2


The following if..then..else statement:

boolean car = true;

String model;

if (car == true) {

mood = “Audi A3”;

} else {

mood = “Not Available”;


can be reduced to one line using the ternary operator:

boolean car = true;

String mood = (car == true)?”Audi A3″:”Not Available”;


TypeCasting in Java

Casting is a way to convert from one type to another, for instance from a float to a double. Java is able to perform these sorts of cases automatically. To perform a cast, you simply put the desired type between parenthesis and to the left of any value you wish to cast.

Have a look following example:-


Selenium Testing Video Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply