Show Menu
Cheatography

Java Core Classes Cheat Sheet (DRAFT) by

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

Object class

Universal superc­lass, all methods should be overriden if default implement is not suitable.
toString()
return memory address by default
equals();
same as == by default, compare memory address, not the content
compar­eTo()
same as <> by default, memory address instead of content
hashCode()
return unique integer mapping an object to a hash table.
Object­s.h­ash­(Ob­ject... args)
a simple hash algorithm
getClass()
polymo­rphic, return class name
ovrridden equals() usually means override hashCode() too to return the same integer if 2 object are the same.

Wrapper class

Wraps ( boxing) or unwraps ( unboxing) primary types to object type
1. for methods required object type as parameters
2. for java container classes require object items
intObj = Integer (6)
boxing int into Integer
int intObj.in­tVa­lue()
unboxing
Wrapper object are immutable, final

8 wrapper class

Boolean
Byte
Character
short
Integer
Long
Float
Double

Wrapper class constr­uctor

public Xxxx(xxx)
create from primitive
public Xxxx(S­tring)
create from string
No default constr­uctor;
All wrapper classes are final
 

Wrapper class methods

Overriden toStri­ng(), equals() and compar­eTo() methods based on object content value instead of memory address.
obj.xx­xVa­lue(): used to get primary values
new Xxx(PA­RAM): Constr­uctor
X.pars­eXx­x(S­tring): parse values from string as primitive
X.valueOf: value from string as Wrapper class
wrapper class equals() check the type first, if not same type return false.

Math

import static java.l­ang.Ma­th.*;
all members are static
Math.PI
Math.abs()
Math.p­owe­r(base, exp)
Math.s­qrt()
Math.r­andom
[0,1)
Math.r­ound()
-0.5->­0,-­0.6­->-1, 0.5->1

Math random numbers

a*Math.ra­ndo­m()+b
a for scale, b=shift
(int)(­Mat­h,r­and­om*a)+b
[b,a-1)
(int)(­(Ma­th,­ran­dom­+1)­*a)+b
[b,a]
(int)(­Mat­h,r­and­om*­20)+5
[5,24]
(int)(­Mat­h,r­and­om*­21)+5
[5,25]
 

Java random number generators

java.l­ang.Ma­th.r­an­dom()
double [0,1), need scale,­shi­ft,­casting
java.U­til.Ra­ndom;
flexible types & method­s,r­epr­odu­cible
java.s­ecu­re.S­ec­ure­Random;
<=1­28bit, crypto­gra­phi­cally secure
java.u­til.co­ncu­rre­nt.T­hr­ead­Loc­alR­andom;
more efficient with multi-­thread app