1 / 76

Introduction to Java and Java Program

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.

tbreaux
Télécharger la présentation

Introduction to Java and Java Program

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture2 Introduction to Java and Java Program

  2. Objectives • What is Java ? • A programming language • A platform(Java APIs, JVM) • Java’s History • Java Technology • Java program • Java vs. C++

  3. 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

  4. The Features of Java Language • [SUN] The Java Language : • simple • object-oriented • network-savvy • interpreted • robust • secure • architecture-neutral • high-performance • multithread • dynamic language

  5. 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)

  6. 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)

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. Security Framework-JDK1.0

  13. What is Sandbox? • Class loader,byte-code verifier and security manager • Java protected domains use of permissions by user

  14. Security Framework-JDK1.1

  15. Security Framework-JDK1.2 No built-in notion of trusted code,each applet runs with separate permissions

  16. 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

  17. 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

  18. 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

  19. 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)

  20. Java 2 Architecture

  21. 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

  22. JavaTechnology • Development Environments • Optional Packages • Other Java Technologies

  23. (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.

  24. 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

  25. From Source Code to Running Program

  26. Java Optional Packages • Java Mail • Java 3D • Java Media Framework(JMF) • Java Servlet • Java Cryptography Extension(JCE) • Java Secure Socket Extension(JSSE)

  27. 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)

  28. Applet and Servlet www.abc.com requests Applet 2 Applet 3 Applet 1 Servlet HTML files JVM JVM applets indication Browser Web Server

  29. Applications(1) Java program source code foo.java

  30. Applications(2) foo.java javac Java compiler

  31. Applications(3) foo.java javac foo.class Program bytecode

  32. Applications(4) foo.java javac foo.class java Java bytecode interpreter

  33. Applets(1) Same procedure as for Applications foo.java javac foo.class

  34. Applets(2) foo.java javac foo.class Netscape Your favorite browser

  35. Your First Java Application(1) /* Display a message */public class HelloWorld { public static void main(String[] args){ System.out.println("Hello World!"); }} HelloWorld.java

  36. 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

  37. Your First Java Application(3) C:\> javac HelloWorld.java C:\> java HelloWorld This produces HelloWorld.class This runs HelloWorld.class

  38. 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

  39. Your First Java Applet (2) C:\> javac helloX.java This produces hello.class(like before)

  40. Your First Java Applet (3) C:\> javac helloX.java This produces hello.class(like before) but then...

  41. 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

  42. Your First Java Applet(5) Appletviewer hellotest.html

  43. A Program Analysis /* Display a message */class hello { public static void main(String[] args){ System.out.println("Hello World!"); }}

  44. Functions • Java program consists of a named class. /* Display a message */class hello { public static void main(String[] args){ System.out.println("Hello World!"); }}

  45. 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!"); }}

  46. 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!"); }}

  47. 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!"); }}

  48. 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!"); }}

  49. 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!"); }}

  50. 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!"); }}

More Related