

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
this should help you in your java course
Typology: Study notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


INTEGER : byte(8bit),short(16bit),int(32bit), long(64bit), DECIM :float(32bit),double(64bit) , OTHER: boolean(1bit), char (Unicode) HEX: 0x1AF, BINARY: 0b00101, LONG :8888888888888 L CHAR EXAMPLES: ‘a’,’\n’,’\t’,’\’’,’\’,’\”’
Assignment Operator: = (ex: int a=5,b=3; ) Binary Operators (two arguments) : + - * / % Unary Operators : + - ++ -- Boolean Not Operator (Unary) :! Boolean Binary : == != > >= < <= Boolean Binary Only : && || Bitwise Operators : ~ & ^ | << >> >>> Ternary Operator: bool?valtrue:valfalse;
int x = (int)5.5; //works for numeric types int x = Integer.parseInt(“123”); float y = Float.parseFloat(“1.5”); int x = Integer.parseInt(“7A”,16); //fromHex String hex = Integer.toString(99,16);//toHex //Previous lines work w/ binary, other bases
Scanner sc = new Scanner(System.in); int i = sc.nextInt(); //stops at whitespace String line = sc.nextLine(); //whole line System.out.println(“bla”); //stdout System.err.print(“bla”); //stderr,no newline
Integer x = 5; double y = x.doubleValue(); double y = (double)x.intValue(); //Many other methods for Long, Double, etc
//Operator +, e.g. “fat”+”cat” - > “fatcat” boolean equals(String other); int length(); char charAt(int i); String substring(int i, int j); //j not incl boolean contains(String sub); boolean startsWith(String pre); boolean endsWith(String post); int indexOf(String p); //-1 if not found int indexOf(String p, int i); //start at i int compareTo(String t); //“a”.compareTo(“b”) -> - String replaceAll(String str, String find); String[] split(String delim);
StringBuffer is synchronized StringBuilder (Use StringBuilder unless multithreaded) Use the .apend( xyz ) methods to concat toString() converts back to String
Math.abs(NUM),Math.ceil(NUM),Math.floor(NUM) ,Math.log(NUM),Math.max(A,B),Math.min(C,D), Math.pow(A,B),Math.round(A),Math.random()
if( boolean_value ) { STATEMENTS } else if( bool ) { STATEMENTS } else if( ..etc ) { STATEMENTS } else { STATEMENTS } //curly brackets optional if one line
while( bool ) { STATEMENTS } for(INIT;BOOL;UPDATE) { STATEMENTS } //1INIT 2BOOL 3STATEMENTS 4UPDATE 5->Step do{ STATEMENTS }while( bool ); //do loops run at least once before checking break; //ends enclosing loop (exit loop) continue; //jumps to bottom of loop
int[] x = new int[10]; //ten zeros int[][] x = new int[5][5]; //5 by 5 matrix int[] x = {1,2,3,4}; x.length; //int expression length of array int[][] x = {{1,2},{3,4,5}}; //ragged array String[] y = new String[10]; //10 nulls //Note that object types are null by default
//loop through array: for(int i=0;i<arrayname.length;i++) { //use arrayname[i]; }
//for-each loop through array int[] x = {10,20,30,40}; for(int v : x) { //v cycles between 10,20,30, }
//Loop through ragged arrays: for(int i=0;i<x.length;i++) for(int j=0;j<x[i].length;j++) { //CODE HERE }
//Note, multi-dim arrays can have nulls //in many places, especially object arrays: Integer[][] x = {{1,2},{3,null},null};
Static Declarations: public static int functionname( … ) private static double functionname( … ) static void functionname( … ) Instance Declarations: public void functionname( … ) private int functionname( … ) Arguments, Return Statement: int myfunc(int arg0, String arg1) { return 5; //type matches int myfunc } //Non-void methods must return before ending //Recursive functions should have an if //statement base-case that returns at once
public class Ball {//only 1 public per file //STATIC FIELDS/METHODS private static int numBalls = 0; public static int getNumBalls() { return numBalls; } public static final int BALLRADIUS = 5;
//INSTANCE FIELDS private int x, y, vx, vy; public boolean randomPos = false;
//CONSTRUCTORS public Ball(int x, int y, int vx, int vy) { this.x = x; this.y = y; this.vx = vx; this.vy = vy; numBalls++; } Ball() { x = Math.random()100; y = Math.random()200; randomPos = true; }
//INSTANCE METHODS public int getX(){ return x; } public int getY(){ return y; } public int getVX(){ return vx; } public int getVY(){ return vy; } public void move(){ x+=vx; y+=vy; } public boolean touching(Ball other) { float dx = x-other.x; float dy = y-other.y; float rr = BALLRADIUS; return Math.sqrt(dxdx+dydy)<rr; }
}
//Example Usage: public static void main(String[] args) { Ball x = new Ball(5,10,2,2); Ball y = new Ball(); List
Single Inheritance with “extends” class A{ } class B extends A{ } abstract class C { } class D extends C { } class E extends D Abstract methods abstract class F { abstract int bla(); } class G extends F { int bla() { //required method return 5; } } Multiple Inheritance of interfaces with “implements” (fields not inherited) interface H { void methodA(); boolean methodB(int arg); } interface I extends H{ void methodC(); } interface K {} class J extends F implements I, K { int bla() { return 5; } //required from F void methodA(){} //required from H boolean methodB(int a) { //req from A return 1; } void methodC(){} //required from I } Type inference: A x = new B(); //OK B y = new A(); //Not OK C z = new C(); //Cannot instantiate abstract //Method calls care about right hand type (the instantiated object) //Compiler checks depend on left hand type
class MyClass
List
List Usage: boolean add(T e); void clear(); //empties boolean contains(Object o); T get(int index); T remove(int index); boolean remove(Object o); //remove uses comparator T set(int index, E val); Int size();
List Traversal: for(int i=0i<x.size();i++) { //use x.get(i); }
//Assuming List
Queue
Queue Usage: T element(); // does not remove boolean offer(T o); //adds T peek(); //pike element T poll(); //removes T remove(); //like poll Traversal : for(T e : x) {} Set
V get(K key); Set
Traversal: for-each w/ keyset/values
A queue that is always automatically sorted using the comparable function of an object
public static void main(String[] args) { Comparator
Sort Example: //Assuming List
for (final String[] s : data) { System.out.println(s[0]+" "+s[1]); } } } More collections static methods: Collections.max( … ); //returns maximum Collections.min( … ); //returns maximum Collections.copy( A, B); //A list into B Collections.reverse( A ); //if A is list