Show Menu
Cheatography

Java Building Blocks (OCA) Cheat Sheet (DRAFT) by

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

Class structure

fields­/at­tri­but­es/­pro­perties
object Status
Method­s/B­eha­vio­rs/­Fun­ctions
object behavior
comments (3 types)
//single line
 
/* multiple*/
 
/**java doc */
classes vs files
one file have one public class
 
one file could have more classes

Main()

JVM hook point to prcess, program entry point
public static void main( String[] args){}
array input
public static void main( String... args){}
varargs list
args[0] is the first argument
separated by space
javac -cp "­,;..;.." MyClas­s.java
> MyClas­s.class
java -cp "..."­ MyClass "San Diego"
args[0­]="San
java -cp "..."­ MyClas­s.class
X MyClass
-cp ".; c:\tmp­\my­*"
sub directory not included by *
variable arguments: array arguments with variable size. int... num is an array with variable size. Only the last parameter could be varargs.

Creating Objects

Constr­uctor
initialize instance fields
name=Class name
public always
no return type
not void either
new Contru­ctor()
default constr­uctor
do nothing constr­uctor
getter
setter
code block
{ }
initia­lizer block
code block out of method block
initia­liz­ation order
field declar­ation line
 
static initia­lizer block
 
instance initia­lizer block
 
constr­uctor

Packages and import

packag­es=­org­anizer for easy to find/use types(­class, interf­ace­,enum);
avoid naming confli­ct;­control access to code(p­ackage private)
Syntax
import java.n­io.*;
X only subpackage here
import java.n­io.f­il­es.P­at­hs.*;
X can not import fields or methos
import java.n­io.f­il­es.P­aths;
import class
import java.n­io.f­il­es.*;
import classes
import java.n­io.f­iles..;
X
import java.l­ang.*;
OK, redundant
Naming conflicts
import java.s­ql.*; import java.u­til.*
Date is ambiguous
Class import precedent *
import java.s­ql.D­ate; import java.u­til.Date;
import collides
use full qualified class name to avoid naming conflicts
Create package
first line in file
javac pkg/Cl­ass­A.java pkg/Cl­ass­B.java
java pkg.Cl­ass­A.class
X usepkg.ClassA
Classpath
java -cp ".; c:/jav­a/l­ib/*; ..." pkg.ClassA
c:/jav­a/lib/*
jar or subdir by import/pkg statement
c:/jav­a/lib
all .class file in the dir
-cp will ignore enviro­nment class_path variable
import static
import a.b.X.*;
direct access all static member
if access static by ref, need to import the class, instead of static import
java import only make compiling a little longer, not make program larger.

JAR file

Java Archive
ole format zip file with optional .jar ext
Content
class files+­res­ource files
why jar?
Security _digital signning
compre­ssion
package versioning
portable
Create jar
jar cf jar-file imput-­fil­e(s­)/d­ire­ctories
 
_export
classp­ath­=c:­\li­b\C­ar.jar
classp­ath­=c:­\lib*
all jars
classp­ath­=c:\lib
all .class files
 

Primitive types

primitives
byte
literal
default
boolean
bit
true
false
byte
1
123
0
short
2
123
0
int
4
123
0
long
8
123L
0
float
4
123.5f
0.0f
double
8
123.5
0.0
char
2
'a'
''
Assignment of literal
Long max=31­234­56789
X
Long max=31­234­56789L
OK
byte b =123;
OK
byte b=256;
x
float f=123.5;
X
float f=123.5f;
OK
default 123 is int, 123.5 is double
default value is for instance variable, local variable has no default, need to be initia­lized explicitly before use it
char is treated as integer number in java, could use ++ -- and conversion between int, byte,s­hort.

Number format conversion

binary
ob11
1,2,4,8
3
octal
o17
1,8,64
15
decimal
56
1,10,100
56
hexade­cimal
ox1f
1,16,256
31
System.ou­t.p­rin­tln­(ox1f) will display in decimal 31.
_ used for all number formats
_123 is a valid identifier
underscore for easy read, _ could not anywhere but 4 locations: beginning, end,before or after decimal point. _123_._45_

Reference types

Reference type reference an object, hold the memory location of the object it reference to.
A reference also called a pointer
The default value for a instance reference type is null
local reference variable must be initia­lized to a null or object

Reference vs primitives

 
References
Primitives
data
obj memory addr
value
default value
null
0,fals­e,0.0f­,0.0, NUL
java strong typed, can not assign 1 to boolean;
null is an object, can print it ("nu­ll"), cannot call method from it - NullPo­int­Exc­eption.

Object vs Primitive

primitive has only one piece of state inform­ation, absolutely no behavior
Object bundled with one or multiple states and/or behavior
Object on heap, ref by ref type

Stack vs Heap

Stack
Heap
small
large
Ref variables
objects
live-span within scope
live or ready for gc
primitive value/­member address
object state
 

Declare and initia­lizing variables

variable name
Starts lower letters | $ | _
 
my contains numbers
package, keyword
lower letters
Class
Initcap
Constant
all caps
int a,string s;
X

Default initia­liz­ation of variables

local variable
must be explicitly initia­lized
instan­ce/­class variables
false, 0,0.0,­'\u­000­0'(­NUL­),null
boolean
false
byte,s­hor­t,i­nt,long
0
float duble
0.0
char
'\u000­0'(NUL)
reference type
null

Variable scope

block scope
multiple levels
methods scope
life time => from declar­ation to end of block
instance scope
life time =>from declar­ation to gc of obj
class static scope
lifeti­me=> from declar­ation to end of program
a variable declared in switch­,if,for loop can not used outside of them

Order of element in a class

package declar­ation
import statement
class declar­ation
field declar­ation
methods declar­ation

Garbage collection

automa­tically handled
remove the referenced obj from heap
could manual call System.gc() -->­fin­alize()
just a hit, may not get run, but if run, never run twice
Obj available for gc: unrefe­renced
set to null; change ref; all ref out of scope
Object
References
on heap
on stack
diff in size
same size (mem addr)
accessed by ref
by variable name
Memory Leak
lazy code:leave unused obj hanging
create variable never used

Benefit of Java

Object oriented
encaps­ulation
getter, setter + private
platform indepe­ndent
robust
auto mem mgn, gc
simple
no pointer, no operator overlo­ading
secure
run within JVM ( sandbox)