Part 2: Operators

Apr 12, 2017
Part 2: Operators
  • Crash Course to Java

    Part 2: Operators



    Operators are used to use and manipulate variables.

    Section A: Arithmetic
    Arithmetic operators are used in mathematical expressions in the same way you would use them in Algebra. Say you have two integers: A has a value of 10, B has a value of 20.

    + (Addition): A + B would give you 30.

    - (Subtraction): B - A would give you 10.

    * (Multiplication): A * B would give you 200.

    / (Division): B / A would give you 2.

    % (Modulus): Modulus divides and returns the remainder. B % A would give you 0.

    ++ (Increment): Increase the value by 1. B++ would make B 21.

    -- (Decrement): Decrease the value by 1. B-- would make B 19.

    Section B: Relational
    Relational operators are all concepts you'll probably easily pick up, and you've probably seen some of them in algebra as well. They act as a boolean which can then be used in if statements, etc.

    == (Equal to): Checks if two values are equal. A == B is false.

    != (Not equal to): Checks if two values are unequal. A != B is true.

    > (Greater than): Checks if the left value is greater than the right value. A > B is false.

    < (Less than): Checks if the left value is less than the right value. A < B is true.

    >= | <= (Greater/Less than or equal to): Like >/< but will also be true if the values are equal. A >= B is false. A <= B is true.

    Section C: Logical
    Logical operators are used to return a boolean based on the boolean result of multiple expressions.

    && (And): Returns true only if both/all expressions are true. A <= B && A >= B is false. A <= B && A == 10 is true.

    || (Or): Returns true if any of the expressions are true, else returns false. A <= B || A > 50 is true. A > B || B == 20 is true. A > B || B == 5 is false.

    ! (Not): Reverses the expression it is applied to. !(A > B) is true. !(A < B) is false.

    Section D: Assignment
    Assignment operators are used to assign value to variables.

    =: The most basic assignment operator. Just sets value. C = A + B would set C to 30.

    +=: Adds the right value to the left value and then sets the left value to the result. B += A would set B to 30.

    -=: Subtracts the right value from the left value and then sets the left value to the result. B -= A would set B to 10.

    *=: Multiplies the left value by the right value and sets the left value to the result. B *= A would set B to 200.

    /=: Divides the left value by the right value and sets the left value to the result. B /= A would set B to 2.

    Section E: Misc
    There are a few other operators outside of those categories.

    (? : ) Conditional Operator: A conditional operator, also known as the ternary operator, is used to evaluate a boolean value and decide what value should be there based on the boolean.
    For example:
    Code (Java):
    private String getName() {
         return this.name == null ? "default value" : this.name;
    }
    instanceof Operator: The instanceof operator is used to check if an object is of a particular type.
    Code (Java):
    private boolean isString(Object object) {
         return object instanceof String;
    }
    isString("potato") would return true. isString(new Integer(5)) would return false.

    Continue the guide in Part 3!
  • Loading...
  • Loading...