Upload
vuongphuc
View
215
Download
1
Embed Size (px)
Citation preview
CS5015 Object-oriented Software Development
Lecture 2: Intro to Java Programming Environment
A.P. O’Riordan, revised 2013
slide 17 from Deitel textbook
2
Software Development
• Software development is the translation of a set of requirements into
software
– this can include the specification of requirements, and the design,
construction and testing of the software
• Software development is part of the larger field of software
engineering
• CS5015 will focus on the development of small software programs using the Java programming language
• Development usually follows a process or lifecycle consisting of program design, coding and testing
– in simple terms a cycle of design-code-test iterations
3
Java Programming Environment
• Java source code files (with file ending .java) are compiled into an
intermediate format (.class files) know as bytecode
– multiple .class files can be packaged together into a .jar (Java archive)
– the bytecode is a standardized portable binary format
• Bytecode instructions run on a JVM (Java Virtual Machine) such as
Oracle’s Hotspot
• The Java Virtual Machine (JVM) runtime executes .class or .jar files
by emulating the JVM by interpreting the bytecode or using a just-in-
time (JIT) compiler
• A set of dynamically loadable libraries is usually used also at
runtime
5
JDK
Programmers can create Java applications using simple tools such as a editor and command-line tools such as provided in Oracle’s JDK (Java SE Development Kit)
Basic tools include:
– javac: compiler for the Java programming language
– java: loader for Java applications
– javadoc: documentation generator
– appletviewer: run and debug applets
– javaws: Java Web Start launcher
– jar: create and manage Java Archive (JAR) files
– jdb: Java Debugger
JDK has more tools for e.g. security, remote execution, monitoring
Java Platforms
• Java platform has various editions
– Java SE (Standard Edition), Java EE (Enterprise Edition), Java
ME (Micro Edition), Java Card (for smart card applications)
– In addition Android uses the Java language with some changes to the
libraries and different virtual machine
– Java SE is the standard edition for general-purpose use on desktop
clients and some server applications
– we will use Java SE exclusively
– current version is Java SE 7
• Both the Java Runtime Environment (JRE) and the Java
Development Kit (JDK) can be downloaded from the Oracle Website
– http://www.oracle.com/technetwork/java/javase/downloads/index.html
6
7
Using JDK
1. Write your Java code in source files, usually one class per file.
2. Compile the code with javac.
3. Fix the compile errors and recompile.
4. When there are no more compile errors run the program wit the
java command.
Example:
– Create a class Customer
– Save in a file called Customer.java
– Compile like so:
prompt>javac Customer.java
– And run like so:
prompt>java Customer
8
IDEs
• Integrated Development Environments (IDEs) are software applications that provide comprehensive support for software development – this can speed up the whole development process, facilitate sharing,
and reuse errors
• Most Java IDEs contain a programmer editor, a compiler, loader, and a debugger – many now contain more supports including build automation, class
browser/object browser, code completion, version control, a user interface builder
• An IDE can work with different programming languages (multiple-language IDEs) or be language-specific
Java IDEs
• Popular open-source Java IDEs include: – NetBeans (Oracle)
– Eclipse (Eclipse Foundation)
• Proprietary: – Xcode (Apple) freeware
– JBuilder (Embarcadero – formerly CodeGear)
– JDeveloper (Oracle)
– JCreator (Xinox)
– IntelliJ IDEA (JetBrains)
• Teaching and learning IDEs: – BlueJ bluej.org
– DrJava drjava.org
– jGRASP www.jgrasp.org
– Greenfoot www.greenfoot.org/
9
10
DrJava IDE
• DrJava (drjava.org) is a lightweight programming environment for Java designed specifically for beginners
– free open-source software
– cross-platform; it’s itself written in Java
• Features include: – DrJava supports the use of different Java compilers, such as the
Oracle javac compiler supplied with the JDK
– DrJava has an interactions pane, where you can input Java expressions and statements and immediately see their results
– DrJava has a programming editor, where you can enter and edit class definitions (code) with support for brace matching, syntax highlighting, line numbers, find-and-replace and automatic indenting
– DrJava has integrated support for compiling, running, debugging, and documenting and a project facility for managing files
12
Eclipse
• Eclipse (www.eclipse.org/) is a popular open source multi-language IDE
– maintained by The Eclipse Foundation a not-for-profit organization
– Eclipse was originally developed by IBM (who are still involved)
– cross-platform (Linux, Windows, Solaris, MacOS)
– current version is called Kepler (4.3)
• Eclipse has a plug-in architecture allowing Eclipse to be easily extended
– additional tools are added as plug-ins
– uses Eclipse Java development tools (JDT) including compiler
– project resources are organized in terms of workspaces
APIs and Packages
• Java Application Programming Interfaces (APIs) are the specifications of the Java library code
• The classes in the Java APIs are organized in separate groupings called packages – each package contains a set of related interfaces, classes and
exceptions
• Java Standard Edition or Java SE is a collection of Java Application Programming Interfaces (APIs) useful to any Java platform programs
• APIs and packages don’t necessarily coincide; an API may be spread across multiple packages
14
15
Java APIs
http://download.oracle.com/javase/7/docs/api/
Hello World Program
• A program consists of source code files with a set of instructions and comments defining a class, e.g. HelloWorld.java
• A program consists of one or more classes in .java files together with resources such as images files
– a class called ImAClass should be stored in a file called ImAClass.java
– by convention a class name should start with a capital letter (but this is not a rule of the language)
– Java is case sensitive
/**
* simply displays "Hello World!" to standard output.
*/
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
16
Comments
Instructions
Methods
• Each class consists of methods that perform tasks
• In the HelloWorld example the only method is main()
• The main() method is the first to be called (invoked) in a Java
application
• HelloWorld consists of one statement System.out.println("Hello World!");
– statements end in a semicolon; it is a syntax error to omit this
• Use blank lines and space characters to enhance program
readability
– note the indentation in the sample in the previous slide
17
Syntax
• The syntax of a programming language specifies the rules for
creating a proper program in that language
• A syntax error occurs when the compiler encounters code that
violates Java’s language rules
• Syntax errors are also called compiler errors, compile-time errors or
compilation errors, because the compiler detects them during the
compilation phase
• The semantics of the language is how the instructions are
interpreted, in this case by the Java virtual machine
18