Title: Java Background v1.1
1Java Backgroundv1.1
2topics
- simplest Java program
- Java platform overview
- getting the software you need
- why did Sun create Java?
- Files (.java, .class, .jar)
31 the simplest Java program
4simplest program in C and Java
include int main()
printf(Hello in C\n") return 0
- public class Hello
-
- public static void main(String args)
-
- System.out.println("Hello in Java")
-
-
Like C, Java is case-sensitive, and statements
end with You need a console to see the
results. Java file name must be Hello.java.
5simplest program in Java using Window from
library
- import java.awt.
- import javax.swing.
- public class Hello
-
- public static void main(String args)
-
- JFrame myWindow new JFrame()
- myWindow.setSize(new Dimension(400,275))
- myWindow.setTitle("Hello in Java")
- myWindow.setVisible(true)
-
Uses the JFrame class in the library to create a
Window. Java file name must be Hello.java.
6simplest program in Java extending Window from
library
- import java.awt.
- import javax.swing.
- public class Hello extends JFrame
-
- public static void main(String args)
-
- new Hello().run()
-
-
- private void run()
-
- this.setSize(new Dimension(400,275))
- this.setTitle("Hello in Java")
- this.setVisible(true)
-
Extends the JFrame class in the library to create
a Window.
72 Java platform overview
8Java is a platform
- Java programs require a Java Virtual Machine
(JVM) to run - the JVM is available for Linux, Windows, Mac and
Solaris - commonly also called the Java Runtime Environment
(JRE) - free download from Sun
- the JRE is pre-installed in Mac OS X
- Windows users have to download and install it
themselves - NOTE the JRE does not include the Java compiler
- developers need the JDK instead (or in addition
to) - the Java compiler is in the Java Development Kit
(JDK) - installing the JDK gives you the compiler and the
JRE - the JDK does not include an integrated
development environment (IDE) - you can one of several free IDEs for Java
- Eclipse (free from IBM)
- Netbeans (free from Sun)
- BlueJ (free from Monash University)
9Just in Time (JIT) compiler in Java runtime (JVM)
compilation
before installation, or the first time each
method is called
10the JDK includes lots of libraries
- Java programs can call a huge body of pre-written
code - these reusable components are called the Class
Libraries - sometimes they are also called packages
- they are also called the Java APIs
- the libraries are designed to be used identically
regardless of OS
11who implements JVMs?
- Sun Microsystems
- Java HotSpot Virtual Machine for Windows, Linux
and Solaris - Hewlett-Packard
- Java runtime for HP-UX, OpenVMS, Tru64,
Reliant(Tandem) UNIX) - IBM
- Java runtime for MVS, AIX, OS/400, z/OS
- Apple Computer
- MacOS Runtime for Java (MRJ)
- the full version of J2SE, version 1.4.2, is part
of Mac OS X - includes the JDK (compilers, etc.) and the
HotSpot Virtual Machine - BEA Systems
- JRockit (for their web server)
123 getting the software you need
13versions of Java
- Java 1 - 1996
- Java 1.0 original, not a very good version
- Java 1.1 greatly revised and improved (1997)
- Java 2 - 1998
- Java 1.2 includes Swing (for graphical user
interfaces) - Java 1.3 more new packages, no new syntax (2000)
- Java 1.4 introduced the assert statement (2002)
- Java 5 - 2004
- Java 1.5 quite a bit of new syntax, including
generics - I am using JDK v5.0 Update 9 of the J2SE (or SE)
- Update 9 is the current sub-version as of
October 2006
14editions
- Java SE (Standard Edition) is for desktop
applications (a.k.a. J2SE) - Java EE (Enterprise Edition) is for web servers
(a.k.a. J2EE) - Java ME (Micro Edition) is for devices such as
pagers and mobile phones (a.k.a. J2ME).
15what languages require a JVM?
- Java used to be pretty much the only language
- there are now efforts by other languages to use
JVM - Jython (formerly known as JPython)
- except for some standard modules, Jython programs
use Java classes instead of Python modules - Jython was fairly complete
- its development stopped in 2005 when Microsoft
hired its creator to work on IronPython ( which
targets .NET instead) - JRuby
- tightly integrates Ruby with Java, with full
two-way access between the java and the Ruby
code. - this year (Sept.), Sun Microsystems hired JRuby's
two creators to work on JRuby full time - Groovy is a new dynamic language targeted at
the JVM
16the three programs you need to write Java
- The Java Runtime Environment (JRE)
- this is the virtual machine that executes Java
programs - the Java compiler and associated utilites
- a way to edit source code
- youll want an integrated development environment
(IDE) with - an editor for source code
- a debugger, to help you find your mistakes
- a viewer to see the parts of your program
- an easy way to compile and run programs
- an easy way to view documentation
- some good IDEs for Java
- BlueJ (free) ? I used this
one first - Eclipse (free from IBM) ? I use this one now
- Netbeans (free from Sun) ? well regarded I
havent used it
17what I am using
- Java Development Kit (JDK) 5.0 of the J2SE
- from http//java.sun.com/javase/downloads/
- get the JDK, not the JRE
- get the Standard Edition (not the Enterprise
Edition) - the JDK gives you both the runtime and the
compiler - its OK if your machine already has a JRE
installed - if your computer cant run Java 5, get Java
version 1.4.2 - Eclipse IDE
- from http//eclipse.org/ (current version is
3.2.1) - unzip into a folder
- run eclipse.exe
- more details at http//harbormist.com/AcmJava
- NOTE
- Java SE (Standard Edition) is for desktop
applications (a.k.a. J2SE) - Java EE (Enterprise Edition) is for web servers
(a.k.a. J2EE) - Java ME (Micro Edition) is for devices such as
pagers and mobile phones.
18the Eclipse welcome screen
go to the workbench
overview
tutorials
samples
whats new
19the Java perspective
20projects in the workspace
- In Eclipse, classes are organized into projects
(complete programs) - each project is kept in a folder
- Eclipse projects are kept in a workspace, which
is just a folder containing project folders - when you first start Eclipse, you will be asked
to choose a workspace (folder) - you can set this as default, so you use the same
workspace every time - you can tell Eclipse to ask you for the workspace
each time it starts up
21starting a new project
- choose File ? New ? Project... ? Java Project and
give your new project a name - this will also be the name of the folder that is
created in the workspace - choose File ? New ? Class and give your new class
a name - you can also check the box that asks Eclipse to
generate a main method for you - start editing!
22compiling and running
- you dont compile your program Eclipse does that
automatically as you type - it also points out errors as you go along
- the first time you run your program, choose Run ?
Run As... ? Java Application - select the default package first, then
right-click - after that, you can just click the green circle
with a triangle in it - or, you can use the drop-down list to the right
of the green circle to choose a particular
program to run
23importing code into Eclipse
- if you have a program that is outside Eclipse
- on the file system, create a folder for the
project in your Eclipse workspace folder - move all the .java source files to that folder
- choose File ? New ? Project... ? Java Project and
type in the name of your new folder - Eclipse will recognize it and ask if you want to
make the folder into a new project just click
Finish - alternative method
- In Eclipse, create an empty project with the
desired name - Choose File ? Import and follow instructions to
import .java files from the file system into
Eclipse
244 why did Sun create Java?
25C and C perceived common problems
- pointers are dangerous
- memory leaks (failing to free memory correctly)
- function pointers (jumping to the wrong place)
- data pointers (pointing to the wrong place)
- manual garbage collection is a lot of work
- multiple inheritance (C) can get very
complicated - ambiguities like the diamond problem (a.k.a.
diamond of death) - not easily portable across platforms, even with
re-compile and discipline
26a few ways Java improved on C
- instead of pointers, Java has references
- references are similar to pointers, but with
protections (cannot jump into illegal parts of
memory)avoids segmentation fault problems - automatic garbage collection
- memory is reclaimed from the heap
automaticallyavoids memory leaks - single inheritance
- ambiguity and complexity of multiple inheritance
avoided - encapsulation
- all code must be in a classintended to encourage
information hiding - array bounds checking
- libraries
- many common tasks already coded and available for
reuse by means of inheritance - many interfaces (behaviors) already coded
275 Files (.java, .class, .jar)
28What is a .class file?
- javac takes a text file as input, and creates a
binary file as output - .class files contain two main things
- bytecode
- instructions for the Java Virtual Machine
- at runtime, bytecode gets JIT-compiled into
native code on the fly - metadata
- a catalog of resources
- a description of the class contents that will be
available at runtime by means of reflection
29what is a .jar file?
- Java Archive files (.jar extension) contain
- one or more .class files
- resources
- strings
- images
- Sounds
- .jar files may also contain .java source files
(optional) - if a JRE is present on a system, .jar files
should be registered to run on the JRE - double-clicking the .jar should run the JRE with
the .jar file as input - the JVM must be in the search path
- the /lib and /bin folders for the JRE must be in
the CLASSPATH - a .jar may reference other .jars if they can be
found
30the single-file application
- monolithic application
- all source code compiled into one .JAR file
APP.jar
31component-based application
- component-based application
- .JAR file one or more other .JAR files
-
compute.jar
APP.jar
data.jar
32where do Java library .JAR files reside on the
file system?
- CLASSPATH environment variable
- user-level, not system-level, if on Windows
- programmers must tell the compiler which .JAR
files a program will need - in Eclipse, you do this by opening the project
Properties - add one or more External .JAR files to the Java
Build Path - (demo)
33jargon checklist
- what is a .jar file? .java? .class?
- what is metadata?
- What is bytecode?
- What is the JVM?
- what is the JRE?
- what is JDK?
- What are J2SE and J2EE?
- What is an IDE?
- What is a GUI?
34what we just covered
- simplest Java program
- Java platform overview
- getting the software you need
- why did Sun create Java?
- Files (.java, .class, .jar)
35the end of this PowerPoint file
Hooray!