這裏就用實際的例子介紹一下,該符號到底是什麼意思:
public static void main(String[] args) {
int a = 1;//0001
int b = 2;//0010
int c = 4;//0100
b |= c;
System.out.println(b);//6
a |= c;
System.out.println(a);//5
}
從上面的代碼片段可以看出:
“|=”類似於“+=”,就是先進行“或”運算,然後再將值賦給前面的變量。就是這麼簡單,你看明白了嗎?