Java operators tutorial video – J014
DeegeU Java Course
This video is part of a larger free online class called “Free Java Course Online”. You can find more information about this class on “Free Java Course Online” syllabus.
Try at home
- Type in a program that adds with the unary operator
- Type in a program that tests the first ten numbers to see if they are a factor of 3 (modulus)
Transcript – Java operators tutorial video
Having variables and values in our program doesn’t help us if we can’t modify those values in some ways. We need to be able to perform math operations, like addition and multiplication. We want to change our variables using tools other than assignment. In this lesson, I’m going to show you how to use Java operators in your programs.
There’s only two goals for this lesson. One, know what operators are. Two, know how to use them. This should be a fairly simple lesson. We’ll cover all of them, but operators just the symbols you’re used to using in arithmetic. There’s a few exceptions, but we’ll cover that. We are splitting this operators into two videos. The first one we will cover what are operators. Then we’ll talk about assignment, arithmetic, and unary operators. The next lesson we’ll finish the rest of the operators.
So what are operators? Operators are special symbols that perform operations on one, two or three values or variables. These operators all return a single result. We’ve already been using the plus operator. It’s hard to do anything without addition. The complete list is here. Well the categories of operators. Lets look at each one.
The first one you’ve seen already is the assignment operator. That’s the equals sign. The equal sign says assign some value or variable to another variable. So in this case we’re setting i to a value 42 in the first case. The second case we’re setting i equal to j. The important takeaway here is assignment is one single equals sign. The value returned is the variable set to what ever is on the right hand side. The right hand side can be several operations. Let’s look at code real quick to make that clear.
So we can define a variable called a. And set a equals to 1. That’s the simple assignment. We saw this in the last lesson. The main thing to remember is you’re setting the value of the type on the left to the value on the right. If it’s a reference type, you’re getting the reference assigned. Not the value.
Operations can be chained. They have different rules on direction, but for the most part its what you would expect. Equality is always left gets the value on the right. It has to make sense too. You can’t set a equal to two and then equal to three. You can set a = b = 3. So working from right to left in this case, 3 gets set to b, then a gets set to b. You can control operations with parenthesis. It doesn’t make sense here too much, but controlling the order of operations will make more sense soon. The last thing note is the equals operator makes the expression a Java statement.
Arithmetic operators are the ones you’re most familiar with. We’ll call them the math operators. The first four should be very familiar to you. These are your basic operators. Addition, subtraction, multiplication, and division. The last one is called modulus. When you do division with integers, you can have a remainder. So 5 goes into 6, one time with a remainder of 1. 5 would go into 5 1 time with a remainder of zero. Back to the code.
We’ll use the assignment operator with these. The reason is the math operators aren’t enough to make Java statements. You can make a value with the math operators, but Java doesn’t know what to do with it unless you assign it to something. So we can again use A, and set it equal to 1 + 2. So A equals 3.
Lets control the order of operations again. Lets do 4 div 2 plus 3. We run this and get 5. So the operations are moving from left to right. We do 4 div 2 and get 2. Then we add 3. That is five.
Lets put parenthesis around the 2 plus 3. That means we’ll calculate 2 plus 3, then divide 4 by the result. We should get the answer 0.8. And we run it and get zero. That wasn’t what we expected. What happened here?
So heres a hint. Look at the type of A. It’s an integer. Everything here is an integer, so the div is integer division. 5 goes into 4 zero times, with a remainder of 4. The remainder is lost. So types do matter. Lets switch the div to modulus and run it again.
We get 3 and 4. In the first case we have 4 mod 2, which is zero. No remainder. Then we add 3 to get three. In the second case, we add 2 and 3 to get 5. Then 5 goes into four zero times, with a remainder of 4.
The unary operators perform operations on a single value, and return a single value. The first one is just sign. Makes the value or variable positive or negative.
You’ll want to use parenthesis with these. Otherwise it can get tricky knowing if you meant addition and subtraction or changing sign.
The other four unary operators are prefix and postfix addition and subtraction. So if we need to add 1 to a variable, we can just use plus plus. The plus plus is the same as saying i plus 1 and assigning it to i. Minus minus does subtraction. So why do we repeat the symbols on different sides of the value? Lets play with these and see what happens.
Lets set i equal to 1. Then lets set j equal to 4 plus, plus plus i. On the next line, we’ll do it again but do j equals to 4, plus i plus plus. What do you think the values here will be?
In the first case, we get 6. What is happening is we are getting i, adding one and returning 2. 2 + 4 equals 6.
In the next case we get 5. Here we get i, add it to 4 to get 5, and then add 1 to i. In both cases i ends up equal to 2. The unary operator determines if i gets incremented before or after its used in the arithmetic. If it’s on the left, it’s before. On the right, it’s
after.
Lets do the minus minus operator for completeness. I’m going to change the plusses to minuses like this. Now what will we get?
4 and 5. So in the first one we took i as 1, subtracted 1 which left zero, and then added 0 to 4 giving 4.
The second one we took i as 1, added to 4 giving five, then subtracting 1 from i leaving zero.
Hopefully that made sense. If not, I encourage you to play with the operators in code on your own. You should also try the exercises. If you have questions, feel free to ask in the comments or on DeegeU.com.
Tools Used
- Java
- NetBeans
Media Credits
All media created and owned by DJ Spiess unless listed below.
- No infringement intended
Get the code
The source code for “The Java operators tutorial video (Part 1)” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:
git clone https://github.com/deege/deegeu-java-intro.git
Go to the Support > Getting the Code page for more help.
If you find any errors in the code, feel free to let me know or issue a pull request in Git.
Don’t miss another video!
New videos come out every week. Make sure you subscribe!
Comments
DJ Spiess
Your personal instructor
My name is DJ Spiess and I’m a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I’ve programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I’ve worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.