Chapter 1

Memory (page 5): Data is encoded as a series of bits (zeros and ones). Memory stores data and program instructions for the CPU to execute. A memory unit is an ordered sequence of bytes each holding 8 bits.

If a computer needs to store a large number that cannot fit into a single byte, it uses several adjacent bytes. No two data items can share or split the same byte. A byte is the minimum storage unit.

A program and its data must be brought to memory before they can be executed. A memory byte is never empty, but its initial content may be meaningless to your program. The current content of a memory byte is lost whenever new information is placed in it.

Every byte has a unique address. The address is used to locate the byte for storing and retrieving data. Since bytes can be accessed in any order, the memory is also referred to as RAM – random access memory.

Programs (page 7): Computer programs, known as software, are instructions to the computer. Computers are told what to do via programs. Without programs, a computer is an empty machine.

A computer speaks machine language which is a set of primitive instructions built into every computer. The instructions are in the form of binary code, so binary codes must be entered for various instructions. Programming this is tedious and is highly difficult to read and modify.

Assembly language is a low-level programming language in which a mnemonic is used to represent each of the machine-language instructions. Assembly languages were developed to make programming easy. A computer cannot understand assembly language, so a program called an assembler is used to convert assembly language programs into machine codes.

High-level languages are English-like and easy to learn and program. There are over 100 high-level languages that include:

  • COBOL (Common Business Oriented Language)
  • FORTRAN (Formula Translation)
  • BASIC (Beginner All-purpose Symbolic Instructional Code)
  • Pascal (named for Blaise Pascal)
  • C (whose developer designed B first)
  • Visual Basic
  • C++ (an object-oriented language based on C)
  • C# (a Java-like language developed by Microsoft)
  • Java

A program written in high-level language is called a source program. Since a computer cannot understand a source program, a program called a compiler is used to translate the source program into machine language program. The machine language program is often then linked with other supporting library code to form an executable file. The executable file can be executed on the machine.

A source program can be ported/moved to any machine with appropriate compilers. The source program must be recompiled, however, because the machine language program can only run on a specific machine. Java was designed to run on any platform. With Java, you write the program once and compile the source program into a special type of machine language code known as bytecode. The bytecode can then run on any computer with aJava Virtual Machine (JVM). The Java Virtual Machine is software that interprets the Java bytecode. Java bytecode can run on a variety of hardware platforms and operating systems

Java bytecode is interpreted. The difference between compiling and interpreting is as follows: Compiling translates the high-level code into a target language code as a single unit. Interpreting translates the individual steps in a high-level program one at a time rather than the whole program as a single unit. Each step is executed immediately after it is translated.

Number Systems(page 11): Computers use binary numbers internally because storage devices like memory and disk are made to store 0s and 1s. A number or character inside a computer is stored as a sequence of 0s and 1s. Each 0 or 1 is called a bit. The binary number system has two digits, 0 and 1. Binary numbers are not intuitive. When you write a number like 20 in a program, it is assumed to be a decimal number. Internally, computer software is used to convert decimal number into binary numbers and vice versa. Binary numbers tend to be long and cumbersome.

Hexadecimal numbers are often used to abbreviate binary numbers with each hexadecimal digit representing exactly four binary digits. The hexadecimal number system has 16 digits:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F

A, B, C, D, E and F correspond to the decimal numbers 10, 11, 12, 13, 14 and 15 respectively.

The decimal number system has 10 digits, and the position values are integral powers of 10. We say that 10 is the base or radix of the decimal number system. Similarly, the base of the binary number system is 2 since the binary number system has two digits, and the base of the hex number system is 16 since the hex number system has 16 digits.

A table of conversions between binary numbers and hexadecimal numbers is on page 13.

Java, the World Wide Web and Beyond (page 14)

The World Wide Web is an electronic information repository that can be accessed on the Internet from anywhere in the world. The Internet is the infrastructure of the Web.

Java was developed by a team led by James Gosling at Sun Microsystems. Originally called Oak, it was designed in 1991 for use in embedded consumer electronic appliances. It was renamed Java in 1995 and was redesigned for developing Internet applications.

Java is a full-featured, general-purpose programming language that is capable of developing mission-critical applications. It is not only used for Web programming, but also for developing standalone applications across platforms on servers, desktops and mobile devices.

Java programs that run from a web browser are called applets. Applets use a modern graphical user interface with buttons, text fields, text areas, radio buttons, etc., to interact with users and process their requests. Applets make the Web responsive, interactive and fun to use.

Java can also be used to develop applications on the server side. These applications, called Java servlets or JavaServer Pages (JSP) can be run from a web server to generate dynamic web pages.

The Java Language Specification, API, JDK and IDE (page 16)

The Java language specification is a technical definition of the language that includes the syntax and semantics of the Java programming language.

