Home arrow Java & J2EE arrow Page 4 - Java Operators

Forming a Relationship with Operators - Java

It's been about a month or so since we finished our beginning Java series, so I'm sure you're ready for more. This time, I am going to teach you to work with operators in Java. When I am finished, you will be able to do complex mathematical equations, add one string of text to another, and build programs so powerful you will be able to put thousands of hard working Americans out of work with the push of a button.

TABLE OF CONTENTS:
  1. Java Operators
  2. Assignment Operators
  3. Doing More Math with Assignment Operators
  4. Forming a Relationship with Operators
By: James Payne
Rating: starstarstarstarstar / 21
October 22, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Admit it, back in the old days when real people answered the phone, you often thought...I bet this operator is hot. Maybe you even flirted a little. Perhaps you even went as far as to try to make a date with the operator, and when she said she didn't know you, you took that as a sign that you should call her back every hour until she did know you. Nice try; you'll be glad to hear then that working with relational operators is quite different from trying to get a date from an operator.

Relational operators compare data to one another. Below is a hot list of relational operators and what they do:


Operator Symbol

What It Does

“==”

Equal To

!=

Not Equal To

>

Is Greater Than

<

Is Less Than

>=

Greater Than or Equal To

<=

Less Than or Equal To

Relational operators will come in handy when we take a look at Conditionals and Loops. For now, here is a sampling of what that relationals look like in code:


class FlexOff {


public static void man(String[] args) {

int my_muscles = 10;

int your_muscles = 1;


if(my_muscles > your_muscles) System.out.println(“My muscles are larger than yours!”);


if(my_muscles < your_muscles) System.out.println(You must be taking steroids.”);


  }

 }

In the above code, we have to value: my_muscles and your_muscles. Each is assigned a numeric value from 1 to 10, with 10 being the greatest. We then have an If-statement (we will go over these in another tutorial) that states if my_muscles are greater than your_muscles, do this. In the above sample,  my_muscles are indeed greater than your_muscles (by a factor of ten no less; I'm practically Hercules) and so the program will print out:

  My muscles are larger than yours!

Had your muscles been bigger than mine, two things would have occurred: 1) Hell would have frozen over and 2) the following text would be written to the screen:

  You must be taking steroids.

Now let's imagine that you were stronger than me. When I had you called up for review by the Steroid Usage is a No No Board, they would ask you, Yes or No, True or False...have you used steroids?

And with that little segue, it's time to introduce you to the Boolean Logical Operators.

Unfortunately for you however, we have run out of time for this tutorial. But never fear; in our next tutorial we will cover the rest of the operators and delve into Statements.

Until then however, feel free to study this table of our remaining Operators:


Operator Symbol

What it Does

&

And (Logical)

|

Or (Logical)

^

XOR (exclusive OR)

||

Or (short circuit)

&&

And (short circuit)

!

Not (logical unary)

&=

AND assignment

|=

OR assignment

^=

XOR assignment

“==”

Equals

!=

Not Equal To

?:

If-then-Else (ternary)

Till next time (plays Batman theme song). POW! BLAM! BIFF! BOP!



 
 
>>> 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: