What We Will Cover
Lab Exercise 1.1
Instructions
Use the next 5 minutes to complete the following.
- Join a group of 3-4 people and prepare an answer to one of the following questions.
- Select one member from your group as a representative to describe your answer to the class.
Exercise Questions
- How do you plan to balance the work required by this course with the other activities in your life?
- What can you hope to learn by the time you complete this course?
- How can someone locate information in the course web pages?
^ top
1.2: Succeeding in This Course
Objectives
At the end of the lesson the student will be able to:
- Describe how to study effectively for this course
- Use computers in the CTC for homework
- Install software used in this course at home
|
^ top
1.2.1: Elements of Success
- Main purpose of grading is to motivate you to learn the course material
- Secondary purpose is to evaluate how well you completed the course work
- Not the same as how well you know the material
- Also must complete the course work on time
- Can look at the grading policies to analyze what must be done to succeed
Homework Assignments
- Homework designed to have you apply the lecture concepts in more detail
- Also designed to have you explore ideas in more depth
- If you get stuck:
- Read the textbook
- Contact the instructor
- Submit homework to WebCT at least the day before it is due
- Even if you are not done, turn it in
- Get credit for what you did complete
- Make sure to state an accurate status in the README.txt file
- Note that homework deadlines are real -- late homework is not accepted
- Most homework has extra credit possibilities
Exams, Tests and Quizzes
^ top
1.2.2: Using the CTC for Homework Assignments
- First you will need a login and password
- You can use either Linux, Macintosh or Windows computers for your assignments
- For more information, follow the link to the CTC homepage
^ top
1.2.3: Can I Run This On My Own Computer?
- All the technology we use runs on most any computer
- Instructions for installing Java available from the HowTo's page
^ top
1.2.4: Summary
- Four elements to success in this course
- Do your homework assignments and turn them in on time
- Prepare for the homework quizzes and complete them on time
- Prepare for the midterm and complete it on time
- Prepare for and attend the final exam
- Can complete homework assignments in the CTC
- Can also install the necessary software at home or work
^ top
Exercise 1.2
Take one minute to write one quiz-question for the information we just covered.
^ top
1.3: Introduction to Java
Objectives
At the end of the lesson the student will be able to:
- Describe how Java compares with C++
- Name and describe three types of programs you can create with Java
- Explain how a virtual machine helps Java achieve platform independence
|
^ top
1.3.1: History of Java
- 1990 - "Green" project started at Sun
- Java originally called Oak
- Developed for digitally controlled consumer devices and computers
- 1991 - Java created by James Gosling, Sun Microsystems, Inc.
- 1992 - "Green" project finished
- 1992 - 1994 - Tried to turn "Green" technology into products
- Failed
- But then the Internet happened...
- 1996 - Official launch of JDK 1.0
- Primary focus was Applets
- 1997 - Sun releases JDK 1.1
- 1998 - Sun releases SDK 1.2
- Now called the Java Platform
- 2000 - Sun releases SDK 1.3
- 2002 - Sun releases SDK 1.4
- Today a general-purpose programming language used extensively
More information
^ top
1.3.2: Applications, Applets and Servlets
- What is java used for today?
Applications
- Applications are programs that perform specific functions for users
- Wordprocessors
- Spreadsheets
- Drawing programs
- One example: jEdit
- Text editor written in Java
- Most modern applications use a Graphical User Interface (GUI)
- Java provides a complete set of APIs for programming GUIs
- Will use GUIs in second half of the course
- Example of GUI application program you will develop in this course:

Applets
- Applet: a Java program that runs in a browser
- Original impetus for Java was applets
- Two famous applets captured the attention of Netscape
Web-Based Applications
- Web applications are programs that perform specific tasks for the Web
- Java is the dominant programming language for developing these applications
- Java provides special server-side application known as a servlet
- Examples of Web-base applications:
- We will write a web browser and web application toward the end of the course
More Information
^ top
1.3.3: Java Compared to C++
- Syntax intentionally similar to C++
- Easy for C++ programmers to move to Java
- Has several important advances over C++
- All programs are encapsulated in classes
- "Pure" object-oriented language
- OOP improves programmer productivity (>2X)
- Automatic memory management ("garbage" collection)
- Improves memory management and reliability
- More intelligent use of pointers (references)
- No pointer arithmetic
- Simpler model
- Less error prone and more reliable
- Build-in bounds checking of arrays
- Helps prevent buffer overflow
- Buffer overflows are one of the leading causes of computer vulnerabilities
- Built-in support for multithreading
- Large class-libraries
- Known as APIs (Application Programming Interfaces)
- Runs without recompiling on many operating systems and hardware platforms
- Fastest Java runs slightly slower than fastest C++
- Originally about 10x slower, now about 2x with "Hotspot" compiler
- Compilers exist to translate directly to native machine code (GCJ)
- Lose portability when compiled
- Note that Java is usually faster than MS Visual C++
^ top
1.3.4: How Java Compiles and Interprets Code
|
Three steps to creating a Java program:
- Editing
- Write program source code using any text editor
- Save source code on file system or network
- Source code files have
.java suffix
- Compilation
- Use a compiler to translate the source code to bytecodes
- Files with translated bytecodes have
.class suffix
- Execution
- Use
java interpreter program to run the bytecodes
- Class Loader: moves bytecodes from file system or network into memory
- Bytecode Verifier: tests bytecodes to establish safety and security
- Interpreter (JVM): translates bytecodes into machine language
Java Virtual Machine (JVM) makes "write once, run anywhere" possible
|

