760 likes | 794 Views
Lecture2. Introduction to Java and Java Program. Objectives. What is Java ? A programming language A platform(Java APIs, JVM). Java’s History. Java Technology. Java program. Java vs. C++. What is Java?. Java is a programming language general purpose object-oriented.
E N D
Lecture2 Introduction to Java and Java Program
Objectives • What is Java ? • A programming language • A platform(Java APIs, JVM) • Java’s History • Java Technology • Java program • Java vs. C++
What is Java? • Java is a programming language • general purpose • object-oriented • Java is a development environment • Java APIs • Java is an application environment • Java Virtual Machine • Java is a deployment environment • Platform Independence
The Features of Java Language • [SUN] The Java Language : • simple • object-oriented • network-savvy • interpreted • robust • secure • architecture-neutral • high-performance • multithread • dynamic language
Overview of the Java Platform • The Java platform includes: • Java Virtual Machine (JVM) • A number of packages that support the complier, interpreter, and run-time environment for Java (APIs)
What Is a Java Virtual Machine? • Java Virtual Machine • the abstract specification • a concrete implementation • a runtime instance • Starting of Java VM • start point:main() method of some initial class • main() method: public, static, return void • accept one parameter: a String array • public static void main(String[] args)
The Architecture of the Java VM • Class loader subsystem • read class files • Execution engine • Use native method interface • Runtime data area • Method area • Heap • Java Stack • PC registers • Native method stacks
Garbage Collection • Basic idea • keep track of what memory is referenced and when it is no longer accessible, reclaim the memory • Garbage collector’s function • Finds data objects that are no longer in use(garbage) • Make garbage space available for reuse
Garbage Collection in Java • JVM spec. doesn’t require any garbage collection • Garbage collection thread • Usually runs with low priority • JDK1.0.2 and 1.1 uses a partially conservative mark-and –sweep garbage collector • Garbage detection = reachability test • An object that are reachable from the roots are considered “live” • No dangling reference
Security-Related Capabilities(1) • JDK 1.0 (Fall 1995) • Policy: “Sandbox” for applets; others unlimited • Mechanisms: SecurityManager, Bytecode verifier, Classloader • JDK 1.1 (Spring 1997) • Policy: can also grant total trust to signed applets • Mechanisms: Java Archive (JAR), crypto-related APIs • Inflexible: Too little or too much privilege
Security-Related Capabilities(2) • Netscape & Microsoft Extensions • Enabled more flexible approaches • Incompatible with each other and with Sun • J2SE (Java 2 Platform Standard Edition) (Fall 1998) • Includes SDK 1.2 and runtime • Policy: can also grant fine-grained privileges to specific applets/classes based on source and/or signatures • Mechanisms: AccessController, ProtectionDomain, CodeSource, Permission, GuardedObject, … • “Java Plug-in” supports both Microsoft & Netscape
What is Sandbox? • Class loader,byte-code verifier and security manager • Java protected domains use of permissions by user
Security Framework-JDK1.2 No built-in notion of trusted code,each applet runs with separate permissions
Class Library- Java Core APIs (1) Language: string, thread, exceptions, math, etc. (2) Utilities: hash table, stack, array, time, date (3) Input /Output Streams: file, network, device (4) Network Facilities: socket, TCP/IP, Internet (5) Graphics and Graphical User Interface(GUI): window, menu, bar, dialogue
Where Is the Java Platform Used? • Where is the Java Platform Used? • to write small programs that run in Web pages • to produce Java classes that can act as extensions to your Web server • to distribute programs over a network • to use Java-based technology to meet the demands of Enterprise-wide distributed environments
Kinds of Java Platform • Base Java platform • Network computers, Desktop computers, Workstations • Windows, Macintosh, OS/2, Linux, Unix, Netware, etc… • Embedded Java platform • Set-top box, cellular phones, Java Card, etc… • JavaOS, Java Processor
Brief History of Java • 1991:Created by James Gosling et. al. at Sun Microsystems "The Green Team" – Called the language 'Oak' • 1994:Bill Joy "HotJava" browser • 1995: Netscape agrees to support Java in Netscape browsers • January 1996: first official release JDK 1.0 • Web: applets, security, URL, networking • GUI: Abstract Windows Toolkit (AWT) • February 1997: JDK 1.1 • December 1998: Java 2 Platform (JDK 1.2)
Java2(1) • J2SE(Java2 Standard Edition) • Basic Java specification for desktop computer development • Runs in the JVM • J2EE(Java2 Enterprise Edition) • Multi-tier, highly available, secure, reliable and scalable • Requires different Java enterprise technologies • Runs in the JVM • J2ME(Java2 Micro Edition) • set-top box, Java Card, etc…. • Runs in the KVM
JavaTechnology • Development Environments • Optional Packages • Other Java Technologies
(1) Java Developer’s Kits(JDK) Java IDE Tools(1) Compiler Interpreter Debugger Applet Viewer Generator of head file Document Administrator Java archive file Generator javac java jdb appletviewer javah javadoc jar • Get it from http://www.java.sun.com.
Java IDE Tools(3) • (2) VisualAge for Java • This environment provides : • a convenient way to enter and save Java programs, • both standalone applications and applets • a debugging facility • an excellent graphic interface which allows students • to create applets which support graphical user • interfaces(GUIs) • (3) Inprise JBuilder • (4) Microsoft Visual J++ • (5) Symantec Café • (6) Sun Java Workshop
Java Optional Packages • Java Mail • Java 3D • Java Media Framework(JMF) • Java Servlet • Java Cryptography Extension(JCE) • Java Secure Socket Extension(JSSE)
Java Programs • Different environments require different kinds of Java • programs: — Applications: can run standalone on a computer of any size, and can be anything from large suites of software to simple utilities — Applets: are programs that can be launched from HTML documents(client-side code that runs in a Web browser) — Servlets: run on Java-enabled Web servers and generate HTML documents that are sent to the client browser for display (sever-side code that runs in a Web server) — JavaBeans: are Java classes or groups of Java classes that conform to a strict set of standards(reusable components that can be manipulate programmatically)
Applet and Servlet www.abc.com requests Applet 2 Applet 3 Applet 1 Servlet HTML files JVM JVM applets indication Browser Web Server
Applications(1) Java program source code foo.java
Applications(2) foo.java javac Java compiler
Applications(3) foo.java javac foo.class Program bytecode
Applications(4) foo.java javac foo.class java Java bytecode interpreter
Applets(1) Same procedure as for Applications foo.java javac foo.class
Applets(2) foo.java javac foo.class Netscape Your favorite browser
Your First Java Application(1) /* Display a message */public class HelloWorld { public static void main(String[] args){ System.out.println("Hello World!"); }} HelloWorld.java
Your First Java Application(2) /* Display a message */class HelloWorld { public static void main(String[] args){ System.out.println("Hello World!"); }} This will be the name of the resulting .class file HelloWorld.java
Your First Java Application(3) C:\> javac HelloWorld.java C:\> java HelloWorld This produces HelloWorld.class This runs HelloWorld.class
Your First Java Applet(1) import java.applet.Applet;import java.awt.Graphics;public class helloX extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); }} helloX.java
Your First Java Applet (2) C:\> javac helloX.java This produces hello.class(like before)
Your First Java Applet (3) C:\> javac helloX.java This produces hello.class(like before) but then...
Your First Java Applet(4) <HTML> <HEAD> <TITLE> A Simple Program </TITLE> </HEAD> <BODY> Here is the output of my program: <APPLET CODE="helloX.class" WIDTH=150 HEIGHT=25> </APPLET> </BODY> </HTML> hellotest.html
Your First Java Applet(5) Appletviewer hellotest.html
A Program Analysis /* Display a message */class hello { public static void main(String[] args){ System.out.println("Hello World!"); }}
Functions • Java program consists of a named class. /* Display a message */class hello { public static void main(String[] args){ System.out.println("Hello World!"); }}
Functions • Java program consists of a named class. • The body of the class is surrounded by braces /* Display a message */class hello { public static void main(String[] args){ System.out.println("Hello World!"); }}
Functions • (Almost) every Java program must have one main() function. /* Display a message */class hello { public static void main(String[] args){ System.out.println("Hello World!"); }}
Functions • (Almost) every Java program must have one and only one main() function. • The body of the function is surrounded by braces /* Display a message */class hello { public static void main(String[] args){ System.out.println("Hello World!"); }}
Statements • A semicolon is a statement terminator (not a statement separator). /* Display a message */class hello { public static void main(String[] args){ System.out.println("Hello World!"); }}
Statements • Statements can be combined within braces to form a compound statement. • White space is (almost always) ignored. /* Display a message */class hello { public static void main(String[] args){ System.out.println("Hello World!"); }}
Objects • The identifierSystem.outis an object. • The identifierprintlnis one of the methods for that object. /* Display a message */class hello { public static void main(String[] args){ System.out.println("Hello World!"); }}