Java Bootcamp – Workbooks and Challenges
)}

Cheat Sheet

This cheat sheet contains the most important takeaways that lead up to section six.

1. Getting Started


  • class: contains all of your code.

  • main(): entry point of your app.

  • javac <file-name>.java: compiles your code.

  • java <file-name>: runs the compiled code.

2. Variables


Data types

There are 6 main data types.

long can store very big numbers. But, int is faster and takes less memory.

Math Operators

Put math operations in brackets if you wish to embed them inside a String.

  • Example: "5 + 2 is " + (5 + 2);

3. Booleans and Conditionals


Comparison Operators

Put comparison operations in brackets if you wish to embed them inside a String.

  • Example: "Five is not smaller than one. I'm certain this is " + (5 < 1);

Rules of thumb

When controlling how your code runs:

  • use switch to compare one value against a list of values.

  • in any other scenario, use if-else.

Do not confuse = with ==

  • use = to set a value equal to another.

  • use == to compare two values and return a boolean.

4. Functions


Function Parts

  1. Level of access: private, public.

  2. Return type: double, int, boolean, char, String, long.

  3. Function name: lowerCamelCase (singChorus, kickBall).

  4. Parameter: value received by the function.

  5. Argument: value passed into the function.

  6. Code: performs your task.

  • return breaks the entire function. Nothing after it can run.

Rule of thumb: if a function calculates a value, return it.

5. Loops


Rule of thumb:

  • use for loops when you know in advance how many times your code should run.

  • use while loops to keep running code while a condition is true.

break: breaks a loop and stops it from running.

continue: skips the current run, and continues to the next one.

6. Arrays


6.1 Defining an array

You can define an array and its values in one line.

Type[] array = { element1, element2, element3 };

You can also define the length of the array and populate it later.

Type[] array = new Type[3]; array[0] = element1; array[1] = element2; array[2] = element3;

In both cases:

  • The variable does not store the array directly.

  • It stores a reference that points to it.

6.2 Accessing values from an array

You can access values from an array by referring to its index, such that:

  • The first index is 0.

  • The last index is one less the length of the array.

6.3 Looping an array

for (int i = 0; i < array.length; i++) { }

6.4 Reference Trap

6.5 Defining a 2D array

Method 1:

Type[][] array = { {element1, element2}, {element3, element4}, };

Method 2:

Type[][] array = new Type[2][2]; array[0][0] = element1; array[0][1] = element2; array[1][0] = element3; array[1][1] = element4;

6.6 Accessing values from a 2D array

  • The first bracket indexes the row.

  • The second bracket indexes the element in that row.

6.7 Looping a 2D array

for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { } }

Scanner


The default delimiter is white space.

Scanner Pitfall


Good coding habits


Conventions

  • class: CamelCase.

  • variable: lowerCamelCase.

  • function: lowerCamelcase.

Tips and tricks


Terminal

  • Use the up key to run previous terminal commands.

  • Write clear to clear the terminal output.

  • Press the tab key for auto-complete.

Escape characters

  • \n adds a new line of space.

  • \t adds a new tab of space.

Shortcut keys

  • Use CMD/Ctrl+/ to comment a highlighted piece of code.

  • In Visual Studio Code, use sysout as a shortcut to System.out.println()

  • Use Ctrl/control + C to interrupt the terminal output.

  • Highlight and press tab for right indentation.

  • Highlight and press shift + tab for left indentation.

Arrays

  • return an array on the fly using: return new Type[] { element1, element2 };

Feedback Summary
4.8
38 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.