Java Cheatsheet http://introcs.cs.princeton.edu/java/11cheatsheet/ Tim Coppieters Laure Philips Elisa Gonzalez Boix
Hello World bestand genaamd HelloWorld.java naam klasse main methode public class HelloWorld { public static void main(string[] args) { System.out.println("Hello world!"); statement body
Primitive Value Types = a variable of this type contains the value directly. Allocated on the stack. Passed by copy. values operators literal boolean boolean values &&! true false char character a @ % \u0000' byte 8 bit integer -128 x 127 short 16 bit integer -32.768 x 32.767 int 32 bit integer + - * / % -2^31 x 2^31-1 long 64 bit integer -2^63 x 2^63-1 float 32 bit float + - * / double integer = geheel getal float = reëel getal 64 bit float http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Primitive Reference Values = Variable of this type contains a reference (aka pointer) to the value. Allocated on the heap. Passed by reference. = Objects or Arrays String (=Object) - use String.equals(s), not ==!!!! value wrappers (=Object): Short, Integer, Long, Double, Float, Byte, etc. - provides more functionality - works with ==
Array int[] a = [1,2,3,4,5,6,7]; int sum = 0; for (int i = 0; i < a.length; i++) { sum += a[i];
ArrayList import java.util.arraylist; ArrayList<String> array = new ArrayList<String>(); for (String s: array) { System.out.println(s); for (int i = 0; i < array.size(); i++) { String s = array.get(i); System.out.println(s); Iterator<String> it = array.iterator(); while(it.hasnext()) { String s = it.next(); http://docs.oracle.com/javase/7/docs/api/java/util/arraylist.html
ArrayList ArrayList<T> array = new ArrayList<T>(); ArrayList<T> array = new ArrayList<T>(size: int); array.add(element: T) array.add(position: int, element: T) array.set(position: int, element: T) array.remove(position: int) array.remove(element: T) array.get(position: int) array.sublist(start: int, end: integer) array.toarray(): T[] array.contains(element: T) array.size(): integer array.isempty(): boolean http://docs.oracle.com/javase/7/docs/api/java/util/arraylist.html
Casting
Classes public class Calculator { private int mode; klasse attribuut constructor (genaamd naar klasse) public Calculator(int mode) { this.mode = mode; public double add(double i, double j) { // tel i en j op public int fac(int i) { // bereken faculteit methoden void main(string[] args) { Calculator calc = new Calculator(2); calc.add(10, 20);
Subclassing public class Publication { public void sell() {... public class Book extends Publication { public String getauthor() {... Publication p = new Book() Book b = new Publication()
Abstract Abstract class - can declare normal fields or methods - can include abstract methods - cannot be instantiated - can be subclassed Abstract method - has no implementation, only signature http://docs.oracle.com/javase/tutorial/java/iandi/abstract.html
Abstract public abstract class Animal { public String name; public abstract String makesound(); public abstract class CatLike extends Animal { public class Cat extends Catlike { public String makesound() { return purr ; public class Tiger extends Catlike { public String makesound() { return raauwr ;
Interface cannot declare fields or constructors methods are abstract by default can extend multiple interfaces classes can implement multiple interfaces http://docs.oracle.com/javase/tutorial/java/iandi/createinterface.html
Interface/Abstract public interface Animal { public void eat(); Classes implementing this interface must implement eat(). public interface Herbivore extends Animal { void eatplant(); Classes implementing this interface must implement eat() and eatplant(). public interface Carnivore extends Animal { void eatmeat(); Classes implementing this interface must implement eat() and eatmeat(). public abstract class Omnivore implements Herbivore, Carnivore { public void eat() { this.eatplant(); this.eatmeat(); Provides concrete implementation for eat(), but must still implement eatplant() and eatmeat(). Therefore abstract public class Human implements Omnivore { public void eatplant() { public void eatmeat() { Provides concrete implementation for eatplant() and eatmeat(). Everything is implemented, not abstract.
Compilation/Execution > javac Hello.java // compiler: generates Hello.class > java Hello // interpreter: executes the bytecode