Home arrow Java & J2EE arrow Conditionals, Expressions and Other Java Operators

Conditionals, Expressions and Other Java Operators

In our last article we left off with Operators. Since then a strange time fluctuation has occurred, and that is exactly where we pick up. So keep reading to learn about exotic things like conditionals, expressions, and more.

TABLE OF CONTENTS:
  1. Conditionals, Expressions and Other Java Operators
  2. Expressions
  3. Climbing the Old If Else If Ladder
  4. The Switch Statement
By: James Payne
Rating: starstarstarstarstar / 3
October 23, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I recall, by some fluke, you had ended up with more muscles than me (*cough* steroids *cough*) and I put a call into my buddy at the Steroid Usage is a No No Board. To determine if you were a liar, they would have to ask you a series of Boolean questions: True or False -- did you do steroids? Yes or No -- Are you Barry Bonds?

Booleans are black and white; for them, there is no in between. It's either true or false, yes or no, on or off. We will get into how to use them in programming a bit later on. For now take comfort in the fact that they exist.

Conditional Operators

Sometimes you need to know if more than one criteria has been met, or if one of the two criteria has been met. To do that, use conditional operators.


class IamBetter {


public static void main(String[] args) {

int my_muscles = 10;

int your_muscles = 10;

int my_looks = 10;

int your_looks =10;

if((my_muscle > your_muscle) && (my_looks > your_looks)) System.out.println("I really AM better than you in every way!");

In the above example, we are asking the computer if my looks and muscles are better than yours. As you can see by the values, they are. Because both criteria are met, the program will print to the screen: I really AM better than you in every way!

If only one criteria had been met, it would have printed nothing.

Let's say again, that because of your heavy steroid use and because my Pilates exercise builds lean muscles instead of those stupid looking ones that you have, that you appear to have more muscles than me. It wouldn't matter; I can make the program say that I am still better than you.


class IamBetter {


public static void main(String[] args) {

int my_muscles = 10;

int your_muscles = 11;

int my_looks = 10;

int your_looks =10;

if((my_muscle > your_muscle) || (my_looks > your_looks)) System.out.println("I really AM better than you in every way!");

Now that I have changed the criteria so that only one match will suffice, it will still print that I really AM better than you in every way! Again, if neither criteria was true, it would have printed nothing.



 
 
>>> More Java & J2EE Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: