Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
There are equality and relational operators that let you compare values to each other. There are mathematical operators that let you execute common mathematical process, Conditional operators that let you define complex conditional processes, and the ternary operator which is a special operator I'll describe in a later video when I discuss conditional processing. The most common operator that you'll find yourself using is the Assignment operator. The single equals character is the assignment operator. You put the variable you're assigning to on the left and the value on the right.
There is another single character operator worth knowing about the modulus or remainder operator. Java uses the % character for this. If you take a value of 10 and you take the modulus of 5 that returns a reminder of 0. So those are the five primary math operators. You can combine these math operators with assignment too. Once again starting with an initial value of 10, let's take look at how you can increment and decrement values. The ++; operator means add one, so adding 1 to 10 gives you 11, the - -; operator goes in the other direction decrementing by one and that would give you a result of 9.
Here is how you can combine math operators with assignment, += means take the current value of this variable and add whatever is on the right side. So intValue +=5; would be 15. intValue -=5; would be 5, *= 5; equals 50 and /= 5; would result an intValue having a value of 2. Be aware that when you use the /= you need to make sure that you're assigning a value that matches the data type of the original variable.
When you use the increment and decrement operators you can place them either before or after the variable name, the operational result will be a little bit different depending on the position of the operator. Once again starting with the value of 10, a Postfix operator looks like this intValue ++, when you place the ++ operator after the variable you're saying first evaluate the variable and then execute the mathematical operation, because I've wrapped this inside a print line command I'm going to be outputting a value and changing it's value, but you need to know that with this Postfix syntax the evaluation happens first and only after that does the math happen.
The result would be an output of 10, but then after the statement is complete the new value of the variable would be 11. If you move the ++ operator before the variable name that's called a Prefix or a Unary operation, this would mean do the math then evaluate the variable. And in this case the output and the new value would be the same. Java's relational operators, that is the operators as you used to compare values to each other look exactly as you would expect. You have >, <, >= and <= values.
There is also a special operator called instanceof it's all lower case and all one word. You use the isntanceof operator to determine whether a variable is a member of particular class. Here is an example I declare a variable with a data type of String and I set it to some value. Now, I want to test that variable and ask whether it's an instance of a particular class. Typically when you use the instanceof operator you refer to the class by its fully qualified strength, its package, and its class name.
The String class is a member of the java.lang package, so the complete evaluation of (s instanceof java.lang. String) means is s a String and if so then I would output the String that's in the middle of if clause. One very important thing to know about comparisons is that you can't use the traditional comparison or relational operators when you're working with Strings. Let's take a look at this code, there are two Strings named s1 and s2, they have identical values of "Hello"; and now I put in some conditional code, I ask the question if (s1 ==s2) then output ("They match"); and otherwise output ("No match!);.
Speaking of equality, here is how you compare values when you're working with primitives. The equality operator is double equal sign. Be sure you're using double equals not single equals, if you use single equals you'll be assigning the value and not comparing it. The inequality operator is an exclamation mark and the equal sign. So this syntax is asking if this value is not equal to that. And finally you can also use the exclamation mark on it's own to reverse the value of a Boolean Expression. A Boolean Expression evaluates to either true or false, when you apply the exclamation mark before it that reverses the value.
If it's started as true it becomes false and if it started as false it becomes true. You'll hear some developers refer to the exclamation mark as the bang character B-A-N-G. And so if they were reading this code it would be if bang this then do something regardless of what you call it bang or the exclamation it reverses the logic, changing a Boolean value from true to false or vice versa. Here are some conditional operators that you can use in complex logical operators.
The double ampersand is Conditional AND. In this code the output would only happen if both this and that are true. You can also do a Conditional OR using the double pipe character, in this code the output is going to happen if either of those variables equates to true. I'll show you more about how to combine these conditional operators with various types of conditional expressions in later videos, but you should now have a good sense of the kinds of operators that are available in Java and how similar they are to other C-style languages.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101830 Viewers
61 Video lessons · 88581 Viewers
71 Video lessons · 72401 Viewers
56 Video lessons · 104095 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.