Java Fundamentals Cheat Sheet
by sschaub via cheatography.com/1000/cs/464/
Java Data Types Java Statements (cont) Java String Methods
byte / short / int / -123, 10 switch ( expression ) { s .length() length of s
long case value: s .charAt(i) extract ith character
float / double 235.13 statements
s .substring(start, substring from start to
break;
char 'U' end) end-1
case value2:
boolean true, false s .toUpperCase() returns copy of s in ALL
statements
String "Greetings from CAPS
break;
earth" default: s .toLowerCase() returns copy of s in
statements lowercase
Java Statements } s .indexOf( x ) index of first occurence
If Statement Exception Handling of x
if ( expression ) { try {
s .replace(old , search and replace
statements statements;
new)
} else if ( expression ) { } catch ( ExceptionType e1) {
s .split(regex) splits string into tokens
statements statements;
} catch (Exception e2) { s .trim() trims surrounding
} else {
catch-all statements; whitespace
statements
} } finally { s .equals(s2 ) true if s equals s2
While Loop statements; s .compa‐ 0 if equal/+ if s > s2/- if
while ( expression ) { } reTo(s2 ) s < s2
statements
See http://docs.oracle.com/javase/6/d‐
} Java Data Conversions
ocs/api/java/lang/String.html for more.
Do-While Loop String to Number
do { int i = Integer.parseInt(str); java.util.ArrayList Methods
statements double d = Double.parseDouble(str);
l.add(itm ) Add itm to list
} while ( expression ); Any Type to String
For Loop String s = String.valueOf(value); l.get(i) Return ith item
for ( int i = 0; i < max; ++i) { Numeric Conversions l.size() Return number of items
statements int i = (int) numeric expression; l.remove(i) Remove ith item
}
l.set(i, val) Put val at position i
For Each Loop
for ( var : collection ) { ArrayList<String> names =
statements new ArrayList<String>();
}
Switch Statement See http://docs.oracle.com/javase/6/d‐
ocs/api/java/util/ArrayList.html for more.
By sschaub Published 17th July, 2012. Sponsored by Readable.com
cheatography.com/sschaub/ Last updated 12th May, 2016. Measure your website readability!
Page 1 of 2. https://readable.com
Java Fundamentals Cheat Sheet
by sschaub via cheatography.com/1000/cs/464/
java.util.HashMap Methods Java Boolean Operators
m.put(key,value) Inserts value with key ! x (not) x && y (and) x || y (or)
m.get(key) Retrieves value with
key Java Text Formatting
m.conta‐ true if contains key printf style formatting
insKey( key) System.out.printf("Count is %d\n", count);
s = String.format("Count is %d", count);
HashMap<StÂrinÂg,String> names =
MessageFormat style formatting
new HashMap<StÂrinÂg, String>();
s = MessageFormat.format(
"At {1,time}, {0} eggs hatched.",
See http://docs.oracle.com/javase/6/d‐
25, new Date());
ocs/api/java/util/HashMap.html for more.
Individual Numbers and Dates
s = NumberFormat.getCurrencyInstance()
Java Hello World
.format(x);
import java.util.Date; s = new SimpleDateFormat(""h:mm a"")
public class Hello { .format(new Date());
public static void main(String[] args) { s = new DecimalFormat("#,##0.00")
System.out.println("Hello, world!"); .format(125.32);
Date now = new Date();
See http://docs.oracle.com/javase/6/d‐
System.out.println("Time: " + now);
ocs/api/java/text/package-frame.html for
}
MessageFormat and related classes
}
* 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
By sschaub Published 17th July, 2012. Sponsored by Readable.com
cheatography.com/sschaub/ Last updated 12th May, 2016. Measure your website readability!
Page 2 of 2. https://readable.com