Title: ITM 172
1ITM 172
- The Java Programming Language
- Chapter 1 Part A
- Introduction to Java and the Development Tools
2Java Why it is a popular programming language
- Main reason Programs written in Java can run on
any type of computer. - A computers processor has an instruction set. (a
list of all the instructions it understands). - A Pentium IV processors instruction set is not
identical to DECs Alpha processors instruction
set or Suns Sparc processors instruction set. - Therefore, if you write a program and then
compile it so that it runs on a Pentium
processor, it might not run on an Alpha or Sparc
processor (because the program contains
instructions that are not in that processors
instruction set). - When Java programs are compiled, they can run on
any type of processor (Therefore, Java programs
have high portability and are architecturally-neut
ral).
3The portability of Java programs
- The way that Java programs must be compiled into
bytecode and then interpreted by the JVM is what
makes Java programs completely portable. - Portable means that a Java application can run on
any hardware/operating system combination. - Since Java programs are not translated into a
specific processors machine language until
runtime, the program can wait until it sees
what platform it has landed on before it
finishes compiling. Then it can adapt itself by
compiling into any specific processors machine
language at runtime.
4Other advantages of Java
- Simple. Its easier to learn than C
- Object-oriented. Programming code is organized
into modules/building blocks/objects. Huge
libraries of these building blocks already
exist. Programs can be built using standard
building blocks. - Distributed. Java programs can interface
(communicate with) other programs running on
other computers using standardized communication
methods. - Multi-threaded. The Java language allows the
programmer to write programs that can spawn
processes simultaneously.
5Other advantages of Java
- Dynamic. Java is an extensible language. That
means, new functionality can be added
indefinitely. - Robust. Java programs dont crash as readily as
programs written in other languages because the
compiler does some forward thinking error
detection and correction. - Secure. Viruses cannot embed themselves in Java
programs.
6Java Application Development
1
4
2
3
Write Source Code in Java
5
7
6
8
java.exe is the VIRTUAL MACHINE
7The Java platform
- In order for an application program to run on a
computer, certain resources must be available to
it. - Resources consists of hardware and software.
Collectively, these resources are called the
hosting platform. - The next slides will explain what a platform is,
and then what platform Java applications require.
8What is a computer platform?
- In computing, a platform describes all the
hardware and software programs that must be
available (as resources) in order for an
application to run. - Example A personal computer (complete with a CPU
and a RAM memory) running the Windows Vista
operating system. - Example A high-end computer (multiple
processors, many GBs of RAM) running the Unix
operating system.
9What does the platform provide?
- The application program cannot carry out all
computing tasks on its own without help from
other programs (we call services) that are
included in the operating system software package
and the hardware. - For example, Microsoft Word cannot carry out all
the word processing tasks (such as printing,
saving and opening files, etc.) without the help
of the operating systems helper programs (some
may be in .dll files) and the hardware (CPU, RAM,
disk drive, printer).
10The Java Platform
- A Java application cannot execute unless it is
running on a computer hosting the Java platform. - The Java platform consists of these components
that must be available at runtime the hardware,
the operating system, and the JRE (the Java
runtime environment). - Non-Java applications only need two components in
their platform the hardware and the operating
system.
11The Java Platform
- So, to run a Java application, there must be
- Hardware (a CPU, RAM, etc.)
- an operating system
- The JRE
- a. the JVM (the byte code interpreter and the
program launcher) - b. the class library (helper modules similar to
.dll files that augment the functionality of the
operating system).
12JRE Part A The JVM (Java Virtual Machine)
- The JVM consists of two things it uses its
interpreter to translate the byte code into
machine language and it uses its launcher to
initiate the running of the application. - The JVMs interpreter is the program that
completes the translation process by translating
the byte code into a specific processors machine
language.
13JRE Part BThe class library
- The other component of the JRE is the class
library (aka API - Application Programming
Interface) - The API is a large collection of reusable
modules of code. Many of the modules in this
library are used like .dll files providing
helper programs that augment the functionality
of the operating system.
14Java Development Tools
- We just finished discussing what resources are
needed to run a Java application. - Now we will discuss what resources are available
to develop a Java application. - These resources are called tools
- The JDK (Java Development Kit)
- An IDE (NetBeans)
15The Java Development Toolkit (JDK)
- To create, compile, and run a Java program, you
need a program called the Java Development
Toolkit (JDK). The toolkit includes - javac The compiler, which converts source code
into Java bytecode - jar The archiver, which packages related class
libraries into a single file (similar to a zip
program). - javadoc The documentation generator, which
automatically generates documentation from source
code comments - jdb The debugger
- JRE (for testing purposes)
16The Integrated Development Environment (IDE)
- The IDE is like the dashboard of your car it
makes several programming tools accessible from
one integrated, GUI-style screen. - These tools include the text editor, compiler,
run-time module, debugger, and help system. The
IDE also forces you to organize your files into
projects and packages. - The name of our IDE is NetBeans. (Other products
that are IDEs include Forte by Sun, Jbuilder by
Borland, Visual J by Microsoft, Visual Café by
WebGain).
17If you want to download and install the JDK and
the IDE (Netbeans) on your home computer, go to
www.java.sun.com and select this link.
18Download this version. On the next link, select
the Windows platform (not Linux or Solaris).
19Getting started (Lab 1)
- The JDK version J2SE 6.3 (containing the JRE) and
Netbeans 6.0 have already been installed on your
PC.
20Lab 1.1- Part A - Class Welcome in Package
Chapter1
- This program displays the message Welcome to
Java on the screen (in the standard output
window not in a Windows window).
21Start the IDE (NetBeans) From the Windows
desktop, click Start, Programs, NetBeans
6.3, NetBeans IDE
22See the NetBeans window.
23If you use NetBeans, all programs must be
encapsulated in a project. A project folder is
created and default settings are applied to the
project folder. To create a project, from the
menu, click File, New Project
24 chose General for the project category and
Java Application for the type of project. Click
Next
25Type the Project Name Welcome Type the Project
Location C\251lastnameLABS Unselect Create
Main Class. Click Finish.
26See the project name appear in the Projects
panel. At least two folders are created below the
projects folder. Click the to expand the
Welcome project folder, and then click the
to expand the Source Packages folder. Just as a
project is a container for an application, a
package is a container (also a folder) for a
collection of similar files within a project.
27All Java code must be encapsulated in class
files. Therefore, the next step is to create a
class/file. From the menu, select File, New
File
28in the New File window, click Java Classes
as the Category and Java Main Class as the file
type. Click Next
29welcome
type the class name Welcome (same as the
project name). Type the package name welcome.
Click Finish.
30The class structure
The main method structure
A file is created that contains some startup
code. It opens in the text editor window. A class
structure is created (with the class name
Welcome) and a method is created (with the name
main).
31Type your name and the lab number in the comment
area in the top. Delete everything except what
you see in this screen shot.
32To create our first Java program, that displays
the words, Welcome to Java, type in method
main System.out.println(Welcome to Java)
33To compile the program, from the menu, select
Build, Compile Welcome.java or press F9.
34The output window shows if you have any syntax
errors. If not, you will see the message BUILD
SUCCESSFUL.
35To run the program, from the menu select Run,
Run other, Run Welcome.java or press Shift-F6.
36When the program runs, you will see the message
Welcome to Java appear in the output window.
37Minimize the NetBeans application window to view
the Windows desktop. Click on Folders view on
the toolbar. Navigate to the source file that you
created. It is under Local Disk C,
251lastnameLABS, Welcome, build, classes,
welcome. See one file Welcome.class
38Restore the NetBeans window so you can exit the
program.
39Anatomy of a Java Program
- Programming statements are grouped into methods.
- Methods and variables are grouped into classes.
- Each class is stored in its own unique file with
the same name as the class. - Files are grouped into packages
- Packages are grouped into projects.
40System.out.println
- To display a line of text on a computer screen,
we invoked the pre-existing method println( ). - When you want your program to display a string of
text in the standard output window, invoke the
println method and pass it the string you want
displayed (Welcome to Java). - The println( ) method is method belonging to the
outstream class, and out is an instance of an
outstream. out is declared in the System class. - The System class is in the java.lang package,
which is in the API (the collection of class
files included in the JRE). Recall that the JRE
contains a collection of helper modules that
can be called upon to perform certain processes
on behalf of your program.
41Comments
- Lines that begin with // or multiple lines
enclosed in / . / - Example
- // This is a comment.
- / This is also a comment. /
- Comments are ignored by the compiler.
42Reserved Words
- Reserved or key words have special meaning to the
compiler and cannot be used for other purposes in
a program - Example words like class, package, public, main,
static, void.
43Modifiers
- Modifiers are key words that specify properties
of data, methods, and classes. For example, the
key word public specifies whether other methods
may invoke the methods and/or access the data
(variables that are) declared in this class.
44Blocks
- A pair of curly braces form a block. There
are class blocks, method blocks, and if blocks,
while blocks, etc.
45The main( ) method
- All executable classes must contain a method
named main( ). - Main( )will be the first method invoked when a
class (or an instance/object of that class) is
loaded into memory. - Because main( ) is method, it must have a header
and a body. Its header looks like this - public static void main(String args)
- Later, this will make more sense to you.
46Lab 1.1 Part B modify class Welcome in
Package Chapter1
- In this program, we simply modify the way we
display the output. - The output of this program is a text string
Welcome to Java. - Instead of displaying the string in the standard
output window, well display it in a
messageDialog box.
47Using GUI objects in a Java Program
- If you dont specify otherwise, the output
(Welcome to Java) of a Java program (such as
Welcome.java) will display in the standard
output window. - Modern day programs generally display their
output in a Windows window called a GUI (Graphic
User Interface) component. An example of a GUI
component that might be used to display output is
a message dialog box. - We will modify Welcome.java so that its output
displays in a message dialog box instead of the
standard output window.
48Start the IDE (NetBeans) From the Windows
desktop, click Start, Programs, NetBeans
6.0, NetBeans IDE
49welcome
The last file that we edited should display in
the editing window. (If not, click File, Open
Project.
50Edit the file Welcome.java as indicated.
51To compile the program, from the menu, select
Build, Compile Welcome.java or press F9.
52The output window shows if you have any syntax
errors. If not, you will see the message BUILD
SUCCESSFUL.
53To run the program, from the menu select Run,
Run other, Run Welcome.java or press Shift-F6.
54When the program runs, you will see the message
Welcome to Java appear in the Message Dialog
window.
55The window title is Example 1.2 output
The text string is Welcome to Java
The INFORMATION MESSAGE icon.
Click the ok button to dismiss the dialog box.
56JOptionPane.showMessageDialog( )
- When method showMessagedialog( ) is invoked, it
displays a dialog box on the screen. (A dialog
box is one type of GUI component). - showMessageDialog( ) is a method of static class
JOptionPane, therefore, to invoke this method, we
use the format classname.methodname - JOptionPane.showMessageDialog()
- Method showMessageDialog( ) requires 4 arguments
- Null means none. (This argument wont be
explained now.) - Text to be displayed Welcome to Java
- Title of box Example 1.2 output
- Type of icon to display in the box
INFORMATION.MESSAGE
57Import javax.swing.JOptionPane
- If you want your program to invoke an external
method (method showMessageDialog( )), the class
that the method belongs to (class JOptionPane)
must be included (imported) into your program. - Import statements must include the package
name. Class JOptionPane is in package
javax.swing (like most of the other GUI component
classes). - Where class System (in the Java.lang package) is
automatically imported into every Java program,
class JOptionPane is not implicitly imported it
must be explicitly imported.
58System.exit(0)
- exit( ) is another method that belongs to class
System. - Since System is a static class, we use the format
classname.methodname to invoke method exit( ). - Method System.exit( ) terminates the program. You
should explicitly terminate a program so that all
threads spawned by your program will be
terminated. - Method System.exit( ) take a single integer
argument (0). The zero is used as a signal to
indicate to the operating system that the program
terminated normally.