The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Conditional Operators

Basically, I like to look at Conditional Operators as a quick way of implementing an If Statement. That's really all they are. Take a look at this code.

class Class1{publicstaticvoid main(String[] args){int age =50;System.out.println(age >30?"You are quite old!":"You are still young!");}}

You can see that we've initalised an integer with the value of 50. Our Conditional Operator then checks to see if it's greater than thirty (age > 30 ?). We use a question mark to see if the condition is true. You then write the true and false outputs separated by a colon. That's really all there is to it!