Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recap Week 2 and 3.

Similar presentations


Presentation on theme: "Recap Week 2 and 3."— Presentation transcript:

1 Recap Week 2 and 3

2 Variable types int boolean char double float long byte short String
Object

3 Variable values int i = 0; boolean b = true; char c = ‘x’;
double d = 4.2; String e = “text 5 6.1;”;

4 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;

5 Variable names Cannot be reserved word:
for, if, int Cannot contain certain characters: variable_name is okay variable-name is not

6 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; }

7 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;

8 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

9 Methods Method signature – visibility then return type then name then (arguments) public int method1(int x, int y) private void method2()

10 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;

11 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)

12 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) {…} }

13 New Object Make new objects with new Example ex = new Example();
The arguments given determines which constructor is used Example ex2 = new Example(1);

14 Conditionals if (x > 0) { … }

15 Else Must begin with if Can have any number of else if if (x > 0) {
} else if (x < -7) { else {

16 Printing System.out.println(text) will print text to the terminal

17 String concatenation Strings can be combined with + to make new Strings “abc” + “def” becomes “abcdef” “abc” becomes “abc57” “abc” becomes “12abc”

18 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;

19 Syntax Every statement must end with a semicolon
Conditionals do not end with a semicolon – this is wrong: if (x == 19); {…}

20 Method call Internal method calls – methodName(arguments)
sum = add(1,2); incrementTotal(); External method calls – variableName.methodName(arguments) Square s = new Square(); s.moveLeft();

21 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

22 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

23 Integer division and modulus
5 / 2 = 2 5 % 2 = 1

24 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();

25 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);


Download ppt "Recap Week 2 and 3."

Similar presentations


Ads by Google