基本用法
太空船运算符在左运算符较小时返回 -1
,在运算符相等时返回 0
,否则返回 1
:
assert 10 <=> 20 == -1
assert 10 <=> 10 == 0
assert 30 <=> 10 == 1
assert 'a' <=> 'b' == -1
assert 'a' <=> 'a'== 0
assert 'b' <=> 'a' == 1
它等同于 Comparable.compareTo 方法:
assert 10.compareTo(20) == (10 <=> 20)
assert 'a'.compareTo('b') == ('a' <=> 'b')