Rumah  >  Artikel  >  Java  >  Operator Java

Operator Java

王林
王林asal
2024-08-30 15:18:56354semak imbas

Artikel berikut menyediakan garis besar untuk Operator Java. Operator Java menandakan simbol yang membantu dalam melaksanakan beberapa operasi pada satu atau lebih daripada satu operan. Operator boleh menjadi +, -, /, *, dsb., bergantung pada keperluan.

Mulakan Kursus Pembangunan Perisian Percuma Anda

Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain

Terdapat pelbagai jenis operator.

  • Operator Aritmetik
  • Operator Bitwise
  • Operator Tugasan
  • Operator Ternary
  • Operator autokenaikan & penurunan automatik
  • Operator perbandingan atau Operator Perhubungan
  • Operator Logik
  • Operator Syif

Jenis Operator dalam Java

Diberikan di bawah adalah jenis operator dalam java:

1. Operator Aritmetik

Operator Aritmetik digunakan untuk melakukan beberapa operasi aritmetik.

Berikut ialah pengendali aritmetik di Jawa.

Operator Name/ Symbol Definition Example
Addition(+) It helps in adding two values. A+B
Subtraction(-) It helps in subtracting two values. A-B
Modulus(%) It helps in getting the remainder obtained by dividing two values. A%B
Multiplication( * ) It helps in multiplying two values. A*B
Division ( / ) Helps in dividing two values. A/B

2. Operator Bitwise

Pengendali bitwise biasanya digunakan dalam melaksanakan operasi anjakan bit dan bitwise dalam Java.

Di bawah ialah pengendali Bitwise yang biasa digunakan.

Operator Name/ Symbol Definition Example
Bitwise AND operator (&) It helps in comparing 2 operand’s corresponding bits. 1 will be returned if both the bits are 1; else 0 will be returned. A&B
Bitwise OR operator (|) It helps in comparing 2 operand’s corresponding bits. 1 will be returned if one of the bit is 1; else 0 will be returned. A|B
Bitwise XOR operator (^) It helps in comparing 2 operand’s corresponding bits. 1 will be returned if the corresponding bits are dissimilar, else 0 will be returned. A^B
Bitwise complement operator (~) It helps in inverting the pattern of bits. That is, 1 will be changed to 0 and 0 will be changed to 1. ~B

3. Operator Tugasan

Operator Tugasan digunakan untuk memberikan nilai kepada pembolehubah tertentu.

Berikut ialah pengendali tugasan di Jawa.

Operator Name/ Symbol Definition Example
-= It helps subtract the right and left operators, thereby assigning the obtained result to the operator on the left. A-=B
/= Helps in dividing the right and left operators and thereby assigning the obtained result to the operator in the left. A/=B
*= It helps multiply right and left operators and thereby assign the obtained result to the operator on the left. A*=B
+= It helps add right and left operators and thereby assign the obtained result to the operator on the left. A+=B
^= Left operand value will be raised to the right operator power. A^=B
%= A modulus operator will be applied. A%=B

4. Operator Ternary

Di Java, pengendali Ternary digunakan terutamanya untuk penggantian keadaan if-then-else. Ia ialah pernyataan satu baris yang digunakan secara meluas dalam pengaturcaraan Java, yang hanya memerlukan 3 operan.

Operator Name/ Symbol Definition Example
condition?v1: v2 V1 will be returned if the condition mentioned is true, and v2 will be returned if the condition is false. A>B?A:B

5. Operator Penambahan Auto dan Operator Penurunan Auto

Pengendali Auto Increment dan Auto Decrement di Java membantu dalam menambah dan mengurangkan nilai sebanyak 1, masing-masing.

Operator Name/ Symbol Definition Example
++ The value will be incremented by 1. A++; It is similar to A+=1
The value will be decremented by 1. A–; It is similar to A-=1

6. Operator Perbandingan atau Operator Perhubungan

Pengendali perhubungan ialah pengendali yang membantu dalam menyemak kesamaan operan. Selain itu, operator ini juga digunakan untuk membandingkan 2 atau lebih nilai.

Operator Name/ Symbol Definition Example
equals to(==) If the left operand is equal to the right operand, true will be returned; else, false. A==B
not equal to(!=) If the left operand is not equal to the right operand, true will be returned; else, false. A!=B
less than(<)  If the left operand is less than the right operand, true will be returned; else, false. A
greater than(>) If the left operand is greater than the right operand, true will be returned; else, false. A>B
greater than or equal to(>=) If the left operand is greater than or equal to the right operand, true will be returned else, false. A>=B
Less than or equal to(<=) If the left operand is less than or equal to the right operand, true will be returned else, false. A<=B

7. Operator Logik

Operator Logik dalam Java biasanya digunakan dalam ungkapan Boolean.

Di bawah ialah pengendali logik dalam Java.

Operator Name/ Symbol Definition Example
Conditional AND(&&) If both the Boolean expressions are satisfied, true will be returned else, false. AD
Conditional OR(||) If any of the Boolean expression is satisfied, true will be returned; else, false. AD
Logical NOT(!) It helps in inverting the operand’s logical state. That is, 1 will be changed to 0 and 0 will be changed to 1. !B

8. Operator Shift

Operator Shift dalam Java digunakan untuk mengalihkan bit ke kiri atau kanan berdasarkan keperluan.

Berikut adalah operator Shift.

Operator Name/ Symbol Definition Example
Left Shift Operator ( << ) X in binary notation will be shifted y bits left by shifting the zeroes from the right. A<<2
Right Shift Operator ( >> ) X in binary notation will be shifted y bits right by discarding shifted bits. B>>2
Unsigned Right Shift Operator ( >>> ) X in binary notation will be shifted y bits right by discarding shifted bits and shifting the zeroes from the right. A>>>2

Mari kita lihat bagaimana keutamaan pengendali ini.

Operator Precedence
Postfix Operators Exp++ Exp–
Unary Operators ++Exp –Exp +_Exp –Exp~ !
Multiplicative Operators * / %
Additive Operators + –
Shift Operators << >> >>>
Relational Operators < > <= >= instanceof
Equality Operators == !=
Bitwise AND Operator &
Bitwise exclusive OR Operator ^
Bitwise inclusive OR Operator |
Logical AND Operator &&
Logical OR Operator ||
Ternary Operators ?:
Assignment Operators = += -= *= /= %= &= ^= |= <<= >>= >>>=

Atas ialah kandungan terperinci Operator Java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:Pengecam JavaArtikel seterusnya:Pengecam Java