|
More Information
^ top
1.3.5: Summary
- Java used to develop many types of programs including:
- Applications: programs that perform specific functions for users
- Applets: a Java program that runs in a browser
- Servlet: server-side application
- Java often compared to C++
- Similar syntax
- Automatic memory management (not in C++)
- Better model for using pointers (references)
- Built-in support for multithreading
- Runs slightly slower than C++
- Java programs require three steps:
- Created with an editor (or IDE)
- Compiled to bytecode using a compiler
- Executed using a JVM
^ top
Exercise 1.3
Take one minute to write one quiz-question for the information we just covered.
^ top
1.4: Editing, Compiling and Running Java Programs
Objectives
At the end of the lesson the student will be able to:
- Compile and run Java programs, given the source code
- Display program output to the command line
|
- Developing Java code consists of three steps
- Writing the source code
- Compiling the source code to bytecode
- Executing (running) the program
- This process is repeated many times during development
^ top
1.4.1: Writing, Editing and Saving Source Code
- Can use most any text editor to write and save Java source code
- Windows: Notepad, WordPad, Textpad
- Linux can use VIM, gedit, pico, ...
- Mac: SimpleCap, ...
- Must make sure to save the file using the four-letter extension:
java
About Using IDEs
- Many Integrated Development Environments (IDEs) available for Java
- Typically provide visual tools for designing forms and debugging code
- I do not recommend using an IDE while learning Java
- IDE will generate code for you, which does not help you learn
- IDEs are complex tools that themselves are difficult to learn
- Can distract you from your goal of learning Java
- I do recommend using IDEs once you have a good understanding of Java
- Easier to develop user interfaces
- Improves programmer productivity
- Any program you submit must run from the command line
Sample Program: Hello World
- Example program we can save as a source code file
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
} // end method main
} // end class HelloWorld
Copy this into a text editor so we can save it
Next step is to compile and run the program
^ top
1.4.2: Compiling and Running Programs
Compiling Source Code
- Open a window with a command prompt
- Change directory (
cd) to where program source code is stored
- Type:
javac HelloWorld.java
- If no errors, creates
HelloWorld.class
Running a Program
- Type:
java HelloWorld
- Interpreter loads
.class file for HelloWorld
- Note that
.class suffix is not typed
- Interpreter calls the
main method by default
Using Text Editors
- Many text editors have provision for compiling within the editor
- Textpad: Tools menu
- jEdit: Press the java and javac tabs
More Information
^ top
1.4.3: Summary
- You can use most any text editor for writing and editing source code
- To compile an application, use the
javac command
- To run an application, use the
java command
- Many text editors provide features to make it easier to compile and run programs
- Once you have mastered the basics of Java, you should use an IDE
- While you are learning, it is better to use a text editor
^ top
Exercise 1.4
Take one minute to write one quiz-question for the information we just covered.
^ top
1.5: Language Building Blocks
Objectives
At the end of the lesson the student will be able to:
- Use comments and whitespace in programs
- Identify Java keywords
- Make up valid identifiers
- Use literals in programs
|
- Like all programming languages, Java has grammar rules
- Rules specify how a correct syntactic structure can be formed from the words and numbers that make up a program
- Grammar also has a semantic definition specifying the meaning of syntactically correct code
^ top
1.5.1: Comments, Statements and Whitespace
Comments
- Comments are ... comments -- notes to people reading the code
- Comments ignored by compiler
- Use comments to document and describe code
- Comments can start with
// and last to end of the line
// this is a comment
Comments can span multiple lines: /* ... */
/* This is a multi-line comment
which can be split
over many lines or a portion of one line. */
Javadoc Comments
- Javadoc utility uses these types of comments to automatically create program documentation
- Javadoc comment is made up of two parts with a blank line (containing a single asterisk "*") between sections
- Description of the code
- Followed by zero or more tags
/**
* Description part of a Javadoc comment
*
* @tag Comment for the tag
*/
Starts with the begin-comment symbol: /**
Last line begins with the end-comment symbol */
Notice that the asterisks line up (by convention)
Use Javadoc comments before every class and function of your programs
More information: How to Write Doc Comments for the JavadocTM Tool
For a list of tags: Tag Conventions
Important in your Java assignments: Document Java Assignments
Statements
- Statements direct the operation of the program
- Most statements end in a semicolon (
;)
- However, statements within a set of braces
{} end with a right brace
Whitespace
- Whitespace: blank lines, spaces, and tabs
- Whitespace ignored by compiler
- Use whitespace to make programs more readable
^ top
1.5.2: Identifiers and Keywords
Identifier - the name of something (e.g. variable, object, method or file) used in a program.
- Purpose of an identifier is to provide a name for something
- Should always try to make the name meaningful
- Programmers create identifiers for the names of many things in a program:
- classes
- variables
- methods
- Identifiers are made up of a sequence of characters
- Each character can be a:
- Letter
- Digit
- Connecting punctuation (e.g. underscore _)
- Currency symbol (e.g. $, ¢, £, ¥)
- Can not start with a digit
- Can not contain any periods or spaces
- Can not be a keyword (e.g. "
if, "for", etc.: see Java Language Keywords)
- Are cAsE sEnSiTiVe!
id, ID, iD and Id are all valid but different identifiers
- Identifiers have naming conventions:
- Use meaningful names
- Use only letters and digits
- Start class names with an uppercase letter
- Start variable and method names with a lowercase letter
- Capitalize interior words in multi-word names (e.g.
myAge)
Keywords
- Keywords are reserved words with predefined meanings
- We used keywords in our HelloWorld program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
} // end method main
} // end class HelloWorld
All keywords have special meaning in Java
- Can only be used for their specified purpose
- Attempting to use for any other purpose will generate a compiler error
- Thus cannot use keywords for identifiers
For a current list, see Java Language Keywords
^ top
1.5.3: Literals
- Strings of characters that represent "literal" data values
- Treated as a constant (i.e. unchanging) value in a program
- Examples of literals:
- Integer:
int x = 123;
- Floating-point:
double x = 1.23;
- Character:
char x = '1';
- Boolean:
boolean x = true;
- String:
String x = "123";
^ top
1.5.4: Classes and main Methods
- Return to our example program we saved as a source code file
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
} // end method main
} // end class HelloWorld
Declaring a class
- When you develop a Java program, you code one or more classes for it
- Each class starts with a declaration like the following:
public class HelloWorld {
// class definition goes here
}
Keyword public means all parts of the program can use this class
Keyword class tells compiler that a class is being defined
Left brace { begins body of every class
Likewise, right brace } ends body of every class
Any code between the curly braces is called the class definition
Declaring a main Method
- Every Java program has one or more methods
- Methods are blocks of code that perform a task
- Similar to functions in other programming languages
public static void main(String[] args) {
Every Java application must have a method called main
Applications begin executing at main method
Keyword public allows any program to use the main method in this class
void means main has no return value
args is a parameter of type String[] -- cover later
For now, mimic the first line of main
Left brace { begins body of every method
Right brace } ends body of every method
Using Methods and Variables Inside Classes and Objects
System.out.println("Hello, world!");
System.out refers to the out variable in the System class
Can refer to variables and methods inside a class
Join the class name and name of the method or variable separated by a period (".")
Similarly, out.println() refers to the println method of the out object
For objects, join the object name with the method or variable separated by a period (".")
Will cover this in more detail later in the course
^ top
1.5.5: Summary
- Comments help document what a program does
- Useful for humans to read
- Java has three styles of comments
- Statements direct what a program does
- Use whitespace to make programs more readable
- Identifiers are any name you create in a Java program
- Keyword is a special word reserved by Java
- Literals are strings of characters the represent constant data values
- You develop Java programs writing one or more classes
- Every Java application starts with a
main method
^ top
Exercise 1.5
Take one minute to write one quiz question for the information we just covered.
^ top
Wrap Up
^ top
Home
| WebCT
| Announcements
| Schedule
| Expectations
| Syllabus
Help
| FAQ's
| HowTo's
| Links
Last Updated: September 10 2003 @16:43:41
|