Java Programming/Operators

From Wikibooks, open books for an open world
Jump to: navigation, search

Contents

[edit] Basic Operands

The basic operands are +,-,*,/, and %.
The only one that might need explaining is the '%' or modulus. Modular division divides the variable by a value and returns the remainder, discarding how many times it actually went into the original variable.

    Exp:
         5%2=1
         Because 5-2 = 3 and 3 - 2 = 1 but 1 - 2 < 0 therefore it would return a 1
         10%2=0
         12%7=5
         etc.

Also, with an integer when you divide ('/') it does not return with a decimal.

    Exp:
         int x = 5;
         
         x/2 != 2.5 it instead ='s 2, discarding the remainder.
         Although, you can cast it into a floating point variable if needed

Casting is an effective way to change comparable objects or primitive data types to other data types or objects.

[edit] Basic Comparisons

< is the Less than sign, used for comparisons if(x < y){...}
> is the Greater than sign, used for comparisons if(x < y){...}
<= less than or equal too
>= greater than or equal too

[edit] Adding to a simple if

xor is the exclusive or if(x == y xor y== z) only one of those can be true, not both.
|| is the or, if(x == y || y == z) only one has to be true, but both can be.
&& is the and operand, if(x ==y && y == z) both have to be true for the if expression to evaluate to true.
! or != means not or does not if(!(x==y)) if y and x are not the same it returns a true, else false. if(1 != 2) would return true, because 1 does not equal 2.

[edit] Comparing Strings

string.equals(string2) evaluates if one string is equal to another, case sensitive.
string.equalsIgnoreCase(string2) evaluates two string if they are equal, ignores case.

[edit] Increments and other operands

++    Adds one to the variable (x++; and x = x+1; are the same)
-- Subtracts one from the variable (x--; and x = x-1; are the same)
+= Adds value to the variable (x += 5 is the same as x = x+5)
-= Subtracts value from the variable (x -= 5 is the same as x = x-5)
*= Multiplies variable by value (x *= 5 is the same as x = x*5)
/= Divides variable by value (x /= 5 is the same as x = x/5)
<<< To be added
Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export