Java 7 for Absolute Beginners

(nextflipdebug5) #1

CHAPTER 2 ■ JAVA SYNTAX


The issue here is one of style. Individual developers have their own thresholds for when code is too
complex (that is, when the information is too dense). Some folks like it really complex and see nothing
wrong with the original line, whereas others like to have every little thing broken out. To the computer, it
doesn't matter. So this is a purely human issue.
You find that developers get serious about coding style. In fact, some of the most serious
disagreements we’ve seen in the software development community have been about coding style. As a
beginner, your best bet is to keep things simple and avoid overly dense code. Eventually, you'll develop
your own threshold for what's too complex.

Package Declaration


Let's start at the top, with the package declaration:

package com.bryantcs.examples.syntaxExample;

That line says this class belongs to a particular package. A package can contain any number of
classes and interfaces. As you develop more complex programs, you'll want to divide your classes and
interfaces into groups, so that you can more easily find any given class or interface. For example, in web
applications, it's common to divide the classes that talk to the database (the data layer) from the classes
that make web pages for the users (the presentation layer). (Those kinds of applications generally have a
middle layer, too, where the data gets transformed into meaningful information for the user.)
Another good reason for multiple packages is so that separate packages can contain classes with the
same name. For example, the standard Java libraries contain java.util.Date (which is in this example)
and java.sql.Date. They each do slightly different things (for example, they have different ways of
formatting dates). If they were in the same package, only one could be called Date.

■ Note Package declarations are optional; however, they are a good idea. Even if you develop only as a hobby,
you want to keep your projects separate from one another.

By convention, the package declaration follows a particular format. Although not strictly Java syntax
(a program can work without it), most Java developers think ill of you if you don't follow the format. It's
easiest to unravel this format by working from right to left:


  • syntaxExample indicates the local name of the package. AverageTest is a simple
    application and so had just the one package.

  • examples indicates the parent package for the local package. We keep all of the
    examples for this book in the examples package.

  • com.bryantcs is the identifier for all of the applications, to keep them separate
    from those of other people when they get out on the Internet. After all, many
    people are likely to have an examples package, and someone else might even have
    an examples.syntaxExample package. Package declarations often look like reverse
    URLS. You might expect to see something like bryantcs.com/examples/
    syntaxExample (but don't visit that site; it doesn't exist). As we see next, the
    domain portion (com.bryantcs) provides the final bit of insurance that our class is
    unique.

Free download pdf