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