Java - What is the output: tenary operator with method

Question

What is the output of the following code.

public class Main{

  public static void main(String[] args) {
    int a = 1 == 1 ? odd() : even();
  }
  private static void odd() {
    System.out.println("Odd");
  }

  private static void even() {
    System.out.println("Even");
  }
}


Click to view the answer

Type mismatch: cannot convert from void to int

Demo

public class Main{

  public static void main(String[] args) {
    int aaaaaaa = 1 == 1 ? odd() : even();
  }/* w  w  w  . ja v  a 2  s  .com*/
  private static void odd() {
    System.out.println("Odd");
  }

  private static void even() {
    System.out.println("Even");
  }
}

Note

We need a function which return int value to assign it to variable a.

Related Topic