The application program interface (API) contains predefined classes and interfaces for developing Java programs. There are three editions of the Java API:

  1. Java 2 Standard Edition (J2SE) (can be used to develop client-side standalone applications or applets)
  2. Java 2 Enterprise Edition (J2EE) (can be used to develop server-side application such as Java servlets and JavaServer Pages)
  3. Java 2 Micro Edition (J2ME) (can be used to develop applications for mobile devices)

J2SE is used to introduce Java programming. There are many versions of J2SE the latest being 5.0. Sun releases each version of J2SE with a Java Development Toolkit (JDK). For J2SE 5.0, the Java Development Toolkit is called JDK 5.0. JDK consists of a set of separate programs for developing and testing Java programs, each of which is invoked by a command line.

Three major development tools are:

  1. Jbuilder by Borland (
  2. NetBeans Open Source by Sun (
  3. Eclipse Open Source by IBM (

A Java development tool is software that provides an integrated development environment (IDE) for rapidly developing Java programs. Editing, compiling, building, debugging, and online help are integrated into one graphical user interface.

A Simple Java Program (page 17)

A Java program can be written in many ways:

  • Applications are standalone executable programs on any computer with a JVM
  • Applets are special kinds of Java programs that run from web browser
  • Servlets are special kinds of Java programs that run from a web server to generate dynamic web content

Every Java program must have at least one class. A class is a construct that defines data and methods. Each class has a name. Class names start with an uppercase letter. Every Java program begins with a class declaration in which the keyword Class is followed by the class name. For example

public class ComputeArea {

// Some code here

}

In order to run a class, the class must contain a method named main. The JVM executes the program by invoking the main method.

A method is a construct that contains statements. The main method in the program on page 17 contains the System.out.printIn statement. This statement prints a message “Welcome to Java!” to the console. ln places a line break after the statement.

Creating, Compiling and Executing a Java Program(page 18)

The Java programming development process consists of creating/modifying source code, compiling and executing programs. Like other programming languages, it is subject to syntax errors.

Java source files must end with the extension .java and must have the exact same name as the public class name. In the example on page 18, the public class name is Welcome, so the source file should be named Welcome.java.

A Java compiler translates a Java source file into a Java bytecode file. The following command compiles a Java source file: javac [source filename].java

If there are no syntax errors, the compiler generates a bytecode file with a .class extension. To execute a Java program is to run the program’s bytecode. The following command runsthe bytecode: javac [source filename]

Do not use the extension .class in the command line when executing the program. The JVM assumes that the first argument in the command is the filename and then fetches filename.class to execute. It would attempt to fetch filename.class.class if you used java filename.class in the command line. In short, .class is assumed.

When executing a Java program, the JVM first loads the bytecode of the class to memory using a program called the class loader. If your program uses other classes, the class loader dynamically loads them just before they are needed. After a class is loaded, the JVM uses a program called bytecode verifier to check the validity of the bytecode and ensure the bytecode does not violate Java’s security restrictions. Java enforces strict security to make sure that Java programs arriving from the network do not harm your PC.

Anatomy of a Java Program (page 20)

A Java application program is comprised of the following components:

Comments: / Document what the program is andhow the program is constructed. Comments help programmers to communicate and understand the program.
Reserved Words: / A.K.A. keywords, are words that have specific meaning to the compiler and cannot be used for other purposes in the program. When the compiler sees the word class, it understands that the word after class is the name for the class.
Modifiers: / These specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers are public and private.
Statements: / Statements represent an action or a sequence of actions. The statement System.out.println(“Welcome to Java”); is a statement to display the greeting “WelcometoJava!” EVERY statement ends with a semicolon.
Blocks: / The braces{ } in the program form a block that groups the components of the program. Each Java program begins and ends with a brace. Nested classes and methods also have open and closing braces.
Classes: / The class is the essential Java construct. A program is defined by using one or more classes.
Methods: / System.out is known as the standard output object. println isa method in the object which consists of a collection of statements that perform a sequence of operations to display a message to the standard output device
main Method: / Every Java application must have a user-declared main method that defines where the program execution begins. The JVM executes the application by invoking the main method. The main method is as follows:
public static void main(String[ ] args) {
//Statements
}

Class Lecture – May 10, 2008 – Week 1

What are Java Programs?

  • A Java program is made up of class definitions.
  • A class definition contains a header and a body.
  • The body contains methods and properties/attributes.
  • A method is a named section of code that can be called by its name (i.e., a function).
  • A property is a variable defined within the class.
  • Java programs come in a variety of application formats and contexts:

–Java applications (what we will focus on).

–Java applets (run on web pages).

–Beans (components).

–Enterprise Java (back end server applications).

What are the states or stages of Java Programs?

  • A Java program starts with source code.
  • The Java program is compiled into bytecode.
  • The Java program is executed within the Java runtime environment.

–Bytecode is interpreted by JRE.

–This is different than a C++ EXE.

–Enables the Java platform independence.

Java Applications and Applets

  • Java Applications

–A Stand-alone program.

–Runs independently at command line (under JRE).

–Has a main() method that is starting point of application.

–No HTML file.

  • Java Applets

–Embedded program.

–Runs in a Web browser.

–No main() method.

–Requires an HTML file.

–Run using JDK’s appletviewer or via web browser.

Java Class Libraries

  • To make use of any of the libraries, you must use an import statement.

–The exception here is java.lang, which is automatically included in all programs.

–Difference between these two statements is that first imports all class definitions of java.util, while second imports just JApplet.

import java.util.*;

import javax.swing.JApplet;

Java Language Fundamentals

  • Java IS case sensitive.
  • Statements normally end with a semi-colon.
  • Statements can be grouped into blocks with braces {}.
  • Statements are free format -- spaces can be freely used and code can be broken up into multiple lines.
  • Indentation is commonly used to improve code readability.

Class Lecture – May 10, 2008 – Week 1 (Continued)

Java Data Types

  • Your programs will need to store and use data.
  • In programming, we must map the data types available in the language to the problem domain.

–With OOP, we can create our own data types that map more directly to the problem domain.

–However, we must eventually work with the types available in the language.

  • There are two general categories of data in a computer program:

–Numeric.

  • Whole numbers (integers).
  • Decimal numbers (floating point).

–Non-numeric.

  • Strings of characters.
  • Single characters.
  • Boolean (true/false).

–These are the intrinsic data types of the language.

  • Your programs will also eventually use classes.
  • Text reference begins at Page 34.
  • Four integer types available:

–byte

–short

–int

–long

  • See Table 2.1 for value ranges for these types and for the memory requirements.

Declaring Variables

  • Variables are storage locations in memory that you can refer to by a name that has meaning to you for the context of the application.
  • Variables are declared by selecting data type and variable name:

int myintvalue, j, k3;

double salary = 10000.0;

  • Variables are NOT initialized automatically.
  • Use of a variable before initialization is illegal in Java.
  • May be initialized at time of declaration, or via assignment statement

Arithmetic Expressions

  • Most programs will need to support numeric calculations.
  • This is accomplished via statements that combine variables with the arithmetic operators.

–+, -, *, /, %.

  • These operators are the binary operators.

–They require left and right operands.

  • There are also a number of unary operators, notably:

–++, --, +=, -=, *=, /=.

  • The complete set of Java operator’s precedence is shown on Page 86.

Class Lecture – May 10, 2008 – Week 1 (Continued)

Type Casting

  • Java sometimes allows mixed arithmetic expressions and sometimes it doesn’t.
  • When it doesn’t, we can usually convince it to do so by type casting.
  • Type casting involves stating what your target data type is.
  • Syntax is to place target data type within parentheses, in front of data to be converted:

int j;

j = (int) 3.24;

mycode.java

Compile yields .class (bytecode)

javac mycode.java

To run

java mycode

'A' ==> single byte character

"A" ==> String literal object

Both of above are literal constants.

Our two sweet spot data types for numbers:

int

double

Remainder/modulus

20 % 5 ==> 0

21 % 5 ==> 1

int b = 10;

int c = 2;

int d = 5;

int a = b + c * d;

| |

10

| |

20

Operator precendence: For example, multiplication trumps addition. Parentheses change the rules of operator precedence. Items in parentheses are evaluated first.

int a = (b + c)*d;

int temp;

temp = b + c;

int a = temp * d;

These statements all add 1 on to a:

a++; ==> post increment

++a; ==> pre increment

a += 1;

a = a + 1;

double x = 3.14;

double y = x + 10; ==> OK, automatic conversion

int z = x + 10; ==> illegal

int z = (int) (x + 10); ==> explicit type cast

int z = (int) x + 10;

Class Lecture – May 10, 2008 – Week 1 (Continued)

JavaDoc is Java Documentation for programmers. The following is an example of JavaDoc:

/**

* @param args the command line arguments

*/

Single quote is used for a single character: ‘A’  single byte character

Double quote is used for a string: “A”  String object

Both of the above are literal constants. Literal constants can be any intrinsic data type

Chapter 2

Algorithm (page 28): Describes how a problem is solved in terms of the actions to be executed, and it specifies the order in which the actions should be executed. Algorithms help the programmer plan a program before writing it in a programming language. An algorithm is as follows:

1) Read the radius.

2) Compute the area using the following formula: area = radius X radius X pi

3) Display the area

Data Structures (page 28): These involve data representation and manipulation. Java provides data types for representing:

  • Integers
  • Floating-point numbers (numbers with a decimal point)
  • Characters
  • Boolean

These data types are known as primitive data types.