Você está na página 1de 2

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 (Character.isLowerCase(ch))
student5 ch = Character.toUpperCase(ch);
remember that upper & lower case matters! // returns true if ch is a decimal digit
// (i.e. '0','1',...,'9'
Primitive Types: if (Character.isDigit(ch))...
int // translates ch into the equivalent int
double // (0 to 9)
char int i = Character.digit(ch, 10);
boolean Arrays:
// create array of 10 doubles
Comparisons (primitive types only): double arr[] = new double[10];
< > <= >= == != number of elements in arr: arr.length
(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 = s.length(); // len gets 12 x += 3; // means x = x + 3;
char c = s.charAt(1); // c gets 'e' x -= 7; // means x = x – 7;
if (s.equals(t))
// true if t exactly the same as s Declarations & Assignments:
if (s.equalsIgnoreCase(t)) int x;
// same as above but ignores case x = 14;
int x = s.compareTo(t); double d = 15.2;
// 0 if they're equal
// positive if s is greater Output:
// negative if t is greater System.out.println("x = " + x
String s1 = "abcdefg"; + " and y = " + y);
String s2 = s1.substring(1,4); // x & y can be any type
// s2 gets "bcd"
String s3 = s1.substring(3); Input:
// s3 gets "defg" import hsa.*; // at start of class
int pos = s1.indexOf('c'); String s = Stdin.readLine();
// pos gets 2 // s gets entire line
String s = Stdin.readString();
// String <-> int conversions // s gets string up to white space
int x = Integer.parseInt(s); int i = Stdin.readInt();
// if s = "123", x gets 123 double d = Stdin.readDouble();
// reminder: parseInt can throw char c = Stdin.readChar();
// a NumberFormatException
// (subclass of RuntimeException) If Statements:
String s = Integer.toString(x); if (a < b) {
// if x = 123, s gets "123" System.out.println("b is bigger");
c = b;
}
else {
System.out.println("a is bigger");
c = a;
} // end if
If – else if – else if ....: Comparator:
if (ch >= 'A' && ch <= 'Z') { public interface Comparator {
System.out.println("upper case"); // returns negative if o1 < o2,
}
else if (ch >= 'a' && ch <= 'z') { // 0 if o1 = o2,
System.out.println("lower case"); // positive if o1 > o2
} public int compare(Object o1,
else if (ch >= '0' && ch <= '9') { Object o2);
System.out.println("digit");
} }
else {
System.out.println(”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 {
System.out.println(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 System.out.println("got an exception");
} // end class MyClass }
Math Class: Iterator:
// returns random double in [0,1) public interface Iterator {
Math.random(); boolean hasNext();
// returns the absolute value of x Object next();
Math.abs(x); // int & double versions // throws NoSuchElementException
// returns square root of x void remove();
Math.sqrt(x) // throws UnsupportedOperationException
// returns maximum of x and y // and IllegalStateException
Math.max(x,y); // int & double versions }
// returns minimum of x and y
Math.min(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);
}

Você também pode gostar