Java : Operators And Expressions

Java : Operators And Expressions

- VINAYAK PANCHAL

Introduction

Knowingly or unknowingly, we are all familiar what operators are ! We all have done mathematical operations at least once in our lives. Operators are basic building blocks of logic in programming language. Then what are expressions? An expression is a statement that can convey a value. Some of the most common expressions are mathematical, such as adding two values. This article will help you understand further more about them. So without any further ado

LetsGetStartedSaturdayNightLiveGIF.gif

Operators

Operators are used to perform operations on Variable and values. For example :-

DATA TYPE (3).png

There are various types of operators :-

✅ Arithmetic Operators ( + , - , * , / , % , ++ , -- )

✅ Assignment Operators ( = , += , -+ , *= )

✅ Comparison Operators ( == , >= , <= )

✅ Logical Operators ( && , || , ! )

✅ Bitwise Operators ( & , | )

📌Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

Untitled54545.png

Remember :- Arithmetic operators cannot work with boolean. Modulo operator (%) can work on floats and doubles.

📌Assignment Operators

These operators are used to add, sub, multiply, etc. the assigned value to the variable.

DATA TYPE (4).png

For example:

DATA TYPE (5).png

The addition assignment operator (+=) adds a value to a variable!

📌Comparison operator

Comparison operators are used to compare two values. Various types of comparison operators are as follows:-

DATA TYPE (6).png

In the below examples, I used greater than comparison operator '>' and it returns "True" value because 5 is greater than 3.

DATA TYPE (7).png

📌Logical Operators

Logical operators are used to determine the logic between two variables.

DATA TYPE (8).png

In the below example, I used Logical AND operation and I got "True" in return because 5 is greater than 3 and 5 is less than 10. 👇🏻

DATA TYPE (9).png

📌Bitwise Operator

Bitwise operators are used to performing the manipulation of individual bits of a number. They can be used with any integral type (char, short, int, etc.). This topic is so big that I have to write a separate article on this, if your in hurry please google it or you can read my article when I'll post it. ( I recommend you to google as topic should be covered quickly and should not be postponed😊)

Precedence of operators

The operators are applied and evaluated based on precedence. Like for example, "+" has a less precedence as compared to say "*" and "/". So Multiplication and division says to addition and subtraction that we are bigger than you so we will operate first. For example 👇🏻

DATA TYPE (10).png

In the above example, it went division first and then subtraction and we get 1 as answer.

Associativity of operators

Multiplication and division has same precedence so how do we evaluate when both come in one equation? Here the law of associativity comes into picture. Law of associativity acts from left to right. Let us see this with an example👇🏻

DATA TYPE (11).png

But you may ask, Vinayak how to do we know that which has greater associativity? (If they have same precedence) Don't worry my friend here you go⬇️

precedence and associativity.gif

I got this from code with Harry's YouTube channel so thanks to him for making this. So, coming back to our topic, first check the precedence and then associativity. So, if there is addition and multiplication then multiplication will operate first and then addition. But if there are division and multiplication then check the associativity column and accordingly get to your answer. Remember, associativity law operate from left to right or right to left so operate in that order accordingly.

Associativity tells us direction of execution. It can be either from left to right or right to left.

'*' and '/' then it is L to R

'++' and '=' then it is R to L

Increment and decrement operators

✅Increment Operators are i++ and ++i

✅Decrement Operators are i-- and --i

Lets understand this for increment operators first and same is for decrement operators only difference is that in increment we will add and in decrement we will subtract.

So when we use " i ++ " the value will be first printed and then it will be incremented by 1. Please remember here that in place of i you can take any variable name. Have a look at the example given below⬇️

DATA TYPE (12).png

So, first we get 3 and then new value is printed as 4. Now, let us understand what "++i" does? It first increments and then it prints! Have a look below ⬇️

DATA TYPE (13).png

Here in the above example of ++i, it is first incremented and then the new value is printed!

Same goes for i-- and --i, only difference is that we subtract here.

THANKS FOR READING MY ARTICLE📄 HOPE THAT IT ADDS VALUE⤴️

FOLLOW ME ON HASHNODE VINAYAK PANCHAL