+ 12
What is error in this code ? Can anyone help me?
18 Answers
+ 33
Amruta  use it like this:
public class Program
{
    public static void main(String[] args) 
{
System.out.println((10>9)?"10>9":"10<9");
  }
}
Ternary operator has return type so its must return a value from ans either one of them.......
Your function works on void method i.e print directly and it doesn't support void invocation inside it so it shows error....
Well I too didn't know that much.....so sorry๐
Hope this helps.....๐
+ 7
Ketan It works perfect 
But why its not working when using printing statements in ternary statement?
+ 6
Canche Emmanuel , Gaurav Agrawal thanks for help
+ 4
Ketan thanks for help
+ 4
Ron Alvarez Yes I know that but I wanted to know the reason behind the error
+ 4
System.out.println((10>9)?"10>9":"10<9"); 
Here you may put a string or a variable.
I should do it this way:
result = (10>9)?"10>9":"10<9" ;
System.out.println(result)
+ 3
It is a compile-time error for either operand expression to be an invocation of a void method and System.out.println is void (does not return any value).
https://stackoverflow.com/questions/15977031/java-ternary-without-assignment
+ 3
Sevak Ekizyan thanks for help
+ 3
System.out.println((10>9)?"10>9":"10<9"); Here you may put a string or a variable. I should do it this way: result = (10>9)?"10>9":"10<9" ; System.out.println(result)
+ 2
public class Program
{
    public static void main(String[] args) {
       /*here you can declare 2 variables for 10 & 9 values*/
      if (10>9)
       {
       System.out.println("10>9");
       }
       else
       {
      System.out.println("10<9");
       }
   }
}
/*I think! in java it should be like that๐*/
+ 2
The ternary operator should always provide as a value. If you are going to make statements using ternaries you will get errors. So the ternary operator should go inside the println() method.
+ 2
S1s yes its will work for ternary๐
+ 1
I did it this way...is it okay for you...?
public class Program
{
    public static void main(String[] args) {
       int a=9;
       int b=10;
        
           
     System.out.println("10>9?");
        
    if(10>9)
    
        { System.out.println("10>9");
        }
        else{
        System.out.println("10<9");
    }
}
}
+ 1
u can use if/else for better understanding
+ 1
It seems that kind of code is not used in Java...but if you try to do it that way it becomes more complicated
+ 1
S1s your code above runs perfectly
0
good great



