Condition without if and switch

I have this code:

public static void main(String[] args){
    boolean greeting = true;        //or false
    if(greeting)
        hello();
}

public static void hello(){
    System.out.println("Hello")
}

I want to call the hello method without using (if, switch) if the greeting value is set to true

Can I rewrite this program without using an if or switch statement? if so, how?

+4
source share
8 answers

you can use enum

enum Greeting {
    GREETING(() -> System.out.println("hello")),
    NO_GREETING(() -> {});

    private final Runnable greeting;

    private Greeting(Runnable r) {
        greeting = r;
    }
    public void greet() {
        greeting.run();
    }
}

and then

public static void main(String[] args) {
    Greeting gr = Greeting.GREETING;   // or Greeting.NO_GREETING
    // or if you insist on the boolean
    // Greeting gr = (greeting) ? Greeting.GREETING : Greeting.NO_GREETING;
    gr.greet();
}

It can also be expanded to have things like

CORDIAL_GREETING(() -> System.out.println("hi wow so nice to see you"))

in the listing.

The ternary operator in the comment, of course, is not completely different from if / else.

+4
source

Can be used optionally:

public static void main(String[] args){
    Optional<Boolean> greeting = Optional.of(false);
    greeting.filter(g->g).ifPresent(g->hello());
}
public static void hello(){
    System.out.println("Hello");
}
+3
source

:

public static void main(String[] args)
{
    boolean b = true;
    Predicate<Boolean> p = s -> {hello();return true;};
    boolean notUsefulVariable = b && p.test(true);      //will be called
    b = false;
    notUsefulVariable = b && p.test(true);              //will not called


}

public static void hello(){
    System.out.println("Hello");
}

while

b = true;
while(b)
{
    hello();
    break;
}
+2

:

public static void main(String[] args){
    boolean greeting = true;        //or false
    while(greeting) {
        hello();
        break;
    }
}

, , ( , ).

+2

greet() , :

public static void main(String[] args) {
    boolean greet = true;
    boolean dummy = greet && greet();
}

public static boolean greet() {
    System.out.println("Hello");
    return true;
}
+1

if. .

  • switch. if .
  • ?. if.
  • . Map, . .

    myMap.get( > 3).execute();

myMap -

 Map(true) = block of code to `execute()` if true.
 Map(false) = block of code to `execute()` if false.

. .

--- , :) ---

null, null , String.

public class Hello {

  private static Map<Boolean, String> response;


  public static void main(String[] args) {
     // setup the response map
     response = new HashMap<>();
     response.put(Boolean.TRUE, "Hello");
     response.put(Boolean.FALSE, "");

     boolean value = true;
     System.out.println(response.get(value));
  }

}

public class Hello {

  private static Map<Boolean, Runnable> response;

  public static void main(String[] args) {
     // setup the response map
     response = new HashMap<>();
     response.put(Boolean.TRUE, new Runnable() {
       public void run() {
         System.out.println("Hello");
       }
     });
     response.put(Boolean.FALSE, new Runnable() {
       public void run() {
       }
     })

     boolean value = true;
     response.get(value).run();
  }

}

System.out.println((condition) ? "Hello" : "");

, , null.

(condition) ? null : "hi";

, null hi , .

0

, .

: greeting true false, vairable.

: boolean greeting = true;, if .

public static void main(String[] args){
    boolean greeting = true;
    // if(greeting)   ---ignores with Compiler
         hello();
}

, : boolean greeting = false;, if if.

public static void main(String[] args){
    boolean greeting = false;
    // if(greeting)   ---ignores with Compiler as Dead Code
    //     hello();   ---ignores with Compiler as Dead Code
}

: greeting true: if. greeting false: if hello(). , if, .

0

, .

(condition ? value1 : value2) , void, , , value1 value2 , hello() . , , :

public class MyClass {

    public static void main(String[] args){
        boolean greeting = true;        //or false
        checkIfGreetingIsTrue(greeting);
    }

    public static void hello(){
        System.out.println("Hello");
    }

    public static Void checkIfGreetingIsTrue(boolean greeting) {
        return greeting ? callHelloInMyBody() : null;
    }

    public static Void callHelloInMyBody() {
        hello();
        return null;
    }
}
-1

Source: https://habr.com/ru/post/1693073/


All Articles