Sie sind auf Seite 1von 1

Java Cheat Sheet

Objects Variables Types


Create an object: Declare a variable: int 1, -25, 0
Type VarName = new ClassName(params); Visibility Type VariableName; float 1.6f, 6.89f
Car myFerrari = new Car(300); private int theAnswer; double 3.1415925
Car myFiat = new Car(120); private Button trueButton; boolean true, false
Call an object's method: Assign a value to a variable: String "Philipp"
myFerrari.drive(); theAnswer = 42;

Classes Methods Arrays


public class Car { Declare a method: Declare an array:
private int speedField; Visibility ReturnType Name (inputs) {...} int[ ] myInts;
// The Constructor: public void myMethod () { Set the array size:
public Car (int speedInput) { // "void" does not return anything } myInts = new int[5];
speedField = speedInput; public int methodWithParam (int a, int b) { Assign values by index:
} return a + b; }
// Car class methods } myInts[0] = 5;
myInts[1] = 74;
If-Else Blocks Logic Retrieve an element:
if (condition 1) { Symbol Meaning Example 1stElement = myInts[0];
// do x if condition 1 is true == EQUAL TO x == 4 2ndElement = myInts[1];
} else if (condition 2) { != NOT EQUAL TO x != 3 3rdElement = myInts[2];
// do y if condition 2 is true && AND x > 0 && x < 5
} else { || OR x < 0 || x > 10
// default case
}

Das könnte Ihnen auch gefallen