General Java Discussion
Between Session 3 (last time) and this session, I intended to have a very open,
very flexible discussion about a variety of topics using the the Western Town code
as a starting point.
Today's starting point:
The materials below SHOULD loosely correspond to the topics we discuss, although
the order below will NOT necessarily correlate with the order discussed in class.
If you see a topic below that was not covered in class or in your textbook and
you are still confused, please see me.
Java data
- There are 8 primitive data types.
- 4 are whole numbers (byte, short, int, long)
- 2 are
decimal numbers (float, double)
- char
- boolean
- The types in bold are the types we will most often use
- Every other data types that you use in Java are all referential data type. These data types come from one of
several sources.
- First, they may be code that is standard Java. String is an example
of this. There are literally thousands of these classes. Just to
get a little look at how you might find out about them, we spent a few minutes
looking at the Java
API for v1.6 (what is in the labs).
- Second, they may be code that others have written but are not standard
Java. When you download code that the author wrote or that I wrote these
fall into this category
- Third, it may be code that you write. (the difference between this
and #2 is minor but does exist. More on this as the semester evolves)
Vocabulary and discussion points when we introduce defining objects (referential data type)
- Class header
- Instance variables
- Access modifiers
- Method headers and types
- Return type
- parameters
- Access methods
- Mutator methods
- Printing in Java
- Naming conventions
- Class and file names
- Instance variable names
- Method names
- instance variables
- tend to be first
- "always" private access
- order of the three pieces needed to "declare" these MATTERS
- Although IVs get a default value at declaration time, this is
considered VERY bad form to depend on it.
- Although it is possible to initialize IV at declaration time, this is
normally bad style
- constructors
- "the role of the constructor is to initialize the instance variables"
(see above)
- ALWAYS public access (why?)
- Tends to be second. That puts it near the IVs
- Can be told apart from methods because it has the same name as the
class and has no return type (see discussion on methods below)
- methods
- have four parts to their signature
- access
- return type
- name
- parameters
- can have either private or public access depending on purpose
While I have proposed an order for these three things, these are really a
style issue
Creating a "main" class
- But a class on its own is nearly worthless.
- The purpose of a class is to serve as a template for
creating individual objects, or instances, of the class.
WesternTown sweatyPost = new WesternTown();