You are on page 1of 2

Java Fundamentals Cheat Sheet

by sschaub via cheatography.com/1000/cs/464/

Java Data Types Java Statements (cont) Java String Methods

byte / short / int / long -123, 10 switch ( expre​ssion ) { s.le​ngth() length of s

float / double 235.13 ​ case value: s.ch​arA​t(i) extract ith character


​ ​ ​ ​st​ate​ments
char 'U' s.su​bst​rin​g( ​start, substring from start to
​ ​ ​ ​break;
end) end-1
boolean true, false ​ case value2:
​ ​ ​ ​st​ate​ments s.to​Upp​erC​ase() returns copy of s in ALL
String "​Gre​etings from earth"
​ ​ ​ ​break; CAPS
​ ​def​ault: s.to​Low​erC​ase() returns copy of s in
Java Statements
​ ​ ​ ​st​ate​ments lowercase
If Statem​ent }
s.in​dex​Of(x) index of first occurence of
if ( expre​ssion ) { Exce​ption Handling
x
​ s​ t​ate​ments try {
} else if ( expre​ssion ) { ​ ​sta​tem​ents; s.re​pla​ce(​old, search and replace
​ s​ t​ate​ments } catch (Exce​pti​onType e1) { new)
} else { ​ ​sta​tem​ents; s.sp​lit​( r​egex) splits string into tokens
​ s​ ta
​ te​ments } catch (Exception e2) {
s.trim() trims surrou​nding
} ​ ​cat​ch-all statem​ents;
whitespace
While Loop } finally {
s.eq​ual​s(s2) true if s equals s2
while ( expre​ssion ) { ​ ​sta​tem​ents;
​ s​ t​ate​ments } s.co​mpa​reT​o(s2) 0 if equal/+ if s > s2/- if s
} < s2
Do-While Loop Java Data Conver​sions
See
do {
String to Number http:/​/do​cs.o​ra​cle.co​m/j​ava​se/​6/d​ocs​/ap​i/j​ava​/la​ng/​
​ s​ t​ate​ments
Str​ing.html for more.
} while ( expre​ssion ); int i = Intege​r.p​ars​eIn​t(​str);
double d = Double.pa​rse​Dou​ble​(s​tr);
For Loop
Any Type to String java.u​til.Ar​rayList Methods
for ( int i = 0; i < max; ++i) {
String s = String.va​lue​Of(​va​lue);
​ s​ t​ate​ments l.ad​d(​itm) Add itm to list
} Numeric Conver​sions
l.ge​t(i) Return ith item
For Each Loop int i = (int) numeric expres​sion;
l.size() Return number of items
for ( var : colle​ction ) {
​ s​ t​ate​ments l.re​mov​e(i) Remove ith item
} l.se​t(i, val) Put val at position i
Switch Statem​ent
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.

By sschaub Published 17th July, 2012. Sponsored by Readability-Score.com


cheatography.com/sschaub/ Last updated 2nd June, 2014. Measure your website readability!
Page 1 of 2. https://readability-score.com
Java Fundamentals Cheat Sheet
by sschaub via cheatography.com/1000/cs/464/

java.u​til.Ha​shMap Methods Java Boolean Operators

m.pu​t(​key,​ ​value) Inserts value with key ! x (not) x && y (and) x || y (or)

m.ge​t(​key) Retrieves value with key


Java Text Formatting
m.co​nta​ins​Key​( key true if contains key
) printf style format​ting
System.ou​t.p​rin​tf(​"​Count is %d\n", count);
HashMa​p<S​tÂ​​rin​Â​g​,St​rin​g> names =
s = String.fo​rma​t("Count is %d", count);
​ new HashMa​p<S​tÂ​​rinÂ​g, String​>();
Mess​age​Format style format​ting

See s = Messag​eFo​rma​t.f​ormat(

http:/​/do​cs.o​ra​cle.co​m/j​ava​se/​6/d​ocs​/ap​i/j​ava​/ut​il/​ ​ ​"At {1,time}, {0} eggs hatche​d.",

Has​hMa​p.html for more. ​ 25, new Date());


Indi​vidual Numbers and Dates
s = Number​For​mat.ge​tCu​rre​ncy​Ins​tance()
Java Hello World
​ .fo​rma​t(x);
import java.u​til.Date; s = new Simple​Dat​eFo​rma​t(""h:mm a"")
public class Hello { ​ .fo​rma​t(new Date());
​ ​public static void main(S​tring[] args) { s = new Decima​lFo​rma​t("#​,##​0.0​0")
​ ​ ​ ​Sys​tem.ou​t.p​rin​tln​("Hello, world!​"); ​ .fo​rma​t(1​25.32);
​ ​ ​ Date now = new Date();
See
​ ​ ​ ​Sys​tem.ou​t.p​rin​tln​("Time: " + now);
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
* 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

By sschaub Published 17th July, 2012. Sponsored by Readability-Score.com


cheatography.com/sschaub/ Last updated 2nd June, 2014. Measure your website readability!
Page 2 of 2. https://readability-score.com

You might also like