首頁  >  文章  >  Java  >  Java 中的一元運算符

Java 中的一元運算符

PHPz
PHPz原創
2024-08-30 15:19:071065瀏覽

對於任何程式語言來說,都有大量的運算子、方法和函數可供根據需要使用。基於類別、物件導向的程式語言 Java 提供了多種運算符,Java 中的一種運算符就是「一元運算符」。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

一元運算子可以是只接受一個運算元並執行將值遞增或遞減 1 的簡單工作的運算子。此外,一元運算子也會對表達式進行取反運算,布林值可以取反。

一元運算子的型別

有五個一元運算子能夠執行各種操作。下面給出了五個一元運算子的列表:

  • 一元加號,用「+」表示。
  • 一元減號,用「-」
  • 表示
  • 一元自增運算符,用「++」表示。
  • 一元自減運算符,用「–」表示
  • 邏輯補碼運算符,用「!」表示

一元運算子與二元運算子有很大不同,二元運算子接受兩個運算元。這些運算子就像是用於對運算元執行某些操作的特殊符號;這裡的運算元是變數和值。

Java 中的一元運算符

1.一元加

簡單地傳回正值。無論值是什麼,一元加都不會傳回負數形式。

2.一元減號

就像加運算子傳回正值一樣,一元減運算子傳回相同值的負形式。對於上面解釋的一元運算符,我們將示範一個範例,其中我們將實作一元加號和減號運算符。

代碼:

class unary_ops {
public static void main(String[] args) {
int num = 6;
num = +num;
System.out.println(num);
num = -num;
System.out.println(num);
}
}

程式碼解釋:我們在上面的範例中示範了加號和減號一元運算子。我們有我們的類,然後是主類,並且我們聲明了一個值為 6 的簡單整數。然後我們將 num 分配給一元加運算子。然後我們列印了結果,這將是簡單的純 6。然後我們將相同的變數傳遞給一元減運算符,並且這裡的值會改變。我們已經用 print 語句印了輸出,預計為 -6,表示負 6。執行上述程式碼後,6 和 -6 是預期輸出。

輸出:

Java 中的一元運算符

3.一元自增運算子

顧名思義,這個一元運算子所做的就是將值加1的運算。無論變數的值是多少,經過增量運算子傳遞後,值都會加1。一元增量運算子可以是後來根據增量操作發生的時間分為兩種:

  • 後遞增:值先被處理,然後再遞增。後自增時,無論數值是多少,都會先用來計算,然後再加一。
  • 預自增:相反,預自增是先自增,然後再計算增量後的數值。

4.一元自減運算子

就像增量運算子將值加一一樣,一元減量運算子將變數值減 1。

與自增運算符類似,自減運算子有兩種變體:

  • Post-Decrement: 使用後置形式的減量運算符,首先使用該值,然後更新該值。
  • 預減: 採用前綴形式,數值先減一,再用於任何計算操作。

示範上述自增和自減運算子的使用。

代碼:

class unary_ops {
public static void main(String[] args) {
int num = 6;
num--;
System.out.println(num);
num++;
System.out.println(num);
}
}

程式碼解讀: 與內部主類別相同,整數num,值為5。首先,我們將減量運算子傳遞給變量,作為num——並且將列印該值。稍後我們將相同的計算值傳遞給增量運算符,結果將被列印。我們的原始值為 6,執行後輸出將為「5 6」。它會先減到 5,然後再加 1,再回到 6。

輸出:

Java 中的一元運算符

5.邏輯補運算子

該運算子用於反轉任何變數的布林值。前任。如果變數的布林值為 true,則在使用邏輯運算子傳遞後,它將被反轉為 false。

Code:

class unary_ops {
public static void main(String[] args) {
boolean bvalue = false;
System.out.println(bvalue);
System.out.println(!bvalue);
}
}

Code Interpretation: We demonstrated a Logical Complement operator using the Boolean data type. In our class, we have the main class within and our simple boolean variable, which holds the value of false. In our first print statement, we printed the original value and later passed the logical complement operator; as you can see, we’ve used the “!” symbol. This implementation will invert the value of the boolean variable, resulting in a changed output of true.

Output:

Java 中的一元運算符

Below are the cases, which if executed, will result in errors:

  • It is important to understand that these increment and decrement operators can only be used with variables, not constant values. Implementing these operators with contact values will result in an error. Another interesting point is how an unexpected error will occur if you use these operators in nested form. Not acceptable nested form is: int x = ++(++y);
  • Operations on Final Variables: Declaring a final variable is as easy as adding a final keyword before the data type, which results in a type variable whose value cannot be changed. Keeping this in mind, we cannot implement these operators on the final variable, as these operations result in a change in value.
  • For Boolean values: We can implement these operators on all types of primitive data types except Boolean values.
  • In case of any of the scenarios mentioned above, it will result in errors.

Conclusion

There are 5 unary operators and with pre and post as two varieties. We understood each operator with a specific definition and usage. Along with an explanation, we have programs for respective operators, screenshots, and code interpretation. And some essential tips to wisely implement these operators.

以上是Java 中的一元運算符的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn