head first java chapter 4

Post on 17-Feb-2017

203 Views

Category:

Software

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Head First Java Chapter 4Tom Henricksen

Sending to a method● Arguments

● Parameters

Dog d = new Dog();

d.bark(3);

void bark (int numOfBarks) {

Method returns● Void - void go() {...

● Value - int calculate score() { …

● Compiler

Let’s code

Dog

DogTestDrive

Pass by valueInt x = 7;

Void go(int z) {

Copy x into z

Reference Variables● Pass one or more parameters

● Match parameters

● Promote type

● Pass literal or variables

Encapsulation● Direct Access

● Use methods instead

dog.size = 50;

dog.setSize(50);

GoodDogprivate int size;

public getSize() { return size;}

public setSize(int s) { size = s; }

int x = 3 + dog.getSize();

Object ArraysDog[] pets = new Dog[7];

pets[0].setSize(30);

Instance and local variables● Instance variables

○ private int a;

○ private String name = “Dog”;

● Local variables

○ int s = 0;

Comparisons● Primitives

○ byte a = 0; byte b = 0;

○ a == b;

● Object

○ dogA.equals(dogB); dogA == dogB;

Sharpen your pencil

Which method calls work?

Be the compiler

● Exercise B

top related