java基础-运算符
1.是什么
运算符是一些特殊符号,主要用于数学函数、一些类型的赋值语句、逻辑比较方面
2.赋值运算符(=)
int a = 10;
3.算数运算符(+、-、*、/、%)
public class IntegerDiv { public static void main(String[] args) { int a = 10; int b = 3; System.out.println(a / b); } }
public class ModCalc { public static void main(String[] args) { int num = 10; System.out.println(num % 2); System.out.println(num % -3); System.out.println(num % 4); System.out.println(num % 5); System.out.println(num % -6); } }
4.比较运算符
public class CompareOprt { public static void main(String[] args) { int a = 10; int b = 15; int c = 10; System.out.println(a > b); System.out.println(a >= b); System.out.println(a < b); System.out.println(a <= b); System.out.println(a != b); System.out.println(a != c); System.out.println(a >= c); System.out.println(a <= c); System.out.println(a == b); System.out.println(a == c); } }
5.逻辑运算符(与、或、非)
public class BooleanOprt { public static void main(String[] args) { boolean a = true; boolean b = false; System.out.println(a && b); System.out.println(a & b); System.out.println(a || b); System.out.println(a | b); System.out.println(a || (10 / 0 > 1)); System.out.println(a | (10 / 0 > 1)); } }
6.运算符的优先级
public class PriorityExample { public static void main(String[] args) { int a = 1 + 3; int b = 2; boolean both = a > 0 && b > 0; System.out.println(both); boolean result = a < b + 5; System.out.println(result); } }