Join David Gassner for an in-depth discussion in this video Using Java operators, part of Java 8 Essential Training.
Here are some of the most common assignment and math operators. When you declare a variable in Java you always start with the data type followed by the name of the variable. But you don't have to immediately assign a value. The default value will depend on whether the variable is a primitive or an object. But if you want to assign a value, you use the single "=" character as the assignment operator. The data type and variable name go on the left, and the value on the right.
This would be a simple assignment, but you can also execute mathematical operations as you assign a value. These would be expressions that evaluate to the correct data type. The + operator is for addition, the - operator for subtraction, the * for multiplication, and the / for division. There's also the % character, which is the remainder operator, also known in some languages as the modulus operator.
Here are some other things you can do with assignments and mathematical operations. I'm starting with an integer value of 10. You can increment and decrement that value with a ++ or a --. And that operator can go either before or after the variable name. I'll talk about the effect of that in just a moment. You can also increment or decrement and assign values at the same time using these operators: += means assign the value and execute the math operation, - = does the same thing for subtraction, and there's also *= and /=.
Now, about placing the increment and decrementing operators before or after a variable name. Once again I'm starting with an integer value of 10. If you place the ++ operator after the variable name that's called a postfix assignment. You're evaluating the value, and then executing the math. In this example I'd be outputting a value of 10 but then resetting the value to 11. If you move the operator before the variable name, that's called a prefix.
Then you're executing the math and then evaluating the value. And then your output and your new value would be the same. The equality operators are used to compare values to each other. The == operator is called the equality operator. With primitive values, it compares the actual values. With reference variables, though, that is, variables that point to objects, you're comparing whether the two variables are pointing to the same object, not necessarily whether they have the same value.
The inequality operator is !=. Some Java developers like to call the exclamation mark the bang operator, but again, this simply means not equals to. And it has the same rules as the equality operator. Primitive variables are evaluated by their values, and reference variables by whether they point to the same object. In addition to == and !=, you also have greater than, less than, greater than or equal, less than or equal, and a special operator called "instanceof," that asks whether an object is an instance of a particular class.
So, for example, if I have a String, and I have a condition of "s instance of java.lang.String," that's the fully qualified name of that class, then my condition would be true, and I could say yes, this variable is a String. One of the exceptions to comparing values is with Strings. Strings cannot be safely compared to each other using the equals operators. It'll work in some circumstances and not in others. And I'll describe the details of that later in this course.
But I'll just say now that if you want to compare Strings to each other, you shouldn't use the equals or not equals operators. Instead you should use the String classes equals method. There is one version of this method that's case sensitive, this one, and another version that compares values without case sensitivity. And again I'll describe those details a little bit later on. The result of this code would be "They match!" And then finally there are the logical operators. The && is the And operator, the || character is the Or operator, and you could use those to combine two conditional expressions and get a result, and then there's the Ternary operator that looks like this.
You examine a boolean condition, and then after the question mark, you set a value you want to assign if the condition is true, and after the colon, a value you want to assign if the condition is false. This is short-hand for an if-then conditional statement. Some developers love using this expression. I tend to avoid it. I tend to use a more verbose if-then statement because I think it's easier to read and maintain later on. But this expression absolutely works and is very popular in the Java programming world.
So that's a summary of the most commonly-used operators in Java. There are a few others include operators known as bitwise operators, but they're used less commonly, and I encourage you to look at the Java documentation for a complete look at all of the operators that are available.
- Understanding the history and principles of Java
- Installing Java, IntelliJ IDEA, and BlueJ
- Creating a Java project
- Working with variables, values, and expressions
- Working with object data types
- Building, comparing, and parsing strings
- Debugging and exception handling
- Creating loops and reusable code
- Passing arguments by reference or value
- Using simple and complex arrays
- Creating custom classes
- Understanding inheritance and polymorphism
- Managing files with Java libraries
- Documenting code with Javadoc
- Packaging classes in JAR files