or   Register or Register today to make and share your own cheat sheets! (Why Join?)

Cheatography Login

Join Us!

Not a Cheatographer? Register here!

Social Media

You can login to or register with Cheatography using your Facebook or Twitter account!

Why Join Cheatography?

Make and share cheat sheets!
Join a great community of Cheatographers and add your very own contributions.

Save your favourites!
Quick access to your most loved cheat sheets.

Fewer ads!
Members see no ads on the site.

Coming soon ...
Requests, ratings and more!

Why Join Cheatography?

Make and share cheat sheets!
Join a great community of Cheatographers and add your very own contributions.

Save your favourites!
Quick access to your most loved cheat sheets.

Fewer ads!
Members see no ads on the site.

Coming soon ...
Requests, ratings and more!

Java Fundamentals Cheat Sheet by sschaub

3 Comments   |   Add a Comment   |   PDF Download   |   Find:

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 Statem­ent
if ( expre­ssion ) {
 ­ ­st­ate­ments
} else if ( expre­ssion ) {
 ­ ­st­ate­ments
} else {
 ­ ­st­ate­ments
}

While Loop
while ( expre­ssion ) {
 ­ ­st­ate­ments
}

Do-While Loop
do {
 ­ ­st­ate­ments
} while ( expre­ssion );

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

For Each Loop
for ( var : colle­ction ) {
 ­ ­st­ate­ments
}

Switch Statem­ent
switch ( expre­ssion ) {
 ­ case value:
 ­ ­ ­ ­st­ate­ments
 ­ ­ ­ ­break;
 ­ case value2:
 ­ ­ ­ ­st­ate­ments
 ­ ­ ­ ­break;
 ­ ­def­ault:
 ­ ­ ­ ­st­ate­ments
}

Exce­ption Handling
try {
 ­ ­sta­tem­ents;
} catch (Exce­pti­onType e1) {
 ­ ­sta­tem­ents;
} catch (Exception e2) {
 ­ ­cat­ch-all statem­ents;
} finally {
 ­ ­sta­tem­ents;
}
 

Java Data Conversions

String to Number
int i = Intege­r.p­ars­eIn­t(­str);
double d = Double.pa­rse­Dou­ble­(s­tr);

Any Type to String
String s = String.va­lue­Of(­va­lue);

Numeric Conver­sions
int i = (int) numeric expres­sion;

Java String Methods

s.le­ngth() length of s
s.ch­arA­t(i) extract ith character
s.su­bst­rin­g(­start, end) substring from start to end-1
s.to­Upp­erC­ase() returns copy of s in ALL CAPS
s.to­Low­erC­ase() returns copy of s in lowercase
s.in­dex­Of(x) index of first occurence of x
s.re­pla­ce(­old, new) search and replace
s.sp­lit­(r­egex) splits string into tokens
s.trim() trims surrou­nding whitespace
s.eq­ual­s(s2) true if s equals s2
s.co­mpa­reT­o(s2) 0 if equal/+ if s > s2/- if s < s2

See http:/­/do­cs.o­ra­cle.co­m/j­ava­se/­6/d­ocs­/ap­i/j­ava­/la­ng/­Str­ing.html for more.

java.util.ArrayList Methods

l.ad­d(­itm) Add itm to list
l.ge­t(i) Return ith item
l.size() Return number of items
l.re­mov­e(i) Remove ith item
l.se­t(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.util.HashMap Methods

m.pu­t(­key­,­value) Inserts value with key
m.ge­t(­key) Retrieves value with key
m.co­nta­ins­Key­(key) true if contains key

HashMa­p<S­t­r­in­­g,S­tri­ng> names =
 ­ new HashMa­p<S­t­r­in­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.j­ava
* 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 format­ting
System.ou­t.p­rin­tf(­"­Count is %d\n", count);
s = String.fo­rma­t("Count is %d", count);

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

Indi­vidual 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);

See http:/­/do­cs.o­ra­cle.co­m/j­ava­se/­6/d­ocs­/ap­i/j­ava­/te­xt/­pac­kag­e-f­ram­e.html for Messag­eFormat and related classes

Favourited by 5 Members:

Vaibhav zenweasel starius dttk akipta

Comments

Matt Matt, 15:21 6 Aug 12

This is exactly what I need, thank you

starius starius, 05:36 30 Oct 12

Love it <3

starius starius, 05:37 30 Oct 12

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

Add a Comment

Comment:

Contents

Column Content Comments Author Updated
- Java Fundamentals Cheat Sheet sschaub 17 Jul 12
1 Java Data Types 0 sschaub 17 Jul 12
Java Statements 0 sschaub 17 Jul 12
2 Java Data Conversions 0 sschaub 17 Jul 12
Java String Methods 0 sschaub 17 Jul 12
java.util.ArrayList Methods 0 sschaub 17 Jul 12
java.util.HashMap Methods 0 sschaub 17 Jul 12
3 Java Hello World 0 sschaub 17 Jul 12
Java Arithmetic Operators 0 sschaub 17 Jul 12
Java Comparison Operators 0 sschaub 17 Jul 12
Java Boolean Operators 0 sschaub 17 Jul 12
Java Text Formatting 0 sschaub 17 Jul 12