English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Programa Java que convierte Integer a un valor booleano especificado

Para convertir Integer a valor booleano, hemos utilizado el siguiente objeto Integer.

Integer one = 1;
Integer two = 1;
Integer three = 0;

Usamos if anidados-La declaración else se utiliza para mostrar valores true o false. Aquí, el valor " 1"es igual a" 2"es el mismo que"1; por lo tanto, el siguiente else-if es viable.

else if (one.equals(two)) {
   System.out.println(true);
}

La pantalla muestra "true", por lo que convertimos Integer a boolean.

Ahora veamos el ejemplo completo para aprender cómo convertir Integer a Boolean.

Ejemplo

public class Demo {
   public static void main(String[] args) {
      Integer one = 1;
      Integer two = 1;
      Integer three = 0;
      //Entero a Booleano
      if (one == null) {
         if (two == null) {
            System.out.println(true);
         } else if (three == null) {
            System.out.println(false);
         }
      } else if (one.equals(two)) {
         System.out.println(true);
      } else if (one.equals(three)) {
         System.out.println(false);
      }
   }
}

Resultado de salida

Verdadero