1
CS 209 Programming in Java
#1
Introduction
Spring, 2006
Instructor: J.G. Neal
2Topics
• CS 209 Target Audience
• CS 209 Course Goals
• CS 209 Syllabus - See handout
• Java Features, History, Environment
• Java Development Software
• Information Sources
• Applications and Applets
3
CS 209 Programming in Java
• Target audience:
– Designed for students with previous programming experience
– Ideally the student should have some experience with: • Arrays,
• Functions/procedures,
• Passing parameters to functions/procedures
CS 209 - Course Goals
• Goals:
– Learn the Java programming language
– Learn about the Java Virtual Machine and Java API – Learn to design, develop, debug, and run Java programs
• Web-based Java applets • Stand-alone Java applications – Learn to use Java development tools – Learn to use good principles and practices
5
Features of Java
• Provides for both web-based and stand-alone applications • Is totally object-oriented
• Provides networking capabilities for distributed systems • Provides for dynamic interactive programs
• Simplifies memory management - automatic garbage collection • Supports multithreading - concurrent processing
• Is portable and platform independent; interpreted
– Programs run on the Java Virtual Machine at runtime • Provides for security
• Very large set of library packages in the Java API
– Graphics, user interface capabilities, networking, database operations, file manipulations, etc.
6
Some Java History
• Developed in the early 1990’s at Sun Microsystems
– Originally called “Oak”
– Designed for use in embedded consumer electronic applications
• Redesigned for developing Internet applications and renamed “Java”
• Releases:
– JDK 1.0, the first Java programming language release, was officially announced in May 1995
– JDK 1.1 released in 1997
– Java 2 SDK v 1.2 (a.k.a. JDK 1.2), 1998 – Java 2 SDK v 1.3 (a.k.a. JDK 1.3), 2000 – Java 2 SDK v 1.4 (a.k.a. JDK 1.4), 2002 – Java 2 SDK v 1.5 (a.k.a. JDK 1.5), 2004
7
JDK Editions
• Java Standard Edition (J2SE) **
– J2SE can be used to develop client-side standalone applications or applets.
• Java Enterprise Edition (J2EE)
– J2EE can be used to develop server-side applications such as Java servlets and Java ServerPages.
• Java Micro Edition (J2ME).
– J2ME can be used to develop applications for mobile devices such as cell phones.
** This course uses the J2SE
Java Program (MyProgram.java) Java Compiler (javac) Java Bytecode (MyProgram.class) HW-based Platform (Sun + Solaris OS) Java API
Interpreter or Java Platform:
Java Virtual Machine
HW-based Platform (PC + WIN OS) Java API
Interpreter or Java Platform:
Java Virtual Machine
HW-based Platform (Mac + Mac OS) Java API
Interpreter or Java Platform:
Java Virtual Machine
The Java
Environment
Editor(Source Code File)
9
Java Development Software
Sun Microsystems, Inc.
• Java 2 Standard Edition (J2SE) SDK from Sun Microsystems
– No cost download from the Sun Microsystems website
• J2SE includes:
• javac - Compiler for the Java language
• java - Launcher for execution of Java applications
• javadoc - API documentation generator
• appletviewer - Run applets without a web browser
• jar - Manage Java Archive (JAR) files
• jdb - Java Debugger
• javah - C header and stub generator
• javap - Class file disassembler
• extcheck - Utility to detect Jar conflicts
• HtmlConverter - Converts to a new HTML file with tags for invoking the Java plugin
• among others . . .
10
Java Development Software
Integrated Development Environments (IDEs):
• NetBeans IDE (http://www.netbeans.org; http://www.sun.com)
• Eclipse IDE (http://www.eclipse.org)
• Borland JBuilder (http://www.borland.com)
• Sun Java Studio (http://www.sun.com)
• IBM WebSphere (http://www.ibm.com)
• JCreator (http://www.jcreator.com)
11
Object-Oriented Programming
Benefits:
– Software modularity – Information hiding
– A wealth of well-tested predefined classes, especially the Java API (part of the J2SE)
– Software reuse
– Enhanced programmer productivity
Object-Oriented Programming
• Fundamental principle:
– Encapsulation of data and methods in an object
• Objects:
– Real world objects have state (attributes or data) and behavior (actions or operations)
– A software object
• maintains its state (attributes or data) in variables and • implements its behavior as methods
• A class:
– Is a “blueprint” or “template”
– Defines the variables and methods common to all objects of a certain kind
• An object is an instance of a class
13
Object-Oriented Fundamentals
• Programs are developed in the form of classes:
– The conceptual entities/objects that comprise the program
• Classes are organized into a hierarchy
– Levels:
• The higher the level, the more generic • The lower the level, the more specific
– Inheritance: subclasses automatically have the variables and methods of their superclasses
– Inherited methods can be overridden in a subclass
• Programmers write their new programs building on the existing Java Class Hierarchy (Java 2 API)
Bicycles
Racing Bikes
Mountain Bikes Tandem Bikes
14
Example Application-Specific Hierarchy
Example Application-Specific Hierarchy
Employee
SalariedEmployee HourlyEmployee TemporaryEmployee
Tier1 Tier2 Tier3 Union NonUnion Category1 Category2 Level 1:
Level 2:
15
Type of Java Programs
• Application - two types
– Console-based program: Input and output are typically from/to the command prompt window
– Window-based program: User interaction (input and output) is performed by way of a graphical user interface (GUI)
• Applet
– Runs within a web browser (e.g., Microsoft Internet Explorer, Netscape, Mozilla, Opera)
– Requires an HTML page containing an Applet tag or Object tag that holds the information needed to invoke the applet – To view an applet use:
• Web browser or • JDK appletviewer