Advertisement
boolean-condition ? first expression : second expression;
class A
{
public static void main(String... ar)
{
int a=10, b=20;
int result =a>b ? 0 : 10;
System.out.println(result);
String str= a<b ? "a is less than b" : "a is greater than b";
System.out.println(str);
System.out.println(a>b ? 10 : 100.50);
System.out.println(a>b ? 'y' : 'n');
}
}
Output
10
a is less than b
100.5
n
class A
{
public static void main(String... ar)
{
A ob= new A();
boolean objectOfAExist=true;
System.out.println(5>10?"yes":"no");
System.out.println(objectOfAExist ? ob : "Object doesn't exist yet");
}
}
no
A@70dea4e
Advertisement
class A
{
public String toString()
{
return "object of A";
}
public static void main(String... ar)
{
A ob= new A();
boolean objectOfAExist=true;
System.out.println(5>10?"yes":"no");
System.out.println(objectOfAExist ?ob: null);
}
}
no
object of A
class A
{
public static void main(String... ar)
{
int a=10, b=20;
System.out.println(a>b ? 0 : 10);
System.out.println(a<b ? "a is less than b" : );
}
}
A.java:8: error: illegal start of expression
System.out.println(a<b ? "a is less than b" : );
^
1 error
class A
{
public static void main(String... ar)
{
float a=10.5f, b=5.5f;
System.out.println(a>b?0:10);
System.out.println(a<b?"a is less than b":null);
}
}
0
null
class Flower
{
public boolean hasAFlower()
{
return true;
}
public String getColor()
{
return "The color of flower is blue";
}
public static void main(String... ar)
{
Flower ob= new Flower();
System.out.println(ob.hasAFlower() ? ob.getColor(): "Doesn't have a flower");
}
}
The color of flower is blue
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement