Java Operators

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.

So look out global economy, here we come.

Operators

There used to be a time when you called "Information" on the phone and you heard a real, honest-to-goodness voice. All that is done with. Now you talk to a computer, and the funny thing about computers is that they have no ears. So when I call up and, for example, ask for the number to Annie’s Pizza, it’s little surprise that the “operator” comes back to me with the number to Danny’s Pizza, no matter how much emphasis I place on the A in Annie.

Unlike modern computer operators, Java Operators aren’t annoying, and like the phone operators of old, are actually helpful. You have used operators your entire life, and their function is simple: they allow you to manipulate data. And let’s face it, what good is data if you can’t manipulate it?

There are several types of operators, but before we go into them, you should understand a little thing we in the biz call operator precedence. Precedence determines in which order operators execute. If that’s not clear, it will be in a few moments.

But first, look at this beautiful table designed by yours truly:


Operator Type

Operator Precedence

postfix

expr++ expr–

unary

++expr –expr +expr -expr ~ !

multiplicative

* / %

additive

+ -

shift

<< >> >>>

relational

< > <= >= instanceof

equality

“==” !=

bitwise AND

&

bitwise exclusive OR

^

bitwise inclusive OR

|

logical AND

&&

logical OR

||

ternary

? :

assignment

“=” += -= *= /= %= &= ^= |= <<= >>= >>>=

The above table lists the operators and their precedence in order.

{mospagebreak title=Assignment Operators}

Variables with no data are just like frosty cold mugs with no soda in them –worthless. Sure, your girlfriend could throw them at you when you forget to take out the garbage or mention how pretty her sister looks. But when mugs and variables were invented, it was because their creator wanted an object that would hold something. In the case of mugs, it’s beer, and in the case of variables, it’s data. That’s where certain kinds of operators come in.

To fill a variable with data, you use one of the assignment operators, the simplest of which is the = symbol.


my_iQ = 250;

The above code assigns the value 250 to my_iq (I didn’t want to be boastful, so I lowered the amount). If I later decided what the heck, why not be boastful, I could always do this:


my_iq = 250;

my_iq = my_iq + 50;

This would first assign the value 250 to my_iq, then add 50 to the value of my_iq, leaving it at 300, which is what it truly is (wink wink). A simpler way to write the above code is like this:


my_iq = 50;

my_iq += 50;

If I were drinking the night before, I could also use the subtraction assignment operator to show my true IQ:


my_iq = 300;

my_iq -= 250;

The code above would show that alcohol has reduced my monstrous IQ to a feeble 50.

If you are working with text or strings, you could do the following:


class MyName {

public static void main(String[] args){

String first_name = “James”;

String second_name = “Payne”;

String full_name = first_name + second_name;

System.out.println(full_name);

  }

}

The above sample will print out the text: “James Payne” to your monitor.

{mospagebreak title=Doing More Math with Assignment Operators}

Below is a list of Assignment and Arithmetic Assignment Operators and what they do:


Operator

What it Does

+

For addition

-

For subtraction

*

For multiplication

/

For division

%

For modulation

++

For incrementing

For decrementing

+=

For addition assignments

-=

For subtraction assignments

*=

For multiplication assignments

/=

For divisional assignments

%=

For modulus assignments


If you want to incrementally increase a number, you can do so this way:


my_weight = 4000;

my_weight++;

This would increase my weight from 4000 to 4001. If I stood up, lifting my enormous body, I could adjust my weight this way:


my_weight = 4001;

my_weight–;

Now my weight is back down to a fearsome 4000. Soon you won’t be able to see me when I turn sideways. Dealing with increments/decrements can get tricky for some people, but not us with our enormous brains. The reason they can become tricky is because of operator precedence. Observe the following two coding samples:


my_weight = 4000;

my_new_weight = ++my_weight;

The above would result in my weight being 4001. This is because the ++ takes precedence, adding 1 to my weight before adding my_weight to my_new_weight. If I had placed the ++ on the opposite side of my_weight I would have received a different result.


my_weight = 4000;

my_new_weight = my_weight++;

This would result in my_new_weight carrying the value 4000. This is because it adds the value of my_weight to my_new_weight prior to adding 1 to the value of my_weight.

{mospagebreak title=Forming a Relationship with Operators}

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!

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan