Vergleichsoperatoren

Einführung in Java

Jim White

Java Developer

Vergleiche

 

 

  • Immer auf true oder false prüfen (Ergebnis ist boolean)

Vergleich des Alters mit minAge

Einführung in Java

Größer als >, kleiner als <

class GreaterThan {
  public static void main(String[] args){
    System.out.println(5 < 6); // Will print true, 5 is less than 6
  }
}
true
Einführung in Java

Größer als >, kleiner als <

// Check if 5 is less than 6
boolean x = 5 < 6; // Value is true, because 5 < 6

// Check if 5 is greater than 6
boolean y = 5 > 6; // Value is false, 5 < 6
Einführung in Java

Größer oder gleich >=

int minSpend = 25;
int total = 25;


// Check if total is greater or equal to minSpend boolean freeDelivery = total >= minSpend; // Value is true, total is equal to minSpend
Einführung in Java

Kleiner oder gleich <=

int minSpend = 25;
int total = 23;

// Check if total is less or equal to minSpend

boolean paidDelivery = total <= minSpend; // Value is true, total is less than minSpend
Einführung in Java

Gleich ==

  • Hier ist == erforderlich, = ist für Zuweisungen
int userAccountNumber = 567346;
int submittedAccountNumber = 456777;

// Check if userAccountNumber is equal to submittedAccountNumber
boolean isUserAccountNumber = useraccountNumber == submittedAccountNumber;
// Value is false

Einführung in Java

Nicht gleich !=

  • Verwende !=
int userHealth = 235;

// Check whether userHealth is not zero
boolean alive = userHealth != 0; // Value is true, userHealth is not zero
Einführung in Java

Vergleichsübersicht

Operator Name Beispiel Ergebnis des Beispiels
> Größer als 6 > 6 false
< Kleiner als 5 < 6 true
>= Größer oder gleich 5 >= 6 false
<= Kleiner oder gleich 5 <=6 true
== Gleich 5 == 5 true
!= Nicht gleich 5 != 5 false
  • Ergebnis aller Operationen ist boolean
Einführung in Java

Lass uns üben!

Einführung in Java

Preparing Video For Download...