Institute of Computer Science
  1. Courses
  2. 2019/20 spring
  3. Object-Oriented Programming (Narva College) (LTAT.NR.003)
ET
Log in

Object-Oriented Programming (Narva College) 2019/20 spring

  • Home
  • Materials
  • Grading
  • Java Glossary
  • Cheat sheet (S1-S6)
  • Source Example
  • Links
Chapter 1

Operators in Java

An operator is a character that represents an action (e.g. + is an arithmetic operator that represents addition). In general, Java has seven types of operators:

  1. basic arithmetic operators
  2. assignment operators
  3. auto-increment and auto-decrement operators
  4. logical operators (will be covered in the next chapter)
  5. comparison (relational) operators (will be covered in the next chapter)
  6. bitwise operators (will not be covered in this material)
  7. ternary operator (will not be covered in this material)

1. Basic arithmetic operators

OperatorMeaningPriority
-unary minus (negative values)highest
*multiplicationmiddle
/divisionmiddle
%remaindermiddle
+additionlow
-subtractionlow

PS! We can use parentheses () to force an evaluation to have a higher priority.

2.Assignment operators

OperatorExampleEquivalent
=x = y;x = y;
+= x += y;x = x + y;
–=x –= y;x = x – y;
*=x *= y;x = x * y;
/=x /= y;x = x / y;
%=x %= y;x = x % y;

Check an example

public class AssignmentDemo {
   public static void main(String args[]) {
      int num1 = 10;
      int num2 = 20;

      num2 = num1;
      System.out.println("= Output: "+num2);

      num2 += num1;
      System.out.println("+= Output: "+num2);

      num2 -= num1;
      System.out.println("-= Output: "+num2);

      num2 *= num1;
      System.out.println("*= Output: "+num2);

      num2 /= num1;
      System.out.println("/= Output: "+num2);

      num2 %= num1;
      System.out.println("%= Output: "+num2);
   }
}

3. Auto-increment and auto-decrement operators

OperatorExampleEquivalent
++x++;x = x + 1;
--x--;x = x – 1;

Useful link: Check examples here.

Operator precedence in Java

Just like in Python and math, operator precedence determines which operator needs to be evaluated first if an expression has more than one operator (e.g. is 2+3*4 equal to 20 or 14?). In table below, the operators with higher precedence are at the top and lower precedence at the bottom.

++ -- !unary operators
* / %multiplicative
+ –additive
> >= < <=relational
== !=equality
&&logical AND
||logical OR
= += -= *= /= %=assignment

Summary: variables, data types, operators & common errors

Self-assessment

Chapter 1
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment