- Podcasts
- The Java Developers Hub
- Java Operators Explained | Arithmetic, Unary, Logical, Bitwise & Assignment Operators
Java Operators Explained | Arithmetic, Unary, Logical, Bitwise & Assignment Operators
The Java Developers Hub
- December 3, 2026
- 2 h 8 min
- Episode 15
About this episodeOperators are fundamental elements of Java programming that allow developers to perform calculations, compare values, make logical…
- Understanding operators and operands in Java.
- Introduction to arithmetic operators (+, -, *, /, %).
- Performing mathematical calculations using Java.
- Understanding unary operators (++, --, +, -, !).
- Working with logical operators (&&, ||, !).
- Understanding bitwise operators (&, |, ^, ~).
- Using bitwise shift operators (<<, >>, >>>).
- Understanding assignment operators (=, +=, -=, *=, /=).
- Comparing values using relational operators.
- Understanding operator precedence and associativity.
- Combining multiple operators in Java expressions.
- Practical examples of Java operators.
- Common operator mistakes and best practices.
- Beginners learning Java programming.
- Computer science and software engineering students.
- Developers transitioning to Java.
- Students preparing for programming examinations.
- Anyone interested in improving programming logic and problem-solving skills.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Java Operators Explained | Arithmetic, Unary, Logical, Bitwise & Assignment Operators
Yes, so let's start. Welcome back Coder Army to our Java series. And today we are going to read a new topic, Operators in Java. Before starting the topic, let me tell you again. Since we are working hard to make this playlist, I hope you also watch this playlist well. Read a video properly every day, understand it, make its notes. I will share my notes with you. But apart from that, I want you to make your own notes and share it on social media, on LinkedIn, on Twitter. Share it wherever you feel like. So that you keep making progress. Second, you can promise yourself that you want to watch videos every day and share them. You have to keep updating your progress on LinkedIn and Twitter like a diary.
Now whenever you will read any programming language, right? If you go one step ahead from a variable, then you will have to use operators. Like in mathematics, you use operators to do plus, minus, multiply, divide. So, you use it in any programming language. So, today we will first study the simplest type of operators, which are your arithmetic operators.
What does the modulus operator do? It divides like a divide. But the remainder that is left is your answer. We will use it now. So, these are our main arithmetic operators. Plus, minus, multiply, divide. And your modulus operator. Now, in Java, in fact, in many programming languages, You can also add some operators.
If you come from CC + +, then you will know what all the operators are. Because we use it the same way. In any other language also. And if you are studying for the first time, then also you will understand. Rest, we will do it together. You will have more fun. I have told you that we have these types of arithmetic operators. Now, in order to explain one by one how the operators work, it won't be fun. So, let's go straight to the code and see its use. So, I have made a demo.java raw file and we have written our boilerplate code. Which we are not understanding now because we will understand it later. What are we learning today? We are learning operators in Java. So, again we are talking about Arithmetic operators.
So arithmetic operators. Now, which are the arithmetic operators we saw? We saw plus, minus, multiply, divide. Apart from this, we saw modulo operator. Apart from that, what did we talk about? We talked about some joint operators. Plus equal to, minus equal to, divide equal to, modulo equal to.
To perform some arithmetic operation on it. And suppose I want to store its result in a third variable c. So I can say A plus B is simple. So you know the answer to this. 10 plus 5 is 15. And this 15 will be stored in our memory. Similarly, if I write something in D. Something like A minus B. So you know what will happen. 5 minus 10 means minus 5 should.
You know that 10 divided by 5 will be 2. And sorry, I will write this by commenting. you will get 2 and it will be printed in the memory. So, you understand all this very well. Let's say, I will also write another int g which is let's say b modulo a. If I divide b by a, then suppose I have divided 10 by 5, then what will be my remainder?
So, here I have shown you all the basic uses. This is simple. If I print it also, then how do we print it? We write system.out.println. I will explain you the meaning of this command later. Now you know that you just have to write this. So let's say, first I write C, sorry, let's write C inside, plus comma, so once I write comma separately, so see what I did here, C, D, E, F, G, I printed comma separately, and if I run this program, So, I will get the same output. 15, minus 5, 52, and 0. Which we were expecting.
So, I understood all this. That I have used plus, minus, multiply, divide, and modulo. They have a simple use. You have been studying plus and minus all your life. So, you will do this. Now let's look at some new types of variables. So let's comment out all of them. Let's start from the beginning. Let's say we have used the a, b, c, d, e, f, g variables. Let's start with the h. Let's say I said the variable is h.
You can do this in programming language, right? You can never do this in mathematics, right? Because in mathematics, assignment means that you are comparing left side with right side, right? But in computer science, when you write 1 equal to, it doesn't mean that you are comparing it. It always means that the value of right side, H plus 2, You are trying to assign it to the left side.
Now we will see the equal to operator. This is also a proper operator. This is called assignment operator. This means that you are assigning the value of the right side to the left side. This does not mean equal to. You are comparing it according to mathematics. For that, we have a special operator. It is double equal to. Which we will see later. But for now, you can assume that this can be written in computer science. So the value of h was a plus 2 and a was 5, so the value of h is 7. Now I increased the value of h by 2. So now the value of h is 9. Now 9 should be printed here. In fact, whenever I use h in the future, it will always be 9. So I can write this. So now because I can write this, Since this is okay in computer science. So if I can write like this, then any programming language, in fact C + +, Java and many others have thought that why not I give it a shortcut keyword, a shortcut operator. So if I have to write h equal to h plus 2, then I directly write h plus equal to 2. Which operator did I use here? Plus equal to. So whenever you see a plus equal to, understand the meaning of this. This means that you want to write h plus equal to 2.
So, h should be 7 again. Now, you know the meaning of this. h minus equal to 2 means h equal to h minus 2. What do you sign on h? Reduce H to 2 and assign it in H itself. Now its value should be 7 again. That means when I want to print it, I should get 7. Which will come. Absolutely correct. In the same way, you can use into equal to Where is it?
Assigned as h divided by 5 And you can write h modulo equal to Let's say again 5 So this also means that you took h and made it modulo from 5 So you can perform all these operations This is exactly the same This plus equal to, minus equal to, multiply equal to is exactly the same So you can write it like this or like that This saves a little space and is a little fast So generally we prefer this in programming If you look at any code, if you pick any real world project, then if you want to add anything in it, then generally we prefer plus equal to minus equal to. If you want to add the same number, if you want to add H in H itself, then right. Okay, very good. It was simple, we understood all this. How far did we understand? We understood this far. There are two more things left, your increment and decrement operator. Okay, what is this? It is very simple. Look, again I will take a new variable. Let's make it a little higher. Let's take g. Suppose I take a variable g equal to 6.
You could have written another version of this above as well. You could have written this as well. I plus equal to 1, right? We saw this above. If we give a little gap here, which is optional, you can also not give a gap. But it looks a little nice. So if I write H plus equal to, I can also write I plus equal to 1.
So if you just want to increment from 1, which you generally have to do in programming, like if we read loops ahead, So you have to increment from 1 many times, not always. So there will be many such scenarios when you will increment an integer value from 1. So in that case, this I + + operator is very useful. So now if I try to print this again. So first the value of I is 60, here I print I. And now I have added i to the final value i.e. i now is equal to 7. This value will be final. I will write it here.
If you have understood increment operator, then decrement is also the opposite of it. You just write i minus minus. Now you can tell that what does it mean? i equal to i minus 1. We can write it in another way which is i minus equal to 1. Okay, as we have written above. We did 6 plus 7 and then did minus again.
Then, I will write j + + in the next line. Again, it will be added in j. So, first it will be 8, then 9, then 10. There is one more way to write plus plus first and then j later. And all these rules are applicable for minus minus as well. I write like this, minus minus j and j minus minus. I can do all these things. So, I can do what I can do for plus plus for minus minus as well. Yes, remember one thing. Intu intu and divide divide.
So, this is not a valid operator. So, we don't use it. That's why it is increment and decrement operator. Because these are the most used operators. We always have to increment or decrement from 1. Because in computer science, if we read arrays or loops, you always go to the next memory. Multiplication, first of all, it doesn't matter what you multiply from 1.
You that you can write j + + or you can write plus plus j too. So I write twice, once I write j + + and once I write plus plus j. Now this is also an increment, the one above is also an increment. We call this prefix increment. And we call this postfix. Because what does prefix mean? First you incremented it. Means first you wrote plus plus and then j. And postfix means first you wrote the name of the variable and then wrote plus plus. This is the difference. Both do nothing. Both do increment only. As it was 7, so j plus plus must have made it 8. And then plus plus j must have made it 9. If I print this here. If I print this here. What do I have to print? j. Sorry, not i divided by j, j. So what should I print? My 9 should be printed. Let's see. Absolutely right, 9 is being printed here. So, what is the difference between the two? Why are there two operators when both are working on the same thing? There is a very small difference and that is also a very useful difference.
Okay? We have to guess the answer of this. And in the next line, let's make another L operator. And in this, I write, plus plus j. What will be the answer? We have to guess this. So, let's understand the meaning of both. Maybe you think that the meaning of both will be the same, but no. First of all, what is j? j was 7, here it was 8, here it was 9. So, we can assume that the value of j is 9. Okay, j is 9. Now, here we said k is equal to j + +. So, first of all, I know that j will be 10 from j + +. I can prove this too. Let's do one thing. Here, I will print j. I am not talking about k and l. I am just talking about j. Now, I know that j was 7.
I'll bring it down Now I have to guess what will be the value of k But first I'll print j Which I think by doing plus plus here j should be 10 So in the last I should see 10 It's showing 10, meaning j is increasing correctly Then what did I do? L equal to plus plus j I'll print j again So that there is no doubt that what will be the value of j J should increase again So now j should be 11 Let's remove it from here So in the last I should see 11, the value of j 11 is correct. So, j is increasing well, which we had thought. Whether write j + + or write + + j, j keeps increasing. What will be its side effect on k? Look, let's understand this. Whenever I write k equal to j + +, then it means that I am.
First assign k to j and then increment j because we know that j is being incremented but in k, do you know the value of j? the old one, which means 9 in j, before being plus plus, there was 9 and above it was 9 so the value of k will be 9, not 10 but j will be 10 right? so what was the proof? the proof was that I printed j and also printed k Basically, what we do is, we separate both commas again. And we printed K as well.
Okay, this is also very simple. First, let me tell you which operators come in this. In this, equal equal to. You will say two equal. Yes, this is the same equal which we were talking about in mathematics. That you are comparing left and right value. Okay, let's see about this too. Not equal to. The exclamation mark means not. We will see what it is. Apart from this, your simple less than, greater than. And we can add this too. Less than equal to and greater than equal to. So, these are all our relational operators.
And what will I put in that boolean? I will put a equal equal b. So whatever result it gets, I will put it in c. And what will I do with that result? I will print that result and see. I will print c here. I should understand the answer, right? So we will do this and see. Okay, our second assignment operator is not equal to. Now this is exactly the opposite of this. If I write a not equal to b. So, I am asking the opposite question from the computer. What am I asking from the computer? Is A not equal to B? Is it so? And again, I will either get the answer as true or false. The question I asked here, sorry, I asked here, where did it go? The question I asked here, I am asking the opposite of that question here.
It's a simple question. Again, either I will get true or false. Again, you can see that I can write a greater than b. Either I will get true or false. So, you must have understood till here. I have seen less than and greater than. Now, what are these two? They are exactly the same. Less than equal to, greater than equal to. We have used this too. a less than equal to b. So, am I asking a question? I am asking a question from compiler. Is a less than or equal to b. So I am asking this question. Is it less than or equal to a? Again, what can be the answer? Either true or false. So what I mean to say is, and I can also do the opposite of this, greater than or equal to, you would have also known that. So what I mean to say is, that all these operators, These all, equal, equal, not equal, less than, greater than, less than equal and greater than equal. These all finally give me a Boolean value.
I hope you have understood what relational operators are and what output they give. So I have told you that they give a Boolean output. So let's quickly see this in the code. So this is the same code that I was running. All the arithmetic operators I had shown you here. Now if we keep on doing this in this class, it will keep on mixing up. So let's do one thing, let's make a new file. So like demo.java was made, let me make a file named demo2.
I have taken two simple variables and I will take a third variable boolean because it will give me boolean value c and I will do a equal equal b see this I can do this right no compiler error has come now I will put a bracket first because it looks a little beautiful and understands what is happening ok what does this mean I am signing the result of this statement in this so now if I write system dot out dot println, I will try to print this as c, then what should I get? I should get false because a is not equal to b, right? So I should get false.
So I hope you understood. If you understood A, then the rest will come. I will just show by doing not. Let's say I said boolean, this time D equal to A not equal to B. So obviously whatever will come up, its opposite will be printed here. So let's say I print D here. So D should be true. Because C is false, D should be true, that A is not equal to B.
A less than is also strict. So, if we look at this, let's take Boolean F, that would be a less than b. And in this case, sorry, it will come out as b. In this case, I will make both as 10. So, let's make it less than or equal to. Now, see, here it will be true. a less than b will be false. Not equal to b will be false. And it will be equal to true. So it should be printed like this.
Because they help us for bit manipulations. Bit manipulation. Basically, it means that they work at the bit level. Let's see how they work. First of all, let's see the operators. For bit manipulation, we have bitwise operators. And, bitwise and, bitwise or, bitwise exclusive or, bitwise not. Now you must be thinking how are there so many operators? These are the same in every language. This is your simple AND. Bitwise AND, bitwise OR, bitwise EXCLUSIVE OR. If you have heard all this for the first time, it will be easy for you to understand.
Apart from this, we have SHIFT RIGHT, SHIFT LEFT. What else do we have? Apart from this, we have SHIFT RIGHT WITH ZERO FILL. Everyone will understand, it's very easy, no problem. Now obviously we can add two operators like and equal to, or equal to, exclusive or equal to, bitwise equal to, sorry shift right equal to, shift left equal to, shift right with zero fill equal to.
What else can we add? That's it. So these are all our bit manipulation operators. It might look like a lot, but when you use it, all your mind will be at ease because this is all repeating. From here to here, we have repeated them. Why did we repeat them? Because we have joined two operators. Just like we were doing plus equal to, we have also done and equal to. It's simple. Now, what are bitwise operators? I said, let's play with bits. How do we play?
Look, let's understand this carefully. Let's say I have two numbers, A And B. Simple. Now I have these two numbers A and B. Let's say I have A. What do we take? Let's take 2. And I have B. 3. So if I write in a result int C equal to A and B. So what will be its answer? First of all, what type of answer will it be? It won't be boolean.
Whenever we talk about bit operations, bit manipulations, and these operators are used, So these operators are applied at the bits level, not at the decimal level. Now this 2 and this 3, they are 2 and 3 in decimal. But if we convert them into bits, then you know, let's move ahead, I'll show you the calculation. So you know how we call 2 in bits. We call 2 as 1, 0. And how do we call 3 in bits?
Now, what am I doing in A and B? I am doing AND in A and B. What is the meaning of AND? It means that I will play something with these bits. Same, if I had not done AND here, I would have done MORE. this operator, then also I would play with these bits, right, basically all these operators that we have written above, they will play with these bits only, okay, they will play with these bits only, now how will they play, there are some rules for that, okay, if you have read some of its rules in discrete mathematics, then it is good, otherwise we will learn here, so for example, what do I do, for example, I am making a table here for you, Let's take two values, A and B. We'll do the AND of A and B once. We'll do A and B once. We'll do A and B once. And yes, we have this operator, not operator. Where did we make it? Here it is, not operator. This is unary operator. What does unary operator mean? It depends on one operand. Like till now we were writing a plus b. So this plus is a binary operator because it needs two operands, a and b. But like a + + was there, unary because it needs only one operand but our not is also a unary operator I simply write not a it needs only one operand let's see what is its value so I write here not a.
If I want to be 1, then both operators have to be 1. If any one of A and B is 0, then I am 0 for that case. My final value is 0. But if both are 1, then I am 1. This is called AND. In fact, if we use this a little bit, if we understand 1 and 0 in true and false, then you will understand things. A and B say that if both are false, 0 means let's say I say false, And 1 means true. So, A and B says that if both are false or if one of them is false, then my final value will be false. But if both are true, then only I will be true.
A exclusive or B. Or you might have read this in logical gates. But if you haven't read it, exclusive or says that in A and B, if there is odd number of 1, then only I will be 1. Whenever I will get odd number of 1, then I will be 1. Now see, in the first one, what is 0, 0? Are there odd ones?
So in that case, this is your 0. So the second line is 0, 1. Is there an odd number of 1s here? Yes, there is one 1. So in that case, we have 1 here. Sorry, we have 1 here. So the next one is 1, 0, 1, 0, again an odd number of 1s. So what will happen in 1, 1? Is there an odd number of 1s here? No, there are two 1s here. So this will again be 0. So this is your final output of exclusive OR. Not gate, I am saying gate again and again because we read logical gates, right? So we use these operators in that too. But we don't realize that when we talk about bits in computer science, then what is happening inside? There is a processor, we saw in the first lecture, now the processor is also working on bits, there are logical gates inside, right? So what is actually working? This is making logical gates, working on 0s and 1s. We just represent it programmatically with this operator. We are doing the same thing. Now what will be not a? Not a will be, which will not be a, which means a will be upside down. So what does it mean?
Because it tells the true value of a and b. What will be the true value of a and b? a and b, a and b, sorry, a exclusive and b, not a, not b, etc. Anyway, operators we have are these main main apart from this see what we have covered and or exclusive or not we have covered 4 operators apart from this we have shift operators shift left sorry shift right shift left and shift right with zeros now let's see what happens and after that we will repeat this again using assignment assignment you must have understood so let's say if I write c is equal to C and 2. Assign C to 2 and assign it back to C. So I could have written C and equal to 2. Like we did for plus. So basically what we were doing in plus minus multiply divide, we did the same thing. We can also do it with all these. With AND, OR, EXCLUSIVE OR, and now with the shifts we will see.
Something like this. And after doing this, our number will become that number. 8. Very good. Now if I have written here, B left shift 1. What does this mean? This means that if I make all the positions one more time, then this means that all the positions, all the bits, all the positions will be left shift. Something like this. This will come here, this will come here, and this will come here. Something like this.
Okay. What does it mean? Let me show you first. First of all, this zero will rise and come from here to this position. On its left position. In the same way, this zero will rise and come to this position. In the same way, this zero will rise and come to this position. And this zero will rise and come to this position. This zero will come here. This will come here. And finally, this will come here. And what will happen to this? This will come out of our bit. It will come out of our 8-bit range.
So we introduced a new 0 in our least significant bit. So our final answer will be this. You can ignore this because we are on the 9th bit and our bit range is 8. So our final output will be this. Okay, this was a little wrong. Here this shift was done, not 0, 1. This is 1. The rest will be 0 obviously. But this is our 1 shift.
If we convert this final number back to decimal, Very good. So what did we do? We shifted 8 to the left from 1 and it became 16. We shifted 16 to the left from 1 and it became 32. So here you must be seeing a pattern that whenever we shift to the left, our number becomes 2. If we do it again, it will become 64. Why is this happening? It's very simple. See what's happening. Wherever there is a 1, like there was a 1 on the 8th bit and the rest of the bits were 0, it moves one position left from its position. And as soon as that one position moves to the left, double position is present in the left. So if the number was 8, double position is present in the left. So if this 1 shifts here, then the overall number will be 16.
So it happens with every number. If we represent 10, then 8 would also be 1 and 2 would also be 1, then the number would be 10. But if 8 moves towards 16, 2 moves towards 4, then 16 plus 4 becomes 20, right? That means 10 is also doubled. So all the ones, one of their positions is left shift and the original number doubles. Very simple. So you can keep doing this. Now you can make 32 to 64 and you can go ahead. So first we understood this story, okay? What does your left shift do? We understood the basics. All the bits shift to the left. Not one by one, but the same as we have written here. We have shifted B from 1, so one position shifted. If we shifted from 2, two positions would have shifted. So this would have shifted to this position. And the two spaces left, we would have shifted to two new ones. If we shift B from 3, then this 0 would come directly to its 3rd position. And if we take 3 new 0s from these 3 positions, then we get 3 new 0s.
That is what we were looking at here. So, I hope you have understood this much. But can we keep doing this? What does it mean? It means that if I write again, let's write it in white, B is equal to B left shift 1. So, if I shift it again to the left, What does it mean to do it again? I make 8 positions and left shift these positions again. This comes here, this here, this here, this here, this here, this 1 comes here, this 0 comes here and this 0 comes out. This new 0 comes, I fill my 1 on it. So, our new value has become this. Which one part is this?
Here, this came here, this came here, this came here, and this one finally came here and this zero came out. And this spot was empty, here a new zero came. Simple. So now our number became this. And what is this number? Where is 1? At 128. So our number became 128. Right? Right? Absolutely wrong. How?
If your MSB is 1, then it means this number is a negative number. And if it is 0, then it means this number is a positive number. We had read this too. As soon as you shift this 1 here, then this 1 will come on your most significant bit and our compiler will understand it as a negative number. And what does it do when it understands a negative number? It takes out its 2's compliment whenever you try to get its value. We have already studied this. Now you know that if we take out its 2's compliment, then our answer will be minus 128.
Let's write it like this, our number is 1 and 7 times 0, 4, 5, 6, 7. If we take out 2's complement, so in 2's, first we take out the first complement. So let's reverse everything, its 0, 1, 1, 1, 1, 1, 1, 1. And then to take out the second complement, what we do is add 1 to it. You have done this many times, 1 and 1's 0 becomes carry 1, then 0, carry 1. 0, 1, 0, here also 1, 0, here also 1, 0, here also 1, 0. And finally here 1, which will come here. Number will be this. If you take out its decimal, you will get 128. In the original number, our MSB was 1. In the original number, our MSB was 1. That means, if we put a negative sign here, then the final answer will be negative 128.
What is the range of a byte? From minus 128 to plus 127. This is the range of your byte. If you keep 1 till here, then everything is fine. But as soon as 1 comes to this MSV, your whole number will become negative. Okay, very good. I understood this too. If I keep doing B left shift 1, then it will become negative after a limit. Okay, what is that limit?
Common sense tells us that if we shift again to the left, then all the bits will shift to the left again. And this 1 will come out again. And the number that is left empty, a new 0 will come here. This means that our range is this only, because it is outside the range. So finally, the number that will be our number, that number will be again 0.
Now, the interesting thing is that after this, no matter how many times you do left shift, if you think logically, then if I write again, B left shift 1, So what will happen? This zero will come here and all the bits will shift to the left and the new one will also come to zero. That means even here everything remained zero. Again the answer is zero. Again I will shift to 1, again everything will be zero and again our answer will be zero.
Because it will say that I will promote this b into integer. And then I will do int into 5, whose result will be an integer. And you are trying to put that integer in byte, which is a big number. You are trying to put it in a small number, which is an error. Now, how did we solve this? We used to do type casting here. We used to write byte. To tell the computer that what I am doing, I am doing this intentionally. We had also discussed that why is this type promotion? So now we will not go there, you can watch that video again. But now I am telling that because of type promotions, what difference does it make on our bitwise operations?
Apply them on bite and shot, then what will happen is that the tie will be promoted on int. They will be less on long when the limit of int also goes out, but we will not talk about that now, we will talk about int. If you perform these shift operations on byte or short, then they will be performed on integer from inside like this line. When you do bx5, then byte is not multiplied by 5 from inside, but byte is first promoted towards int and then we multiply this integer by 5. So similarly, when you were writing here, B left shift 1, B left shift 1, then the B shift that was happening here was actually not a byte shift. First, this byte shift was happening in the integer and then the integer was shifting left and right. Obviously, your answer will still be the same. If you assume an integer instead of a bit, then also 8 to 16, 16 to 32, 64 will be the same.
When we have shifted everything to this range and our 9th bit has become 1, what happens after that? For that, you have to understand the rule of type promotion carefully. Your shift operations are performed in int and long. If you try to do it in byte or short, then type will be promoted in int and will perform above it. Then you can type cast it again and put it in byte. That is your choice. You can't write this line directly on the compiler. You have to typecast byte here as we did earlier. This is very important. Now see how the game changes completely. The game changes like this. If I tell you again that I had a number.
Now, I had said, That I will shift this, first I will shift once, then twice, better than this I will write B left shift 7 times As soon as I shifted 7 times, this 1 came up and here everything else is 0 Very good, right? And what would be our final number? Minus 128, we saw it above Now understand this story carefully. The story is that when I have written in the memory, let's say b is equal to b left shift 7. So what would have happened from inside? First of all, this b would have been converted into an integer. And I would have shifted that integer to 7 bit. And again I would have assigned it to a bit. That means here I would have written byte. Here I would have done type casting. So actually if I write this statement correctly, then it would be something like this.
Sorry. It will be something like this. B equal to type cast to byte. B left shift 7. Something like this will be our statement. See, what I said. This is our byte. So it would have happened that Byte takes 8 bits So I have represented it in 8 bits Very good But as it was typecast in integer Our integer takes 32 bits Do you remember that integer is of 32 bits?
Okay that suppose we had this number, we had bit B equal to 1, and this is its binary, I have expressed it. What will happen, as soon as I wrote B equal to B left shift 7, what will happen from inside? So from inside this will happen, first of all, our byte will be converted into integer, that means now we need 32 positions. So now if I do this and do this, then 32 positions will become very big. So first I will show you only 8 positions, 1, 2, 3.
Now you can assume that we have made 8 positions here Similarly, some 8 positions are here So 8 plus 8 is 16 Then 8 positions are here That is 24 And then 8 positions are here So total is 32 So 8 plus 8 plus 8 And this is 8 So I am just showing you the starting 8 positions Meaning the lowest positions I am just showing you that So that it is easy You can assume that there are 8 positions here as well Now this is our int I will write it here Now this type is our integer type of which I am teaching 8 bits of starting. Now, on this integer, these 7 bits will be moved, right? 7 bits will be moved on the integer, which means this 1 will come up here and the rest will be 0. Like this, like this, like.
Bit also has a 0, it moved on 10th bit 10th bit also has a 0, it moved on 11th bit So all the remaining bits are 0, just remember This bit is 1 This means that definition wise, our MSB is not this This is our starting 8 bit, our MSB must exist somewhere here Our 31st bit will be here and this is our 0th bit So our 31st bit will be our MSB And our 31st bit will be 0 this time Because this bit is 1 and this bit is 0 So this whole number is a positive number.
So its answer should be plus 128 But then we type cast this operation in byte See here it is written that type cast the answer of B left shift 7 in byte Because you can't write it simply, you will get compiler error As soon as we type cast it in byte What will our computer do? The 32 bits will be truncated with the 8 bits of the starting. And this byte will be truncated with the 8 bits of the starting. We discussed earlier that this is an integer of 32 bits. If you want to represent it in a byte, which is of 8 bits, then the previous 8 bits of this 32-bit will come here and the rest of the values will be lost. These least significant 8 bits will come here.
So I took the starting 8 bits and put them here. The rest of the bits got lost and my number became this. Now as soon as this number is created and it is stored in B and I wrote getB which is my statement system.out.println when I write this and now I have written B then what will happen? When we go to the memory and check the number, we will get this number. Now according to this number, this is its MSB because now only 8 bits are left here and this is its LSB. So now the compiler will think that this is a negative number because here our most significant bit is 1. So it will tell this number negative. It will tell us minus 128. Which is also correct because we typecast it. So this was the story. How did we simply explain it? That if we keep shifting each.
Finally this number will become negative. But it doesn't happen like that. Remember that the shifting is always in integer and then we truncate it. So this is how that negative number is made. Now this story is important because let's say if you again... I will shift one more position in B. I will write B left shift 1. First 7th position has been shifted. I am shifting one more position and I am typing it from 1. As soon as I have written this statement, now you have to understand this is a very important concept. So those people who don't know bitwise very well, they will say that as soon as I shift left again from 1, then what will happen?
Will move to the left and here we will have only 0, 0 left. What I mean to say is that if I first make their position 1, sorry. 1, 2, 3, 4, 5, 6, 7, 8 So what will happen after shifting? This 0 will come here, this 0 will come here Like this everything will move to the left This 0 will come here and this 1 will come out And a new 0 will come here And our overall number will become simply 0 Right? If you do this in compiler also then only 0 will be the answer So if you write b first, b left shift 7 And then you will do B left shift 1 again And typecast here and here as well So 0 will come But we will have to understand why 0 will come It is very important Look carefully When our number was made When we were at this stage And we wrote B left shift 1 So what will happen in the background? In the background this will happen That our byte will be typecast in integer Now how will it be typecast in integer?
If I have the original number here, the MSB here, if that is also 0, if the number is 0 here, then I can put 0 everywhere, right? Because that is a positive number. But you imagine that the number here is 1. If the MSB is 1 in bytes, then can I blindly put 0 here? Not at all. If I put 0 here, then this number will become positive again. But here 1 means that this whole number should be negative. So I have to put 1, 1, 1, 1, 1 here instead of 0.
If I put 0, 0, 0, 0 everywhere here, and kept the same here, then this number will become a positive number overall. In the same way, to represent minus 128 in the integer, If I keep it like this and make all the zeros here, then this overall number will become my plus 128, which I don't want. That's why I have to do the same here. I have to store 111 in all the remaining 24 bits. And the remaining 8 bits will remain this. So that our overall number becomes minus 128 only. You can take it out. If all 24 bits are 1, And the remaining 8 bits are the same which were 1 and the remaining 7 bits are 0 When you will complement these 2's then the answer will be minus 128 And this is what we wanted So you understood this first Now this is important because if you understood this That when we did B left shift 7 Right, till here our work is done, so we saved it in B. Then we did B left shift 1. Means our number, when we left shift 1 again, so first this B converted in integer. And when it converted in integer, what will happen?
This means that all the bits in the front have become 0 Which we had already shown you, all the bits in the front have become 0 But all the 24 bits in the back are still 1 All the 24 bits in the back are still 1 and this is our number And when we again typecast this in bytes Then we again truncate the starting 8 bit and we show that the number is 0 Okay, this means that what would happen if I had not written this Let's say int I equal to B left shift 1. If I had written like this, what would have happened? Now you will understand why you have read what you have read till now. And how they are different from those kids who understand.
It was 0 because the starting 8 bit was truncated. But if we look at the integer number beyond 8 bit, then our number will be minus 256. I hope you have understood this concept. This is a very important concept because people don't understand bitwise so well. You just have to understand that bitwise always performs on integer or long. People make this mistake that if I had a number which was 1 and I kept on left shifting it and if it was a byte then I will write b is equal to b left shift 7. As soon as I write this, my number will become negative. If I write 8 then my number will become 0. See here in the starting all the bits became 0 because all the 8 bits went left. Now, after this, whether I do 9 or 10, basically means, B left shift 9, B left shift 10, B left shift 11, no matter how much I do, now my number will be just 0 here. Meaning, no operation of shifting is performing now. But actually, it is not like that. All our bits are not lost, they are stored in integer. And if you don't typecast it, don't truncate it, try to print the entire integer Then you will get the double of minus 128, minus 256 Then the double of this minus 512 Then the double of this minus 1024 And then the double of this. By doing this, you will keep getting it. Now, till when will we get this? That is a very good question to ask. So, I told you this. All our bitwise operations are performed on an integer. And I told you that we were just truncating the data in the byte. Which was wrong because we were not able to see the whole picture. So if you want to see the real picture, you have to print the entire integer. So now let's assume that I already have the number int I equal to 1. And I will shift to I, let's say I just shifted once. So if I have I1, then you know, I means 32 bits, so I have to represent 32 bits. So I don't do it now, let's assume these are the first 8 bits, then these remaining 8 bits, these remaining 8 bits, 8 plus 8 is 16, plus 8 is 24, and I represent the remaining 8 bits like this, 1, 2, 3, 4, 5, 6, 7, 8. I will represent something like this.
If I would have written 30 directly, do you know how far I would have moved? I would have moved to this bit. The bit right next to the MSB. I will make this too. Let's say, I will write it here. There will be some numbers like 0, 2, 3, 4, 5, 6, 7, 8. I have written 8 zeros here. They are very small, but you can adjust them. And which is our 31st bit? This is 31st bit. Okay?
So when I do I left shift 30, then basically I... could shift till this bit. So this bit would be 1 and rest of the bits would be 0. Now this would be a very big number because my 31st bit is still 0 and this number is 1 and the rest are 0s. So this would be a very big number. Now if I write I left shift 1 one more time, then my 1 would come up here. That means my MSB would be this 1 and the rest would be 0. Now as soon as I write this, then again what would happen? Compiler would think that there is a negative number and my answer would come in negative. That means whatever the biggest negative number is, which can store integer, I would get that. Interestingly, what can I write by combining these two statements? I left shift 31 times, right? So if you do I left shift 31 times, then the number you get, the negative number you get, is the maximum negative number of the.
Small negative number, not big, I was saying the opposite, the smallest negative number, which it can hold, we get that, right, in the same way, if I left shift an integer, 31 times, then I will get integer, which can hold the smallest number, right, the smallest number, which can hold, this number, in java, if you write, integer, dot min value if you write something like this then you will get the same number which we are talking about again this is already written in java we also call this negative infinity in many cases we will discuss further why we call it okay now you can assume that this is the smallest number which can hold an integer which we will get if we left shift i 31 times now the last question the most interesting question if I write i left shift 32 then is this also applicable Then I wrote I left shift 33 or 34 or any other big number, is that applicable? Look, the answer to this is very interesting. The answer is no, not applicable.
Look, you know, we do actual shift operations on integer, right? Byte and short were always type cast, type promote in integer. We can do actual shift operations on integer. Now if we do an actual shift operation in integer and java thinks that if you make a least significant bit as MSB by moving it 31 times. If 1 was here and you moved it 31 times then it came to MSB. This was LSB.
To keep any processor optimized, because if you shift from LSB to MSB, even after that, if you start shifting, then the number is completely gone. So, there is no logic in doing the calculation after that. So, Java modifies all the shift operations in integers or even in long. For example, whenever you shift something, it passes through mod32. That means, if I write I left shift 31, then it will remove I left shift 31 mod 32. And do you know the special thing about mod operation? What does mod do? It takes out the remainder, right? So if you mod anything from 32, then the remainder can come from 0 to 31. No one can get a bigger number than this. That means your integer is always capped. That means you can left shift from 0 to 31 in any integer. After that, you can't do it. Because if you tried to do 32, then 32 mod 32 will become 0. That means you will come back to this. If you try to do 33, then 33 mod 32 will become 1. That means you will come to this and you will start rotating again. So 0 to 31 went, then again you will rotate 0 to 31, then again 0 to 31. You will not be able to get out of this cycle. And why did you do mod 32?
It will not let you get out of that range. This means to say, and this is very interesting. If I write I mod 32, then it is equivalent to I mod 0 because 32 mod 32 is 0. And what does I mod 0 mean? The original number, I left shifted 0 times on it. It means I didn't left shift. So I left my original number there. So it is equivalent to I. And similarly, I left shift 33 is equivalent to I left shift 1. So I hope you understood the whole story If you can shift left to maximum You can do it till 31 Because 31 is the maximum range See here on 31st is your MSB, most significant bit So you can put any LSB to maximum and put it to MSB After that if you try to shift again Then it will say that I am applying mod operation of mod 32 So if you shifted I to left 32 times then mod 32 means You left shifted I zero times. So the original I will remain the same.
I hope you understood the whole story. What is left shift? How does it work? And how does left shift actually work in depth? You must have understood that. I also told you that in byte and short, left shift operations don't work. Even right shift. In general, any shift operation doesn't work in byte and short. It only works in integer and long. And when we express it in bytes, then we see a truncated form of it. We see the starting 8 bits. The whole operation is going on in the remaining 24 bits. And we also saw that in integer, you can shift a maximum of 31 times. Whether you shift it left or right. Because after that, at the 32nd time, your shifting becomes modular.
Now, if I write B is equal to B, right shift 1. Obviously, you know that I have to type cast here. Because it is not allowed. Because B will be in integer first. And if you do any operation in integer, then the final result is integer. And you can't express that integer in byte. So you have to type cast. You know that.
So I wrote this. Now see, right shift means the opposite. Now move all the bits right. So what will happen? This bit will come here. This bit here. This will be 1 here and the rest will be 0. This is here. Now what will come here? Here we can't write directly 0. Again same rule because this is your MSB. And MSB termines the sign of the number. If we write 0 directly, then even if your number was negative before, it will become positive. We will have to see what was the original sign there. So, before there was 0 on MSB, so we took 0 for the new bit. If it was 1 here, then we would have taken 1 for the new bit.
If you right shift it again, the number will come here and the rest will be 0. And the number will become 2 because there is 1 here. That means if you right shift it, it looks like you are dividing it by 2. It's the opposite. You must have understood the reason for this. Because we move the number to the right side where half the number is present. So if there was 8 here, it would be 4. It would be the 4th place. If there was 4 here, it would be the 2nd place. So the overall number becomes half.
And what will be the new number here? Here also the new number will be 1. Because here it was 1. Our number will be this. If you subtract this number from the decimal again, then what will be your number? Minus 64. You can subtract it and see. Take its two complements. First the first complement, then the second complement. You will get minus 64. Which is right, isn't it? If we do half of minus 128, then minus 64. Then if you subtract it again, you will get minus 32. Then if you subtract it again, you will get minus 16, then minus 8, then minus 4. And it will continue like this. Now, the interesting thing is that if you write b equal to b right shift 1, then there is no problem. Truncation will also work here. But the best thing is that your LSB is always lost. And the new value always comes towards the most significant bit. This means that if you write a byte b right shift 1, sorry, this means If you keep doing right shift operation in byte or in short, then you will get expected result. You will never feel that there is any wrong output which is not in left shift. You can apply the same concept if I do all this on int. So if I apply the same operation on int, let's say I write int i equal to 128, so if I right shift that too, then overall we would get the same result. What I said earlier is that actual shifting operations are performed on int and long only. Because on byte and short, truncation happens. So you can apply the same logic here as well. The actual shifting will be on long or integer. After that, if you try to put it in byte or short, then you will only get truncated value.
And your mod32 rule is always applied. That you are more than 32 bits. Mod 32 bit rule is also applied here. Obviously, you can't move anything more than 32 bits. 32 bit means that you have moved something from MSB to LSB. This is your 31 moves. After this, if you take the 32nd move, then what will happen? This will also come here, which will be lost. The whole number has changed. This means you do mod 32. So, this means if you try to shift outside 31 to 32, then it is equivalent to that you are.
Because we have 1 here. So it would have come here. It's a simple thing. So you would have understood this. We had already studied this. But if I do the same thing. Byte right-shift with 0s 1 operation So what would happen? So we would get an output like this See, obviously all these bits would move, this would be 0 here, this would be 0 here Similarly, this 1 would be here, the rest would be 0s It looks exactly the same till here. But instead of 1, I would have simply put 0 here.
This is a normal right shift and this one is a right shift with zeros. Now what is the use of this operator? Sometimes there is no sense of negative values. For example, if you talk about pixels, you have an image and you know that there are small pixels and one pixel that there is an integer value. In that case, we always need a positive value. We don't have a concept of negative value in that. In that case, right shift with zeros is useful because we know that if we right shift, then even if MSB has 1 or 0, we don't care. We always have to introduce a new zero. It has a very specific use case. When you get that use case, you will understand it better. But there is nothing else in it. Right shift with zeros is just like that. So let's quickly go to the code and finish it. So first of all, let's see our bitwise operations. We have to write the code for.
Then let's do E which is equal to A exclusive or B simple. And let's do F which is equal to let's say not A. So we have done all this. Now we will print it. So we have printed it. C, D, E and F. All commas will be printed separately. So let's quickly see what the answer should be. Now you know what is the binary of 2. The binary of 2 is 1, 0. What is the binary of 3?
If you do A exclusive and B, then you know what is exclusive and what is not. If there is an odd number of 1, then it is 1, otherwise it is 0. So, here is 0 and 1. So, if there is an odd number of 1, then the answer will be 1. Here also 1. And here also 1. Even number of 1s. Means the answer will be 0.
I have written 0 for this 1 and 1 for the final 0. So this will be the output. Now if you convert it to decimal again, then it will be minus 3. If you convert it to decimal again, then your answer will be minus 3. So your total answer is 2, 3, 1 and minus 3. Let's see quickly, this is being printed. So 2, 3, 1 and minus 3. make it to G.
30 times shift, so our answer is this big. Now if I shift 31 times, this 1 will come here, and this will be 0, and this will be your smallest number, which can store integers, which is a minimum value. So let's see, 31 times a negative number should come, we talked about it. Yes, see this came, negative. Now if I shift this 32 times, So what will happen?
It means to shift 32 times. If we look carefully, then what does this mean? That you are doing 32 mod 32, which is equal to 0. Which is equivalent to, that you are saying g equal to g mod, sorry not mod, g left shift 0 times. Which means you haven't left shifted g. So our answer will be again 1. Which was g originally. Now, if we see, our 1 should be printed.
So, what we have read is absolutely correct about shifting. Now, if you shift it again 33 times, then 33 mod 32 1 will come. So, your answer will be 2 again. Because this means that we have shifted it only once. So, I will write it as G is equivalent to Sorry, I'll write it like this again.
And your right shift also has two shifts. One is your normal shift. This one and this one. Which you call right shift with 0. You can use this too. It will do the same thing. Let me tell you one more thing. Let's say I have a byte number. And I take the number h. And that would be 1 again. And I write h equal to h left shift 1. So see, here my error comes. And what error comes? Type mismatch cannot convert from int to byte. This is what we talked about. Whenever you left shift H from 1, the compiler will promote H in int. And then it will try to left shift int to 1. And then it will try to put that number back in H. Now when you put integer back in byte, you know there is an error.
And then this, whatever the answer is, I am casting it in bytes. That means, the previous 8 bits will remain. The rest will be truncated. Something like this. And this will be your answer. And what does answer mean? It will be 2 again. So, it will be 2 only. But actually, how it became 2, we will have to know that.
We understood internally that first that type is promoted in int and then 8 bits are truncated in the last. So, if I print H like G, So, let's print this too quickly. Here we have H. So, let's see. We should also get 2 here. 2. Absolutely correct. So, this is the whole shift operations. You can play with it a little more. You will understand. I hope you have crystal clear everything.
See, this is very simple. If you understood bitwise, then you would have understood this. There are only two operators in this. One is AND AND and the other one is OR OR. By the way, it doesn't read AND AND, it reads AND only and it also reads OR only. Okay, but we read AND in bitwise and its AND looked like this, the single ampersand one.
The simple thing is that your logical operators don't work on bits. Like bitwise operators used to work on bits. Okay, so what do they work on? They work on expressions. Okay, like suppose I wrote an expression and wrote another expression. To evaluate both of them, I used these two operators. Okay, what does it mean?
Look, let's say I have written int a equal to 5 somewhere here. And I have written one more expression b equal to 10. And let's say I take one more int c equal to 15. Now suppose I write an expression int d is equal to a less than b and a less than c. If I write a statement like this, then what do you understand from this statement? You must be understanding this statement that I did something wrong here, here int will not come, here boolean will come. Yes, you must be understanding this statement that the value we are going to get here is going to be boolean.
And operator, we had understood in bitwise also, even if we had read single and in it, and it would have worked on bits, and it works on expressions instead of bits. But from inside, their logical, conceptual thing remains the same. And that is, if both the conditions are true, then only the end result will be true. And if one of the two conditions is false, then the answer will be false. And if both are false, then obviously it will be false.
We saw this on the basis of zeros and ones in Bitwise. Here we can see it on the basis of true and false. So what I mean to say is that if this expression comes true and this expression also comes true, then only true will be stored in this D. If one of these expressions is false or both are false, then it will be false in D.
Condition is true. If one of these two is true, then E will be true. We saw in the truth table that there are two, A and B. A can be 0, B can be 0, 0, 1, 1, 0, and 1, 1. So, we just converted this truth table to true and false instead of 0 and 1. So, if you make it here, instead of 0 and 0, you write false, false, false, true, true, false, And false, false.
Okay, so here also it will be false and here also it will be true. And in A and B, here also it will be false and here also it will be true. Right, so it was in the same condition. 0, 0, 0, 0, 0 and 1 and here also 0 and here also 1. Right, so it is exactly the same. The concept which we used in bitwise, we are using it in logical. Okay, I hope you understood that how are OR operator and AND operator looking here.
There is one more thing more interesting than this. And we call that thing short circuit. Short circuit. Now see what short circuit says. Short circuit says that if I know one thing about AND. That both, suppose I have written A and B here. Let's talk about true and false. Suppose I have written.
Because look, you can write multiple such conditions together with it. That you know, if the first one fails, then the second one should never be checked. When does this work? When we do null checks, we will discuss about nulls later. But short circuit works a lot, especially in loops and conditions. When you intentionally want that if the first condition fails, then the second should never be executed. In that condition, you use the concept of short circuit. So you should know that it exists. And one more thing, the opposite of this. Whenever you write A and B, you know that if A is true, then it will be true. So in this case, it will be false. In the rest of the three cases, it will be true. So this means, if I have written A and B, and my A result is true, then do I need to check B again? Because I know that B, whether it is false or true, it doesn't matter. The whole combined statement is true. So, I don't even check B. So, again, this is also called short circuiting.
That is, these bitwise operators, these ones, and single OR and unary NOT. Yeah, we can leave unary or NOT. We will talk about this simply. We can use these operators only for bits. 99% you are right. But in Java, smartly, you can use these bits operators with expressions too. Like we declared a5, b10, c15. And we wrote a less than b. And and a less than c. Instead of this and, I can write one and. And its result can also be assigned to a variable d. This is also a correct statement. But what will happen from inside? We know that this operator AND works on bits, but there are no bits here. Here we are talking about expressions. Nothing will happen. What will happen is that the one who does double AND, the one who gives logical output, that if both of them are true, then only the answer will be true. If one of them is false, then the answer will be false. If both are false, then obviously it will be false. But what will it do? It doesn't do short circuit. Does not do Short circuiting. This is a very interesting interview question. Many people think that short circuiting is always there in Java.
You can use bitwise AND instead of logical AND and you can do this, this is an interesting technique if you intentionally don't want a short circuit there are many specific use cases in java 99% of the time when you write code you never use this AND with expressions mostly you use double AND and same double OR which we call logical OR This will be a very limited condition. This is generally a smart interview question. It doesn't have much use case. But still, it is asked. And the people who know this, they stand out a lot. Because they understand the operators very well.
This is your bitwise operator. Let's use it in the code. So, let's see our logical operators. Let's say I wrote a as 5. Let's take the same example. I wrote b as 10. And I wrote c as 15. Now, let's say I write a boolean variable d. And I write a less than v. And b less than c. And what should I do with that boolean variable? System.out.println And I will print d. So what should happen?
Okay, we had studied short circuit. And I had said that short circuit means that if there is OR and the first condition is true, which has happened in this case, then it will not evaluate the second condition. In this case, it means that it will not be compared to B or C. It will directly give the true answer. But if I say the opposite, if I put AND here and fail the first condition, let's say I said 25. Now because the first condition has failed because 25 is not smaller than 10 So it will not evaluate the second condition again So it will not compare with B and C This is called short circuit But if I put single OR here Let's say if I would have done double AND So the answer would have been FALSE Because A is not less than B If a is less than b.
But in this case, I can say that b is not compared to c. Because the answer is false in a and b. But if I had put a bitwise end here, then also the answer would have been false. But I can also say that this time both are compared. Obviously, the answer of this statement is false. The answer of this statement is true. But false and true. If you do false and true, then you always get false. So the final answer is false. But you will ask, what is the meaning of this?
What is the difference if the previous statement is evaluated or not? It is used when you use it to increase a statement in conditionals. We will see that later. And it plays a very smart role. I told you that 99% of the time, only double and is used in programming. No single and is used. There is a 1% chance. But this is a very good question to be asked in an interview. So we should know, right? Rest, I hope you have understood all these short circuiting logical operators. Let's quickly move to our last operator and finish it. Now see, you have read our last operator many times. That is the assignment operator.
Let's say I write int a equal to 5. So this 5 is my AssignmentOperator. Assignment operator What does it mean? What I want to do here I am saying This value of my right side 5 Put it in A That's it This is what assignment operator says Remember that's why we talked about this equal to And what's the difference between this equal to This equal to is assignment And this lower equal to is relational Relational Because it's comparing So if I give an example of this Let's say I have an int a equal to 5 and a b equal to 10. If I write a equal to b, then this is a Boolean statement. I told you what a equal to b means. What are you asking the compiler? Is a equal to b? You are asking the compiler this. The compiler will answer this in true or false. In this case, it will be false. But if you write a equal to b, So this means that you are telling the compiler that whatever is in B, put it in A.
So, this is the difference between assignment operator and relational operator. So, let's go back to relational operator. So, we took an example. Let's say, int a equal to 5. This means that whatever is on the right side, i.e. 5, if you put it in a variable, it will be a container a, we put 5 in it. Obviously, I put it in binary, to represent it, I showed it in decimal. Okay, you can also make a chain of assignment operator.
I can write code like this, int a equal to b equal to c equal to 10. This is also a valid java code. Because here, see how it starts. First of all, it will look at the rightmost thing, which is a literal 10. It will put it in C first. Then it will come to B in C. The one in B will come to A. And in all of them, A, B and C, the value of 10 will come.
Only once, otherwise you would have written int A equal to 10 three times. int b is equal to 10, and you write int c is equal to 10. So you write this statement three times. It's better if you short it and write int once. And three variables are going to come. a is equal to b is equal to c is equal to 10. Write it three times and your statement will run. You can run it and see. So this was the assignment operator. You know that we have always used it. You just had to tell me its name.
Now, one last thing that we have to discuss, now you will say, there are so many operators, but when you use them, they will be used seamlessly, you won't even know. You won't feel like, I need to remember them. No, you just need to understand that these are operators. As you keep using them in code, they will easily sit in your mind. Now, the last thing left is a special operator. That is a special operator, which we call ternary operator.
Ternary operator will be fun to read when we read conditionals. So, I will explain it well. When we read if-else, it will be more fun. But, let me tell you. Till now, all the operators we were reading, either it was unary operator, like unary not, because it is on the same operand. Or we had binary operators, like plus, minus, multiply, divide.
This is the only operator, ternary, which takes place on three operands. So, operand 1, operand 2, and operand 3. It takes place on three operands. Now, how does it take place? What does it do? We won't read about it now, because it won't be fun, we won't understand. We can understand, but it won't be fun when we read about if-else, about conditionals. So, I am removing this from here. Okay, let's just look at one more thing. Now you'll say, bro, how many things will you show? Let's do one more thing. But this is definitely the last.
So I have used a lot of operators together. So what will be its final output? Just like we have a board mass in maths. Which tells us the precedence. That division will be first, then multiplication, addition, subtraction. In the same way, we follow precedence in computer science. Obviously, if we follow the mathematics board maths rule, then the priority of division will always be higher, then multiplication will come, and then addition and subtraction will come.
So, see, this is the table, this is the precedence table. In this, the highest precedence is at the top, and as it comes down, the precedence becomes low. And in the same row, their precedence is the same. In the same row, let's say there is divide and multiply. I said their precedence is same. If I evaluate them, how will they be evaluated? They will be evaluated left to right. Where is the equal to operator? It is the lowest precedence. This is evaluated from right to left.
We had given an example above. First 10 to C, then to B, then to A. This is how the evaluation is done. Now look, don't get scared by looking at this precedence table. No one remembers it. It doesn't happen in anyone's mind. You don't even know. You just know that the lowest is equal to. Because after everything is done, you assign something to the left side variable. That's why its precedence is lowest.
So, you don't need to remember all this and you don't need to keep it in your mind. I will tell you why. This is theoretical concept. When you look at it in theory, then obviously precedence should be there. But you think practically, when you write code, then you never write such a complicated statement. If you want to write a complicated statement in Maths, then you will use brackets. Interestingly, you can use brackets in Java as well. The precedence of brackets is highest. Highest precedence. So, you can play with brackets and evaluate any expression according to you. So, let's say, if it's written somewhere, A equal to, let's say, B plus C into D. So, obviously, according to the rule of board mass, first C into D should be there. Then, whatever result will come, it should be plus B. So, B plus C into D. But, let's say, I don't want this. I want that first, B plus C and then multiply by D. So what will I do? I will do nothing. I will simply put a bracket in B and C. So what does this mean? This means first evaluate this and then multiply the answer by D.
So, it doesn't make much difference from this precedence table. A little bit should be in mind that yes, equal to is the last. Theoretical, if you want to give someone knowledge or tell someone in an interview, then there should be a little idea that post-it prefix are more, then arithmetic comes, then your shifts come, etc. But, whenever you are literally working on an application, then if you are working on a complex expression, then you always control things with brackets, not with their precedence. Because it looks so bad that even if you remember the precedence, then you.
So here we have finished all the operators. We have not studied ternary yet. Which I said we will study when we will do conditionals. Apart from that we have seen all types of operators. We have seen its code. Now I want you to practice it yourself. I know the video has become very long. But those who have stayed till the end. Their operator's concept must have been cleared. If you have any question in your mind. Then please comment on it. And interact with me. So that's it in this video. See you in the next video. Till then thank you so much.
