Java Syntax World – Examples & Explanation of Top Java Operators

If you are a Java developer or learning to become one, then this article will be very helpful for you. In this article, we are supposed to cover the top java operators that are very useful for the developers in their daily life. Moreover, we are going to share the utilization of these java operators with the help of one functional example.

Operators in JavaScript are the unique symbols which carry out operations on operands. Such as ‘+’ is a symbol which performs the operation of addition. So, let’s just learn some of the cool Java operators;

Assignment operators

The assignment operators are used to assign the particular value to a variable. Such as;

int age;

age = 5;

Here the assignment operator assigned the value on its right variable to the left variable   In this example, 5 is assigned to the variable age with the help of a = operator. There are plenty of other assignment operators available. However, to keep the tutorial simple we will learn about them later and in this post, we will check out the example of the = operator only.

Practical use; Input

class AssignmentOperator

{

   public static void main(String[] args) {

       

       int number1, number2;

       

       // Assigning 5 to number1

       number1 = 5;

       System.out.println(number1);

               

       // Assigning value of variable number2 to number1

       number2 = number1;

       System.out.println(number2);

   }

}

Output

5

5

Arithmetic operators

As the name suggests, Arithmetic operators are used to perform the mathematical operations like addition, subtraction, etc., There are different operators are dedicated to each mathematical function such as addition (+), subtraction (-), multiplication (*), division (/), remainder (%).

Practical use; Input

class ArithmeticOperator {

   public static void main(String[] args) {

       

       double number1 = 12.5, number2 = 3.5, result;

       

       // Using addition operator

       result = number1 + number2;

       System.out.println(“number1 + number2 = ” + result);

       

       // Using subtraction operator

       result = number1 – number2;

       System.out.println(“number1 – number2 = ” + result);

       

       // Using multiplication operator

       result = number1 * number2;

       System.out.println(“number1 * number2 = ” + result);

 

       // Using division operator

       result = number1 / number2;

       System.out.println(“number1 / number2 = ” + result);

       

       // Using remainder operator

       result = number1 % number2;

       System.out.println(“number1 % number2 = ” + result);

   }

}

Output

number1 + number2 = 16.0

number1 – number2 = 9.0

number1 * number2 = 43.75

number1 / number2 = 3.5714285714285716

number1 % number2 = 2.0

Unary operator

Unary operators are those operators which only perform one operant operation. There are plenty of of different unary operators are available such as;

+ unary operator is not necessary to use since numbers are positive without using it
Unary operator inverts the sign of an expression
++ increment operator use to increments value by 1
‘—‘ decrement operator used to decrements value by 1
! Logical complement operator use to inverts the value of a Boolean

Practical use; Input

class UnaryOperator {

   public static void main(String[] args) {

       

       double number = 5.2, resultNumber;

       boolean flag = false;

       

       System.out.println(“+number = ” + +number);

       // number is equal to 5.2 here.

       

       System.out.println(“-number = ” + -number);

       // number is equal to 5.2 here.

       

       // ++number is equivalent to number = number + 1

       System.out.println(“number = ” + ++number);

       // number is equal to 6.2 here.

 

       // — number is equivalent to number = number – 1

       System.out.println(“number = ” + –number);

       // number is equal to 5.2 here.

 

       System.out.println(“!flag = ” + !flag);

       // flag is still false.

   }

}

Output

 

+number = 5.2

-number = -5.2

number = 6.2

number = 5.2

!flag = true

We have to cut short here, the rest java operators will be explained in the next article in the series.

Leave a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href=""> <abbr> <acronym> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Send a Message