Post

Java Cheat Sheet

Simple Java cheat sheet.

Java Cheat Sheet

This is a simple cheat sheet for Java.

Startup

1
2
3
4
5
public class Main {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

Variables Types

TypeDescription
StringStores text, such as “Hello”. String values are surrounded by double quotes
intstores integers (whole numbers), without decimals, such as 123 or -123
floatstores floating point numbers, with decimals, such as 19.99 or -19.99
charStores single characters, such as ‘a’ or ‘B’. Char values are surrounded by single quotes
booleanstores values with two states: true or false

Examples

1
2
3
4
5
    int myNum = 5;               // Integer (whole number)
    float myFloatNum = 5.99f;    // Floating point number
    char myLetter = 'D';         // Character
    boolean myBool = true;       // Boolean
    String myText = "Hello";     // String

Arithmetic Operators

OperatorNameDescriptionExample
+AdditionAdds together two valuesx + y
-SubtractionSubtracts one value from anotherx - y
*MultiplicationMultiplies two valuesx * y
/DivisionDivides one value by anotherx / y
%ModulusReturns the division remainderx % y
++IncrementIncreases the value of a variable by 1++x
--DecrementDecreases the value of a variable by 1--x

Assignment Operators

OperatorExampleSame AsDescription
=x = 5x = 5 
+=x += 3x = x + 3Adds and assign
-=x -= 3x = x - 3Subtract and assign
*=x *= 3x = x * 3Multiply and assign
/=x /= 3x = x / 3Divide and assign
%=x %= 3x = x % 3Modulo and assign remainder
&=x &= 3x = x & 3Bitwise AND (0101(5) & 0011(3) = 0001(1))
|=x |= 3x = x | 3Bitwise OR (0101(5) | 0011(3) = 0111(7))
^=x ^= 3x = x ^ 3Bitwise XOR (0101(5) ^ 0011(3) = 0110(6))
>>=x >>= 3x = x >> 3Shift bits right (0100(4) >>= 2 = 0001(1))
<<=x <<= 3x = x << 3Shift bits left (0100(4) <<= 1 = 1000(8))

Comparison Operators

OperatorNameExampleDescription
==Equal tox == yReturns true if x equals y.
!=Not equalx != yReturns true if x is not equal to y.
>Greater thanx > yReturns true if x is greater than y.
<Less thanx < yReturns true if x is less than y.
>=Greater than or equal tox >= yReturns true if x is greater than or equal to y.
<=Less than or equal tox <= yReturns true if x is less than or equal to y.

Logical Operator

OperatorNameDescriptionExample
&&Logical ANDReturns true if both conditions are truex < 5 && x < 10
||Logical ORReturns true if at least one condition is truex < 5 || x < 4
!Logical NOTReverses the result: returns false if the expression is true!(x < 5 && x < 10)

If…Else

Else If

Syntax

1
2
3
4
5
6
7
if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}

Example

1
2
3
4
5
6
7
8
9
int time = 22;
if (time < 10) {
  System.out.println("Good morning.");
} else if (time < 18) {
  System.out.println("Good day.");
} else {
  System.out.println("Good evening.");
}
// Outputs "Good evening."

Short hand

Syntax

1
variable = (condition) ? expressionTrue :  expressionFalse;

Example

1
String result = (time < 18) ? "Good day." : "Good evening.";

Switch Case

Syntax

1
2
3
4
5
6
7
8
9
10
switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int day = 4;
switch (day) {
  case 1:
    System.out.println("Monday");
    break;
  case 2:
    System.out.println("Tuesday");
    break;
  case 3:
    System.out.println("Wednesday");
    break;
  case 4:
    System.out.println("Thursday");
    break;
  case 5:
    System.out.println("Friday");
    break;
  case 6:
    System.out.println("Saturday");
    break;
  case 7:
    System.out.println("Sunday");
    break;
}
// Outputs "Thursday" (day 4)

While Loop

While

Syntax

1
2
3
while (condition) {
  // code block to be executed
}

Example

1
2
3
4
5
6
7
8
9
int dice = 1;
while (dice <= 6) {
  if (dice < 6) {
    System.out.println("No Yatzy.");
  } else {
    System.out.println("Yatzy!");
  }
  dice = dice + 1;
}

Do/While

Syntax

1
2
3
4
do {
  // code block to be executed
}
while (condition);

Example

1
2
3
4
5
6
int i = 0;
do {
    System.out.println(i);
    i++;
}
while (i < 5);

For Loops

For Loop

Syntax

1
2
3
for (statement 1; statement 2; statement 3) {
  // code block to be executed
}

Example

1
2
3
for (int i = 0; i < 5; i++) {
  System.out.println(i);
}

Nested

1
2
3
4
5
6
7
8
9
// Outer loop
for (int i = 1; i <= 2; i++) {
    System.out.println("Outer: " + i); // Executes 2 times
  
    // Inner loop
    for (int j = 1; j <= 3; j++) {
        System.out.println(" Inner: " + j); // Executes 6 times (2 * 3)
    }
} 

For Each

Syntax

1
2
3
for (type variableName : arrayName) {
  // code block to be executed
}

Example

1
2
3
4
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (String i : cars) {
  System.out.println(i);
}
This post is licensed under CC BY 4.0 by the author.