Maison > Java > javaDidacticiel > le corps du texte

Opérateurs Java

王林
Libérer: 2024-08-30 15:18:56
original
191 Les gens l'ont consulté

L'article suivant fournit un aperçu des opérateurs Java. L'opérateur Java désigne un symbole qui permet d'effectuer plusieurs opérations sur un ou plusieurs opérandes. Les opérateurs peuvent être +, -, /, *, etc., selon les besoins.

Commencez votre cours de développement de logiciels libres

Développement Web, langages de programmation, tests de logiciels et autres

Il existe différents types d'opérateurs.

  • Opérateurs arithmétiques
  • Opérateurs au niveau du bit
  • Opérateurs d'affectation
  • Opérateur Ternaire
  • Opérateurs d'incrémentation et de décrémentation automatiques
  • Opérateurs de comparaison ou opérateurs relationnels
  • Opérateurs logiques
  • Opérateurs d'équipe

Types d'opérateurs en Java

Vous trouverez ci-dessous les types d'opérateurs en Java :

1. Opérateurs arithmétiques

Les opérateurs arithmétiques sont utilisés pour effectuer plusieurs opérations arithmétiques.

Voici les opérateurs arithmétiques en Java.

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. Opérateurs au niveau du bit

Les opérateurs au niveau du bit sont couramment utilisés pour effectuer des opérations de décalage de bits et des opérations au niveau du bit en Java.

Vous trouverez ci-dessous les opérateurs Bitwise couramment utilisés.

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. Opérateurs d'affectation

Les opérateurs d'affectation sont utilisés pour attribuer des valeurs à certaines variables.

Voici les opérateurs d'affectation en Java.

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. Opérateurs ternaires

En Java, l'opérateur ternaire est principalement utilisé pour le remplacement de conditions if-then-else. Il s'agit d'une instruction d'une seule ligne largement utilisée dans la programmation Java, qui ne prend que 3 opérandes.

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. Opérateurs d'incrémentation automatique et opérateurs de décrémentation automatique

Les opérateurs d'incrémentation automatique et de décrémentation automatique en Java permettent d'incrémenter et de décrémenter les valeurs de 1, respectivement.

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. Opérateurs de comparaison ou opérateurs relationnels

Les opérateurs relationnels sont des opérateurs qui aident à vérifier l'égalité de l'opérande. En plus de cela, ces opérateurs sont également utilisés pour comparer 2 valeurs ou plus.

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. Opérateurs logiques

Les opérateurs logiques en Java sont couramment utilisés dans les expressions booléennes.

Vous trouverez ci-dessous les opérateurs logiques en 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. Opérateurs de quart

Les opérateurs de décalage en Java sont utilisés pour décaler les bits vers la gauche ou la droite en fonction des besoins.

Voici les opérateurs 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

Voyons quelle sera la préséance de ces opérateurs.

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 = += -= *= /= %= &= ^= |= <<= >>= >>>=

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!