java ternary operator or conditional operator
In whole JAVA conditional operator is the only ternary operator.
How To Use It?
It is very simple to use.
First, it checks the condition, if the condition is true then it executes next statement after ? symbol .
If the condition is false then it executes statement after (colon) : the symbol.
Syntax with an example.
example 1.
int a=(10<20) ? 50 : 60;
System.out.println(a);
Output is 50
example 2.
int a=(10>20) ? 50 : 60;
System.out.println(a);
Output is 60
example 3.
(10<20) ? System.out.println("hello") : System.out.println("hi") ;
Output is hello
example 4.
(10<20) ? System.out.println("hello") : System.out.println("hi") ;
Output is hi
No comments:
Post a Comment
Be the first to comment!
Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.
Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.
Regards,
JavaByChetan
Back To Home