Arvutiteaduse instituut
  1. Kursused
  2. 2018/19 kevad
  3. Objektorienteeritud programmeerimine (Narva Kolledž) (LTAT.NR.003)
EN
Logi sisse

Objektorienteeritud programmeerimine (Narva Kolledž) 2018/19 kevad

  • Home
  • Materials
  • Java Glossary
  • Source Example
  • Cheat sheet (S1-S6)
  • Grading
  • Links
Session 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 next session)
  5. comparison (relational) operators (will be covered next session)
  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. 2+3*4 is 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

Session 1
  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused