Recap Week 2 and 3
Variable types int boolean char double float long byte short String Object
Variable values int i = 0; boolean b = true; char c = ‘x’; double d = 4.2; String e = “text 5 6.1;”;
Variable declaration and assignment Declaration (only once) – type then name int i; Assignment (any number of times) – name = value i = 7; Can be performed together: int i = 7;
Variable names Cannot be reserved word: for, if, int Cannot contain certain characters: variable_name is okay variable-name is not
Scope Variables exist only in their scope, defined by {} public class Example { private int field; public void method(int argument) { int local = 0; … if (…) { int local2 = 1; }
Fields Declared outside of methods Fields have visibility modifier Public variables can be accessed anywhere, private variables can only be accessed by the owner Declaration of field – visibility then type then name public int i; private int j;
Default value Fields have default values before any assignment is made int : 0 boolean : false Any object : null Local variables do not have default, they always require an assignment before use
Methods Method signature – visibility then return type then name then (arguments) public int method1(int x, int y) private void method2()
Return type void means no return If a method is non-void, it must return public boolean isPositive(int x) { if (x > 0) { return true; } else { return false;
Constructors A constructor is run when an object is first made Constructor signature – visibility then class name then (arguments) public Example(int arg1, String arg2)
Multiple constructors Can have multiple constructors if they have different arguments Only one will be run when the object is made public class Example { public Example() {…} public Example(int arg) {…} }
New Object Make new objects with new Example ex = new Example(); The arguments given determines which constructor is used Example ex2 = new Example(1);
Conditionals if (x > 0) { … }
Else Must begin with if Can have any number of else if if (x > 0) { … } else if (x < -7) { else {
Printing System.out.println(text) will print text to the terminal
String concatenation Strings can be combined with + to make new Strings “abc” + “def” becomes “abcdef” “abc” + 5 + 7 becomes “abc57” 5 + 7 + “abc” becomes “12abc”
Comments //This is a single-line comment int i = 17; //this bit gets ignored /* this is a multiline comment*/ int i = /* we can put multiline comments anywhere */ 18;
Syntax Every statement must end with a semicolon Conditionals do not end with a semicolon – this is wrong: if (x == 19); {…}
Method call Internal method calls – methodName(arguments) sum = add(1,2); incrementTotal(); External method calls – variableName.methodName(arguments) Square s = new Square(); s.moveLeft();
Primitives hold values int a = 0; int b = 1; a = b; b = 2; After these calls, a is 1 and b is 2 Primitives don’t have methods
Objects hold references Square s1 = new Square(); Square s2 = s1; s1.moveLeft(); Both s1 and s2 are pointing to the same Square which has just been moved left
Integer division and modulus 5 / 2 = 2 5 % 2 = 1
Null null is a special object value Any object variable can be assigned null Square s = null; Null objects don’t hold any information – this will cause an error: s.moveLeft();
Chain method calls If a method returns an object, its methods can be immediately called This: String name = getName(); char c = name.charAt(0); Is equivalent to: char c = getName().charAt(0);