java-operators-cheatsheet

Website containing basic Java Operators and their usages


Project maintained by Andre601 Hosted on GitHub Pages — Theme by mattgraham

Site containing most, if not all known Java operators.
If you find invalid information, or want to add missing one, feel free to open an issue or open a PR on the GitHub Repo (and source) of this site.

Credits

This site is based on the one of Cafeaulait.org with some minor changes being made. All original credit goes to the people of that site.

Note

The tables may look weird (missing or misplaced text) due to the usage of | within the tables themself.
This is a visual bug and only affects the GitHub repository. The main website itself is unaffected by this.

Operators

Number Operators

Can be used to manipulate numbers.

Operator: Function: Example:
+ Addition. 1.1
+= Add and assign numbers. 1.2
- Substraction. 1.3
-= Substract and assign number. 1.4
* Multiplication. 1.5
*= Multiply and assign number. 1.6
/ Division. 1.7
/= Divide and assign number. 1.8
% Take remainder 1.9
%= Take and assign remainder. 1.10
++ Increase by 1. 1.11
-- Decrease by 1. 1.12

Boolean Operators

May be used in if statements.

Operator: Fuction: Example:
> Greater than. 2.1
>= Greater than or equal to. 2.2
< Less than. 2.3
<= Less than or equal to. 2.4
== Is equal to. 2.5
!= Is NOT equal to. 2.6
     
&& Operator AND. 2.7
|| Operator OR 2.8
     
! Operator NOT. 2.9

String Operators

Can be used to manipulate Strings.

Operator: Function: Example:
+ Concatate two Strings. 3.1
+= Concatate and assign Strings. 3.2

Bit Operators

Operator: Function: Example:
~ Bitwise NOT. 4.1
| Bitwise OR. 4.2
|= Bitwise OR and assign. 4.3
^ Bitwise XOR. 4.4
^= Bitwise XOR and assign. 4.5
& Bitwise AND. 4.6
&= Bitwise AND and assign. 4.7
>> Signed bits shifting right. 4.8
>>= Signed bits shifting right and assigning. 4.9
<< Shift bits left. 4.10
<<= Shift bits left and assigning. 4.11
>>> Unsigned bit shifting right. 4.12
>>>= Unsigned bit shifting right and assign. 4.13

Examples

1.1

int i = 0;

// Add 1 to i.
i = i + 1;

// Will print 1.
System.out.prntln(i);

1.2

int i = 0;

// Add 1 to i.
i += 1;

// Will print 1.
System.out.prntln(i);

1.3

int i = 5;

// Remove 2 from i.
i = i - 2;

// Will print 3.
System.out.prntln(i);

1.4

int i = 5;

// Remove 2 from i.
i -= 2;

// Will print 3.
System.out.prntln(i);

1.5

int i = 1;

// Multiply i by 5.
i = i * 5;

// Will print 5.
System.out.prntln(i);

1.6

int i = 1;

// Multiply i by 5.
i *= 5;

// Will print 5.
System.out.prntln(i);

1.7

int i = 10;

// Divide i by 2.
i = i / 2;

// Will print 5.
System.out.prntln(i);

1.8

int i = 10;

// Divide i by 2.
i /= 2;

// Will print 5.
System.out.prntln(i);

1.9

int i = 21;

// Get remainder of 21 / 4.
i = i % 4;

// Will print 1.
System.out.prntln(i);

1.10

int i = 21;

// Get the remainder of 21 / 4.
i %= 4;

// Will print 1.
System.out.prntln(i);

1.11

int i = 0;

// Increase by 1.
i++;

// Will print 1.
System.out.println(i);

// ====================

// Usage in a for loop.
for(int i = 0; i < 10; i++){
    System.out.prntln(i);
}

1.12

int i = 10;

// Decrease by 1.
i--;

// Will print 9.
System.out.prntln(i);

// ====================

// Usage in a for loop.
for(int i = 10; i > 0; i--){
    System.out.prntln(i);
}

2.1

// Gets a random value between 0 and 10.
int a = new Random().nextInt(10);
int b = 9;

// Will be true when a is more than 9.
if(a > b)
    System.out.prntln("A is larger than B");

2.2

// Gets a random value between 0 and 10.
int a = new Random().nextInt(10);
int b = 9;

// Will be true when a is more than or exactly 9.
if(a >= b)
    System.out.prntln("A is greater or equal to B");

2.3

// Gets a random value between 0 and 10.
int a = new Random().nextInt(10);
int b = 1;

// Will be true when a is less than 1;
if(a < b)
    System.out.prntln("A is less than B");

2.4

// Gets a random value between 0 and 10.
int a = new Random().nextInt(10);
int b = 9;

// Will be true when a is less or equal to 1.
if(a <= b)
    System.out.prntln("A is less than or equal to B");

2.5

// Gets a random value between 0 and 10.
int a = new Random().nextInt(10);
int b = 10;

// Will be true when a is equal to 10.
if(a == b)
    System.out.prntln("A is equal to B");

2.6

// Gets a random value between 0 and 10.
int a = new Random().nextInt(10);
int b = 10;

// Will be true when a is between 0 and 9.
if(a != b)
    System.out.prntln("A is not equal to B");

2.7

// Gets a random value between 0 and 10.
int a = new Random().nextInt(10);
int b = 5;
int c = 10;

// Will be true when a is more than 5 AND less than 10.
if((a > b) && (a < c))
    System.out.prntln("A is greater than B and less than C.")

2.8

// Gets a random value between 0 and 10.
int a = new Random().nextInt(10);
int b = 0;
int c = 10;

// Will be true when a is equal to 10 OR equal to 0.
if((a == b) || (a == c))
    System.out.prntln("A is equal to B or C");

2.9

// Getting a basic true/false with the Random function
int i = new Random().nextInt(1);

String text = "FooBar";
if(i == 1)
    text = "Hello World!";

// Will be true if the String text is not equal to "Hello World!".
if(!text.equals("Hello World!"))
    System.out.prntln("Not Hello world!");

3.1

String text = "Foo";

// Concatate the extra text.
text = text + "Bar";

// Will print "FooBar".
System.out.prntln(text);

3.2

String text = "Foo";

// Concatate and directly assign the extra text.
text += "Bar";

// Will print "FooBar".
System.out.prntln(text);

4.1


4.2

/*
 * Bitwise OR is useful for try-catch where multiple
 * Exceptions can be thrown by one or multiple methods.
 *
 * Reduces the amount of catch-blocks significantly.
 */
public void performStuff(){
    try{
        MyClass.doStuff();
    }catch(IOException | NullPointerException ex){
        ex.printStackTrace();
    }
}

4.3


4.4


4.5


4.6


4.7


4.8

// We first get the currently used RAM and max allocated RAM.
long used = ManagementFactory.getMemoryMXBeam().getHeapMemoryUsage().getUsed();
long max  = ManagementFactory.getMemoryMXBeam().getHeapMemoryUsage().getMax();

// We shift both values by 20, turning them info MB value.
used = used >> 20;
max  = max >> 20;

System.out.prntln("RAM usage: " + used + "/" + max + "MB");

4.9

// We first get the currently used RAM and max allocated RAM.
long used = ManagementFactory.getMemoryMXBeam().getHeapMemoryUsage().getUsed();
long max  = ManagementFactory.getMemoryMXBeam().getHeapMemoryUsage().getMax();

// We shift both values by 20, turning them info MB value.
used >>= 20;
max  >>= 20;

System.out.prntln("RAM usage: " + used + "/" + max + "MB");

4.10


4.11


4.12


4.13