Java/Language Basics/Arithmetic Operators

Материал из Java эксперт
Перейти к: навигация, поиск

Arithmatic Assignment Operators

 

public class Main {
  public static void main(String[] args) {
    int i = 5;
    int j = 10;
    i += 5; // same as i = i + 5
    j -= 2; // same as j = j - 2
    System.out.println("i = " + i);
    System.out.println("j = " + j);
  }
}
/*
i = 10
j = 8
*/





Arithmetic Operators Example

 

public class Main {
  public static void main(String[] args) {
    int i = 5 + 2;
    int j = i - 1;
    int k = j * 2;
    double l = k / 6;
    System.out.println("i = " + i);
    System.out.println("j = " + j);
    System.out.println("k = " + k);
    System.out.println("l = " + l);
  }
}
/*
i = 7
j = 6
k = 12
l = 2.0
*/





Increment and Decrement Operators

 
public class Main {
  public static void main(String[] args) {
    int i = 10;
    int j = 10;
    i++;
    j++;
    System.out.println("i = " + i);
    System.out.println("j = " + j);
    int k = i++;
    int l = ++j;
    System.out.println("k = " + k);
    System.out.println("l = " + l);
  }
}
/*
i = 11
j = 11
k = 11
l = 12
*/