Java-Conditional Operators / Chapter -10/Operator & Assignments /

In whole JAVA this 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 : 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


Next:Chapter -11/Operator & Assignments / new Operators
Back:Chapter -9/Operator & Assignments / Assignment Operators
Home:

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

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com