Cheat Sheet
This cheat sheet contains the most important takeaways that lead up to section five.
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 aboolean
.
4. Functions
Function Parts
-
Level of access:
private
,public
. -
Return type:
double
,int
,boolean
,char
,String
,long
. -
Function name:
lowerCamelCase
(singChorus
,kickBall
). -
Parameter: value received by the function.
-
Argument: value passed into the function.
-
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 istrue
.
break
: breaks a loop and stops it from running.
continue
: skips the current run, and continues to the next one.
Scanner
The default delimiter is white space.
Coding Pitfalls
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 toSystem.out.println()
-
Use
Ctrl
/control
+C
to interrupt the terminal output.