Java Fundamentals Cheat Sheet

A Programming › Java cheat sheet in English by sschaub

Java Fundamentals

197,967 views 56,571 downloads 17 Jul 2012 History
4.8 avg · 34 ratings

Java Data Types

byte / short / int / long
-123, 10
float / double
235.13
char
'­U'
boolean
true, false
String
"­Gre­etings from earth"

Java Statements

If Statement
if ( expression ) {
 ­ statements
} else if ( expression ) {
 ­ statements
} else {
 ­ statements
}

While Loop
while ( expression ) {
 ­ statements
}

Do-While Loop
do {
 ­ statements
} while ( expression );

For Loop
for ( int i = 0; i < max; ++i) {
 ­ statements
}

For Each Loop
for ( var : collection ) {
 ­ statements
}

Switch Statement
switch ( expression ) {
 ­ case value:
 ­ ­ ­ statements
 ­ ­ ­ ­break;
 ­ case value2:
 ­ ­ ­ statements
 ­ ­ ­ ­break;
 ­ ­def­ault:
 ­ ­ ­ statements
}

Exception Handling
try {
 ­ ­sta­tem­ents;
} catch (Except­ionType e1) {
 ­ ­sta­tem­ents;
} catch (Exception e2) {
 ­ ­cat­ch-all statem­ents;
} finally {
 ­ ­sta­tem­ents;
}

Java Data Conver­sions

String to Number
int i = Intege­r.p­ars­eInt(str);
double d = Double.pa­rse­Double(str);

Any Type to String
String s = String.va­lueOf(value);

Numeric Conver­sions
int i = (int) numeric expression;

Java String Methods

s.length()
length of s
s.charAt(i)
extract ith character
s.subst­ring(start, end)
substring from start to end-1
s.toUpp­erC­ase()
returns copy of s in ALL CAPS
s.toLow­erC­ase()
returns copy of s in lowercase
s.indexOf(x)
index of first occurence of x
s.replace(old, new)
search and replace
s.split(regex)
splits string into tokens
s.trim()
trims surrou­nding whitespace
s.equals(s2)
true if s equals s2
s.compa­reTo(s2)
0 if equal/+ if s > s2/- if s < s2

java.u­til.Ar­rayList Methods

l.add(itm)
Add itm to list
l.get(i)
Return ith item
l.size()
Return number of items
l.remove(i)
Remove ith item
l.set(i, val)
Put val at position i
ArrayL­ist­<St­rin­g> names =
 ­ new ArrayL­ist­<St­rin­g>();

See http:/­/do­cs.o­ra­cle.co­m/j­ava­se/­6/d­ocs­/ap­i/j­ava­/ut­il/­Arr­ayL­ist.html for more.

java.u­til.Ha­shMap Methods

m.put(key,value)
Inserts value with key
m.get(key)
Retrieves value with key
m.conta­insKey(key)
true if contains key
HashMa­p<S­t­­rin­Â­g­,St­rin­g> names =
 ­ new HashMa­p<S­t­­rin­g, String­>();

See http:/­/do­cs.o­ra­cle.co­m/j­ava­se/­6/d­ocs­/ap­i/j­ava­/ut­il/­Has­hMa­p.html for more.

Java Hello World

import java.u­til.Date;

public class Hello {
 ­ ­public static void main(S­tring[] args) {
 ­ ­ ­ ­Sys­tem.ou­t.p­rin­tln­("Hello, world!­");
 ­ ­ ­ Date now = new Date();
 ­ ­ ­ ­Sys­tem.ou­t.p­rin­tln­("Time: " + now);
 ­ }
}
Save in Hello.java
Compile: javac Hello.java
* Run: java Hello

Java Arithmetic Operators

x + y
add
x - y
subtract
x * y
multiply
x / y
divide
x % y
modulus
++x / x++
increment
   
--x / x--
decrement
Assignment shortcuts: x op= y
Example: x += 1 increments x

Java Comparison Operators

x < y
Less
x <= y
Less or eq
x > y
Greater
x >= y
Greater or eq
x == y
Equal
x != y
Not equal

Java Boolean Operators

! x (not)
x && y (and)
x || y (or)

Java Text Formatting

printf style formatting
System.ou­t.p­rin­tf(­"­Count is %d\n", count);
s = String.fo­rma­t("Count is %d", count);

Messag­eFormat style formatting
s = Messag­eFo­rma­t.f­ormat(
 ­ ­"At {1,time}, {0} eggs hatche­d.",
 ­ 25, new Date());

Individual Numbers and Dates
s = Number­For­mat.ge­tCu­rre­ncy­Ins­tance()
 ­ .fo­rma­t(x);
s = new Simple­Dat­eFo­rma­t(""h:mm a"")
 ­ .fo­rma­t(new Date());
s = new Decima­lFo­rma­t("#­,##­0.0­0")
 ­ .fo­rma­t(1­25.32);

Badges

Great Cheat Sheet Useful Cheat Sheet Good Cheat Sheet Nice Cheat Sheet Appreciated Cheat Sheet Downloaded from Unusual Locations × 17 🇻🇪 Big in Venezuela

Comments (5)

[deleted] 12:43 19 Sep 2013

try { sta­tem­ents; } catch (Exce­pti­onType e1) { sta­tem­ents; } catch (Exception e2) { cat­ch-all statem­ents; } finally { sta­tem­ents; } We can write the above code like this also try { sta­tem­ents; } catch (Exce­pti­onType | Exception e1) { sta­tem­ents; } finally { sta­tem­ents; } But this will work only in JavaSE7.0 and above.. sankar <a href="http://wisentechnologies.com/it-courses/java-training.aspx">java training in chennai</a>

Reply
[deleted] 12:41 19 Sep 2013

try { ­ ­sta­tem­ents; } catch (Exce­pti­onType e1) { ­ ­sta­tem­ents; } catch (Exception e2) { ­ ­cat­ch-all statem­ents; } finally { ­ ­sta­tem­ents; } We can write the above code like this also try { ­ ­sta­tem­ents; } catch (Exce­pti­onType | Exception e1) { ­ ­sta­tem­ents; } finally { ­ ­sta­tem­ents; }

Reply
starius 05:37 30 Oct 2012

HOLY COW YOU UPLOADED TONS OF JAVA! I <3 u

Reply
starius 05:36 30 Oct 2012

Love it <3

Reply
Matt 15:21 6 Aug 2012

This is exactly what I need, thank you

Reply

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.