Monday, March 25, 2013

Java;Notes - 7.) Java Operators

In Java you get the usual math operations of:

  • Addition(+)
  • Subtraction(-)
  • Multiplication(*)
  • Division(/)
I am sure by now we know what these do... Other than mess with our brains once we get into Algebra!
"I got it! We will add numbers to English!! It will be marvelous!" - The Devil
Back on topic:

Java and the Modulus:
In java the / operator denotes integer division and so will only divide whole numbers. This is where we get a special division tool called the "Modulus" which looks like: %
If it sounds like something out of Harry Potter your right! This one allows us to magically find remainders of division For example:

  • 15  /  2  = 7
  • 15 % 2 = 1   <---LOOK AT IT GO!
  • 15.0 / 2 = 7.5 
NOTE: If your going to make a calculator to handle Floating Point Numbers its best to do with double instead of int (Which is what I did with my previously posted Calculator Program) 

Java Binary Arithmetic Operators shortcut:
Java also has a nifty built in shortcut for Binary Arithmetic Operators in assignments like so:

x += 4; 
is the same as:
x = x + 4 

In general: Place the operator to the left of what ever sign your going to use!

Increment and Deincrement: 

To get a number / variable to increase or decrease its value by one you simply do this:
  • number++   <--- Adds 1 from variable number
  • number--    <--- Subtracts 1 from variable number

Relational and Boolean operators: 

 Boolean operators will help determine if something is true or false. Think of this as the interesting class in math about "Greater than Less than or Equal to" Since that is what Boolean focuses on. So lets jump into it!
The basic operators are as follows:

  • == (When something is for sure equal to something else)
    • IE: x == 3 (Variable "x" is equal to 3)
  • !=  (Tests inequality)
    • IE: 3 != 7 (3 is not equal to 7)
  • <  (Tests to see if one thing is less than something else)
    • IE: 3 < 7 (is 3 is less than 7)
  • >  (Tests to see if something is greater than something else)
    • IE: 7 > 3 (is 7 greater than 3)
  • <= (Tests to see if something is less than or Equal to something else)
    • 3 <= 7 (Is 3 Less than or Equal to 7)
  • >= (Tests so see if something is Greater than or Equal to something else)
    • 7 >= 3 (Is 7 Greater than or Equal to 3)
  • && (Is the logical "and" operator)
    • The second argument is not evaluated if the first argument determines the value when you combine two expressions with the && operator like so: num1 && num2 
    • If the truth of the first value is determined to be false it is impossible for the result to be true and so num2 is left alone and not evaluated. The following exploit can be used to avoid errors
    • x != 0 && 1 / x > x + y  (Can not / 0) 
  • || (Is the logical "or" operator)
  • ? (Ternary operator: Evaluates Expression1 if the condition is true to the second expression otherwise) 
    • condition ? expression1 : expression2
    • for example: x < y ? x : y
Bitwise Operators:
In java if you want to work with single bits of a string you can do so with Bitwise operators. The typical operators in mention consist of the following:
  • & ("and")
  • |   ("or") 
  • ^  ("xor")
  • ~  ("not")
All of these operators work on bit patterns. Say you have a variable int of "n" then:
int fourthBitFromRight = (n & 0b1000) / 0b1000;
This would return a "1" since the 4th bit from the letter "n" is a 1 and 0 otherwise. Using "&" with the appropriate power of 2 lets you mask out all but a single digit.

There are also the following Operators:
  • >> (Shift Right)
  • << (Shift left)
Which are useful when you need to build up bit patterns to do bit masking. Like so:
int fourthBitFromRight = (n & ( 1 << 3 )) >> 3;

Finally the ">>>" operator fills the top bits with 0. Unlike ">>" which just extends the sign bit into the top bits. 

Note: there is no "<<<" operator.