Java Reference
In-Depth Information
first time in the source, it has had a type name in front of it, such as int or String . In light of
this, now try the following in the Code Pad:
int sum = 0;
sum = 99 + 3;
This time there is no complaint, because sum has been introduced with a type and can be used
without repeating the type thereafter. If you then type
sum
on a line by itself (with no semicolon), you will see the value it currently stores.
Now try this in the Code Pad:
String swimmer = "cat" + "fish";
swimmer
One again, we have given an appropriate type to the variable swimmer , allowing us to make an
assignment to it and find out what it stores. This time we chose to set it to the value we wanted
at the same time as declaring it.
What would you expect to see after the following?
String fish = swimmer;
fish
Try it out. What do you think has happened in the assignment?
Exercise 2.80 Open the Code Pad in the better-ticket-machine project. Type the following
in the Code Pad:
TicketMachine t1 = new TicketMachine(1000);
t1.getBalance()
t1.insertMoney(500);
t1.getBalance()
Take care to type these lines exactly as they appear here; pay particular attention to whether
or not there is a semicolon at the end of the line. Note what the calls to getBalance return
in each case.
Exercise 2.81 Now add the following in the Code Pad:
TicketMachine t2 = t1;
What would you expect a call to t2.getBalance() to return? Try it out.
Exercise 2.82 Add the following:
t1.insertMoney(500);
Search WWH ::




Custom Search