Download Java Background - v1.2 ()

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java Background
v1.2
24-May-17
1
topics
1. simplest Java program
2. Java platform overview
3. getting the software you need
4. why did Sun create Java?
5. Files (.java, .class, .jar)
24-May-17
Java_background..ppt
2
1
24-May-17
the simplest Java program
Java_background..ppt
3
simplest program in C and Java
#include <stdio.h>
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.
24-May-17
Java_background..ppt
4
simplest 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.
24-May-17
Java_background..ppt
5
simplest 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.
24-May-17
Java_background..ppt
6
2
24-May-17
Java platform overview
Java_background..ppt
7
Java 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 IDE’s for Java
• Eclipse (free from IBM)
• Netbeans (free from Sun)
• BlueJ (free from Monash University)
24-May-17
Java_background..ppt
8
Java’s Just in Time (JIT) compiler
.java
Java
source
(cached in
memory)
native
code
24-May-17
at compile time
compiler
at run time
.class
bytecode
.class file
metadata
JIT compilation
occurs the first time
a method is called,
and only if it’s called
JIT
Compiler
Java_background..ppt
9
the 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 API’s”
• the libraries are designed to be used identically regardless of OS
24-May-17
Java_background..ppt
10
who implements JVM’s?
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)
24-May-17
Java_background..ppt
11
3
24-May-17
getting the software you need
Java_background..ppt
12
versions 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
• Java 6 – 2006
• Faster runtime and other new features
• I am using JDK v6.0 SE on my laptop
• Its runtime is faster than the 5.0 one
24-May-17
Java_background..ppt
13
editions
• 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).
24-May-17
Java_background..ppt
14
what 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
24-May-17
Java_background..ppt
15
the 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
• you’ll 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 IDE’s 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 haven’t used it
24-May-17
Java_background..ppt
16
what 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
• it’s OK if your machine already has a JRE installed
• if your computer can’t 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.
24-May-17
Java_background..ppt
17
the Eclipse welcome screen
go to the workbench
overview
24-May-17
tutorials
samples
Java_background..ppt
what’s
new
18
the Java “perspective”
24-May-17
Java_background..ppt
19
projects 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
24-May-17
Java_background..ppt
20
starting 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!
24-May-17
Java_background..ppt
21
compiling and running
• you don’t 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
24-May-17
Java_background..ppt
22
importing 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
24-May-17
Java_background..ppt
23
4 why did Sun create Java?
24-May-17
Java_background..ppt
24
C 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
24-May-17
Java_background..ppt
25
a 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 automatically—avoids
memory leaks
• single inheritance
• ambiguity and complexity of multiple inheritance avoided
• encapsulation
• all code must be in a class—intended 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
24-May-17
Java_background..ppt
26
5
24-May-17
Files (.java, .class, .jar)
Java_background..ppt
27
What is a .class file?
•
javac takes a text file as input, and
•
.class files contain two main things:
creates a binary file as output
1. bytecode
•
•
instructions for the Java Virtual Machine
at runtime, bytecode gets JIT-compiled
into native code on the fly
code.java
compiler
2. metadata
•
•
24-May-17
a catalog of resources
a description of the class contents that will
be available at runtime by means of
“reflection”
Java_background..ppt
code.class
28
what 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
24-May-17
Java_background..ppt
29
the single-file application
• monolithic application
• all source code compiled into one .JAR file
APP.jar
24-May-17
Java_background..ppt
30
component-based application
• component-based application
.JAR file + one or more other .JAR files
compute.jar
APP.jar
data.jar
24-May-17
Java_background..ppt
31
where 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)
24-May-17
Java_background..ppt
32
jargon checklist
24-May-17
•
what is a .jar file? .java? .class?
•
what is “metadata”?
•
What is bytecode?
•
What is the JVM?
•
what is the JRE?
•
what is “the runtime”?
•
what is JDK?
•
What are J2SE, J2EE and J2ME?
•
What is an IDE?
Java_background..ppt
33
what we just covered
1. simplest Java program
2. Java platform overview
3. getting the software you need
4. why did Sun create Java?
5. Files (.java, .class, .jar)
24-May-17
Java_background..ppt
34
the end of this PowerPoint file
Hooray!
24-May-17
Java_background..ppt
35