Basic Language Rules and OOP ... Basic Language Rules and OOP Concepts P A R I T Chapter 1 Language

  • View

  • Download

Embed Size (px)

Text of Basic Language Rules and OOP ... Basic Language Rules and OOP Concepts P A R I T Chapter 1 Language

  • Basic Language Rules and OOP Concepts

    Basic Language Rules and OOP Concepts


    A R T


    Chapter 1 Language Fundamentals Chapter 2 Declarations and Access Control Chapter 3 Overloading, Overriding, Runtime Type,

    and Object Orientation Chapter 4 Garbage Collection Chapter 5 The java.langPackage

    366c01.qxd 10/8/01 8:43 AM Page 1

  • 366c01.qxd 10/8/01 8:43 AM Page 2

  • NEWBIE SOME EXPERIENCE EXPERT 40 hours 20 hours 5 hours

    C H

    A P T

    E R1Language

    Fundamentals ITINERARY

    •• Objective 1.01 Identify correctly constructed source files, pack- age declarations, import statements, class decla- rations (of all forms including inner classes), interface declarations and implementations (for java.lang.Runnable or other interfaces described in the test), method declarations (including the main() method that is used to start execution of a class), variable declarations and identifiers.

    •• Objective 1.02 State the correspondence between index values in the argument array passed to a main() method and command-line arguments. Identify all Java programming language keywords and correctly constructed identifiers.

    •• Objective 1.03 State the effect of using a variable or array ele- ment of any kind when no explicit assignment has been made to it.

    •• Objective 1.04 State the range of all primitive data types and declare literal values for String and all primi- tive types using all permitted formats, bases, and representations.



    366c01.qxd 10/8/01 8:43 AM Page 3

  • Java is a high-level, general purpose, object-oriented programming language that is similar to other object-oriented programming (OOP) languages. Programs writ- ten in Java are not compiled directly into machine-executable code; they are com- piled into bytecode, a machine independent instruction set. Each Java enabled system includes a Java Runtime Environment (JRE) specifically designed to work with the system’s operating software. Each JRE includes a “software-execution engine,” the Java Virtual Machine (JVM). The JVM acts as an interpreter, translat- ing bytecode into machine-specific instructions which are then executed on the host platform. Multiplatform applications written in other languages have many distribution versions because a different, machine-executable version must exist for each targeted operating platform. Java programs are compiled to one bytecode ver- sion which can then be executed on any Java enabled system. This is Java’s ultimate strength and the source of its write once, run anywhere (WORA) reputation.

    Whew! That was a mouthful! Feeling a little like you did the first time you looked at the Java documentation? Wondering how on earth you’re going to remember all that? Relax! You don’t have to. Feel better? Good. Now take a deep breath…you have to understand it! The exam doesn’t test your memory. It’s full of code exhibits—10-, 20-, and 30-line code examples—and you’re expected to know what they do. That means you have to know how to write code the compiler will understand and know how the compiler will interpret it.

    And that is what this chapter is all about. It ain’t fascinating reading, but it will introduce you to the layout of typical Java programs and the language we use to construct them: the language you’ll use everyday as a Java programmer. So, are you ready? Let’s go!

    Identify Correctly Constructed Source Files and Declarations

    Writing a Java source file is simple; all you need is a text editor. Writing sourcefiles that will compile without error is a little trickier. The Java compiler is a specialized application that reads source files and translates them into bytecode. Because the compiler is, in essence, a translation machine, it expects your files to be written in the Java language and it expects the contained statements to be written according to the rules of the Java language. Just as an English speaking person would have difficulty understanding an English sentence written in accordance

    Objective 1.01

    4 B A S I C L A N G U A G E R U L E S A N D O O P C O N C E P T S

    366c01.qxd 10/8/01 8:43 AM Page 4

  • CHAPTER 1 Language Fundamentals 5

    with the rules of French grammar, the compiler will have difficulty translating statements written following the rules of Basic, C, or another programming lan- guage. In this objective, you’ll learn how source files and their contained state- ments are written and how the compiler deals with nonconforming text.

    Organizing Java Source Files Java applications are organized into packages. A package is composed of compila- tion units or source files. Source files are plain, vanilla text files that describe defined types: classes and interfaces. A defined type is a template that your programs can use over and over again to create objects. Objects are individual software entities, instances of a class. It helps to think of a class as an ice cube tray. Every time you want ice cubes, you fill the tray with water, stick it in the freezer, and a few hours later you pop out a dozen nicely formed cubes of ice. A class is very similar, but you are not limited to creating a dozen or ten dozen ice cubes at a time; you can create as many as you like, as often as you like, and better yet, you don’t have to wait a few hours for them to freeze into shape!

    Packages let you organize source files (the text files containing your class def- initions) into related groups. Package names are hierarchical and correspond to the directory structure of the hard drive where your source files are stored. They are generally grouped according to their use. For example, classes created by Sun for the Java Development Kit (JDK) bear the package name java and are further organized into subgroups: awt (Abstract Window Toolkit), lang (language), math (mathematics),io (input/output), and so on. The original source files would have been stored in a matching directory structure :

    directory ………java

    subdirectory …awt

    subdirectory …lang

    subdirectory … math

    subdirectory … io

    You can learn more about the Java Language Specification by

    going to


    Travel Assistance

    366c01.qxd 10/8/01 8:43 AM Page 5

  • Package names are written using dot notation. The preceding packages would be written as : java.awt, java.lang, java.math, and

    The JDK install files include an archive (src.jar) of all the source files used to create the JDK. To view a source file for a particular class, unpack the file from the src.jar archive. For example, to unpack the source file for java.awt. Button on a Win32 platform, open a DOS prompt and switch to the JDK direc- tory. Then enter the following command:

    jar xf src.jar src/java/awt/

    The new directories: src, java, and awt will be created. You may view the file using any text editor. You’ll find that any given source file fol- lows a standard structure.

    The Structure of a Java Source File A source file has the file extension .java. (compiled source files are given the file extension .class). A source file has three basic sections, all of which are optional:

    • A package declaration • Import declarations • Defined type (class or interface) declarations The following is an example of a simple Java source file:

    /** */

    package ch36601;


    public class SimpleJava {

    // class body


    You can view the compiled bytecode of any .class file using the javap.exe utility that ships with the JDK. For example, executing javap -c ch36601. SimpleJava, where -c is the option to disassemble code, displays the following on a Win32 system:

    Compiled from

    public class ch36601.SimpleJava extends java.lang.Object {

    public ch36601.SimpleJava();

    6 B A S I C L A N G U A G E R U L E S A N D O O P C O N C E P T S

    366c01.qxd 10/8/01 8:43 AM Page 6

  • }

    Method ch36601.SimpleJava()

    0 aload_0

    1 invokespecial #1

    4 return

    CHAPTER 1 Language Fundamentals 7

    Descriptive text included in a source file is referred to as a comment. Java treats as comments any single-line text following the // characters and any block of text between the /* and */ characters. Comments enclosed between the /** and */ characters are used by the javadoc.exe utility to create Application Programming Interface (API) documentation. The documentation installed with the JDK is an example of API documentation generated by the JavaDoc utility. Comments are not included in compiled code.

    An empty file with the .java extension will compile with-

    out error, although it will be useless.

    Travel Advisory

    Java documentation (JavaDoc) is not specifically covered on the exam.

    You do, however, need to be able to distinguish a comment from an

    executable statement.

    Exam Tip

    The package Declaration You can use a package declaration (remember, it’s optional) to group-related source files. The declaration has the following syntax:

    package packageName;

    All complete Java statements end with a semicolon ( ; ).A line which

    only consists of a semicolon is an empty statement.

    Exam Tip

    366c01.qxd 10/8/01 8:43 AM Page 7

  • 8 B A S I C L A N G U A G E R U L E S A N D O O P C O N C E P T S