Basic Java Cheat Sheet
For your reference; this sheet will also be included in exams
CISC 124, fall 2004
Variable Names: Characters:
studentName // if ch is lower case, capitalize
middle_initial if ([Link](ch))
student5 ch = [Link](ch);
remember that upper & lower case matters! // returns true if ch is a decimal digit
// (i.e. '0','1',...,'9'
Primitive Types: if ([Link](ch))...
int // translates ch into the equivalent int
double // (0 to 9)
char int i = [Link](ch, 10);
boolean Arrays:
// create array of 10 doubles
Comparisons (primitive types only): double arr[] = new double[10];
< > <= >= == != number of elements in arr: [Link]
(Note comparison for equality is a double equal)
Increment/Decrement:
Strings: x++; // means x = x + 1;
String s = "hello, world" x--; // means x = x – 1;
String s2 = "abc" + "def" + 13;
// s2 gets "abcdef13" Extended Assignment:
int len = [Link](); // len gets 12 x += 3; // means x = x + 3;
char c = [Link](1); // c gets 'e' x -= 7; // means x = x – 7;
if ([Link](t))
// true if t exactly the same as s Declarations & Assignments:
if ([Link](t)) int x;
// same as above but ignores case x = 14;
int x = [Link](t); double d = 15.2;
// 0 if they're equal
// positive if s is greater Output:
// negative if t is greater [Link]("x = " + x
String s1 = "abcdefg"; + " and y = " + y);
String s2 = [Link](1,4); // x & y can be any type
// s2 gets "bcd"
String s3 = [Link](3); Input:
// s3 gets "defg" import hsa.*; // at start of class
int pos = [Link]('c'); String s = [Link]();
// pos gets 2 // s gets entire line
String s = [Link]();
// String <-> int conversions // s gets string up to white space
int x = [Link](s); int i = [Link]();
// if s = "123", x gets 123 double d = [Link]();
// reminder: parseInt can throw char c = [Link]();
// a NumberFormatException
// (subclass of RuntimeException) If Statements:
String s = [Link](x); if (a < b) {
// if x = 123, s gets "123" [Link]("b is bigger");
c = b;
}
else {
[Link]("a is bigger");
c = a;
} // end if
If – else if – else if ....: Comparator:
if (ch >= 'A' && ch <= 'Z') { public interface Comparator {
[Link]("upper case"); // returns negative if o1 < o2,
}
else if (ch >= 'a' && ch <= 'z') { // 0 if o1 = o2,
[Link]("lower case"); // positive if o1 > o2
} public int compare(Object o1,
else if (ch >= '0' && ch <= '9') { Object o2);
[Link]("digit");
} }
else {
[Link](”other"); Vector:
} // end if constructors:
Vector(int initialCapacity,
While: int capacityIncrement)
// computes 1 + 2 + ... + N
// capacityIncrement defaults to 0
int i = 0; Vector(int initialCapacity)
while (i <= N) { // initialCapacity defaults to 10
sum += i; // same as sum = sum + i; Vector()
i++; // same as i = i + 1; boolean add(Object o) // returns true
} // end while void add(int index,
Object element)
for: Object get(int index)
// same as preceeding while int indexOf(Object elem)
for (int i = 0; i <= N; i++) { Object remove(int index)
sum += i; Object set(int index, Object element)
} // end for int size()
// prints odd numbers from 1 to 100, Exceptions:
// in reverse order // Sample code that throws & catches
for (int i = 99; i > 0; i = i – 2) try {
[Link](i); ... some code ...
if (...some condition...)
Class Structure: throw new Exception("optional msg");
public class MyClass { ... more code ...
public static void main(String args[]) { }
.... catch (Exception e) {
} // end main [Link]("got an exception");
} // end class MyClass }
Math Class: Iterator:
// returns random double in [0,1) public interface Iterator {
[Link](); boolean hasNext();
// returns the absolute value of x Object next();
[Link](x); // int & double versions // throws NoSuchElementException
// returns square root of x void remove();
[Link](x) // throws UnsupportedOperationException
// returns maximum of x and y // and IllegalStateException
[Link](x,y); // int & double versions }
// returns minimum of x and y
[Link](x,y); // int & double versions
Comparable:
public interface Comparable {
// returns negative if this < o,
// 0 if this = o,
// postiive if this > o
public int compareTo(Object o);
}