Show Menu
Cheatography

Java Cheat Sheet (DRAFT) by

Terms and syntax used by Java developers

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Primitives - raw data types

int (32-bits)
short (16)
float (32)
double (64)
char (16)
long (64)
byte (8)
boolean (1)
Wrapper Class - object version of primitive, can convert by un/aut­oboxing
Literal - represents a primitive value (d, f, ' ', " ", L)

Strings - immutable, final sequence of chars

String Pool
area in Heap that stores String literals to save memory
String s1 = "hi";
String s2 = "hi";
String s3 = new String ("hi­");
draws from same memory address

stored in heap not pool
String­Builder
mutable
String­Buffer
+threa­dsafe
Integer Pool - range -128 to 127

Array - object holding elements of one type

int[] numbers = {1, 2, 3};
int[] numbers = new int[size];
int[][] 2dArray = new int[ro­ws]­[co­lumns]

Object Class - everything inherits from

.toStr­ing();
prints memory address
.equals();
checks for same memory address
.hashC­ode();
id not guaranteed to be unique
Object - an instance of a class
Class - blueprint for object

Except­ion­s/E­rrors

Error
cannot recover from
stackoverflow, syntax...
Checke­d/C­ompile
cannot compile, compiler sees
FiloIO, SQLExc­ept­ion...
Unchec­ked­/Ru­ntime
not required to be handled
NullPointer, Arithm­etic...
throws
throws e to method caller
throw
commands e be thrown
useful for testing
throw new Except­ionType();
Create Exception
by extending
Exception
class
public Money Exception{
super("Error Messag­e")};
try{must have try & catch/­finally not just try}

catch(Except­ionType e){catches+ but only 1 runs}

finally{always executes};

Catches go from specific exception to general
 

Advanced Operators

+ Concat­enation
only operator overlo­ading in Java
% Modulus
returns reminder (7 % 5 = 2)
! Not
returns opposite boolean
++/-- Increment
pre (
printl­n(++5)
=6)
post (
printl­n(5++)
=5)
&&/|| And/Or
short-­circuit evaluates one side
& / | And/Or
evaluates both sides
^ Exclusive Or
both cannot be true
? Ternary
(if condit­ion)? return­:else return

Access Modifiers

Public
all
Private
only class
Protected
also children
(Default)
only package

Scopes - where does reference extend

Class
static
Object
instance
Method
local
Block
local
Static {block runs at beginning when class first accessed, initia­liz­ation};

File I/O - data access outside JRE

FileIn­put­/Ou­tpu­tSteam
read/write bytes
FileRe­ade­r/W­riter
by characters
Buffer­edR­ead­er/­Writer
line by line
Buffer­edW­riter bw = new Buffer­edW­riter (new FileWr­ite­r(".../­jav­a/s­rc/­mai­n/r­eso­urc­es.i­o.t­ex­t"));

final Keyword

Class
cannot be extended
Method
cannot be overridden
Variable
cannot change value
 

Terms

Java
classe­d-based object­-or­iented progra­mming (OOP), write once run anywhere (WORA), automatic memory manage­ment, pass by value or copy reference, strong types
IDE
Integrated Develo­pment Enviro­nment
Linter
auto compiles and catches errors in IDE
Reflection API
allows java to write code at runtime where classes, fields, methods are treated as objects
Recursion
calling a method within itself
Code Coverage
how much reachable tested code

Memory

MetaSpace
unchanging info
HeapSpace
variable content
StackSpace
method calls/­var­iable references