Week 2 Notes and Questions
- What is a "class"
- A way to describe the behaviour and properties of a concept for the
purposes of programming. In java, use "methods" or "functions" for the
behaviour, and "fields", "members" or "attributes" for the properties.
- Example: for a GUI's "window" class, what might some methods be/what
might some properties be?
- Classes relate to each other by calling each other's methods. So it's
important to decide what methods you need.
- Usually, the properties of a class are available only to the class
itself. Why? What are the advantages of making the properties of a
class available to other classes?
- The idea is that by thinking in terms of "classes" rather than
"programs", you can design software in a modular reusable way
- What is a Java "program"
- java source files are compiled into bytecode by the "javac" command.
Each class that the compiler finds will be given its own "class" file.
- there is no "link" stage for Java - all the class files are left
separate, but they do contain information about what other classes they
reference.
- when the java compiler processes a source file that refers to another
class, it will look for that class and make sure that (a) it exists, and
(b) that it is being used properly. If the class doesn't exist then the
compiler will try and find the source file for it and compile that too
(and so on).
- java compiler is itself a java program. Class files are the same
whatever platform.
- java classes are executed by the JVM. JVM may be invoked:
- by "java" command on the command line
- by browser when running an applet
- by servlet container (e.g. tomcat)
- JVM is a "virtual machine". So it can allow many classes to execute at
once, independently of each other or communicating. JVM is platform
specific but appears exactly the same as far as java classes are
concerned.
- Q: how does JVM know which bit of the class you want to execute?
A: depends on what context it's being invoked, e.g. "main" or "service"
or "appletmain" or whatever.
- The JVM uses "classpath" to find class files. When you tell the JVM to
execute a class, it uses "classpath" to find it, and then recursively
loads all the other classes which are referenced, again using "classpath"
to find each of them. Classpath is like a VMS search list, or a UNIX or
Windows "path" in that it contains a list of places to look; the main
difference is that it can include "jar" files as well as directories
Questions for this week (same rules as last time, no asking questions):
- Find out how many classes get loaded when you run "helloworld"
- Produce some html documentation for a program you've written
- write a "hello world" program and another program which says "goodbye
world". Now make the "helloworld" program execute the "goodbye world"
program. I.e. you'll be able to do:
% java goodbyeworld
goodbye world
% java helloworld
hello world
now I will run goodbye world
goodbye world
here I am back in hello world
%
- sign up to the java developer connection, and find out from there three ways
that you could count the number of words in a string of text. Write a
program that uses one of these ways to do this:
% java countwords "this sentence has five words" "this one doesn't"
You gave me 2 sentences
Sentence 1 has 5 words
Sentence 2 has 3 words
Back to index page