Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Part I : Chapter 01 Introduction to Java Programming 1 Objectives • To learn about Java and its history. • To create, compile, and run Java programs. • To understand the Java runtime environment. • To write a simple Java application. 2 History of Java • Green Project • Developed by a team led by James Gosling at Sun Microsystems. • 1991: Oak (use in embedded consumer electronic application) • 1995: Java (redesigned for developing Internet applications) James Gosling and Duke http://java.sun.com/people/jag/ 3 History of Java • This is a snapshot taken at a barbecue that James Gosling threw for some of the folks associated with the Green Team. From left to right they are: Al Frazier, Joe Palrang, Mike Sheridan, Ed Frank, Don Jackson, Faye Baxter, Patrick Naughton, Chris Warth, James Gosling, Bob Weisblatt, David Lavallee, and Jon Payne. Missing in action: Cindy Long, Chuck Clanton, Sheueling Chang, and Craig Forrest. • (http://java.sun.com/features/1998/05/birthday.html) 4 Java Version History Version Code-Name Release Date JDK 1.1.4 Sparkler Sept 12, 1997 JDK 1.1.5 Pumpkin Dec 3, 1997 JDK 1.1.6 Abigail April 24, 1998 JDK 1.1.7 Brutus Sept 28, 1998 JDK 1.1.8 Chelsea April 8, 1999 J2SE 1.2 Playground Dec 4, 1998 J2SE 1.2.1 (none) March 30, 1999 J2SE 1.2.2 Cricket July 8, 1999 J2SE 1.3 Kestrel May 8, 2000 J2SE 1.3.1 Ladybird May 17, 2001 J2SE 1.4.0 Merlin Feb 13, 2002 J2SE 1.4.1 Hopper Sept 16, 2002 J2SE 1.4.2 Mantis June 26, 2003 J2SE 5.0 (1.5.0) Tiger Sept 29, 2004 Java SE 6.0 (1.6.0) Mustang December 11, 2006 Java SE 6 Update 14 - May 28, 2009 Java SE 7.0 (1.7.0) Dolphin Plan to release in 2010 5 Characteristics of Java • Java is simple. • Java is Object-Oriented • Java is Case-sensitive • Java is Interpreted (Write-Once, Run-Anywhere) • Java is Robust • Java is Multithreaded • Java can be manipulated in two ways: Application and Applet 6 Platform of Java Technologies • Java, Micro Edition (JMETM technology) • Java, Standard Edition (JSETM technology) • Java, Enterprise Edition (JEETM technology) 7 Platform of Java Technologies http://java.sun.com/java2/whatis/index.html 8 Architecture of Java Java Programming Language Java class file Java API Java platform Java Virtual Machine (Java VM) Computer System API = Application Programming Interface 9 Typical Java Development Environment Java source code .java Java compiler (javac) .class Java Interpreter (java) HTML page .html Appletviewer or Browser Application Applet 10 Typical Java Development Environment Editor Compiler Disk Class Loader Bytecode Verifier JVM Primary Memory 11 Style of Java Programming • Java application – Run in text mode – Run in GUI mode • Java applet <html> <applet code=“myApplet.class” width=… height=…> </applet> </html> 12 Anatomy of the Application Program • • • • • • • • Comments Reserved Words Modifiers Statements Blocks Classes Methods The main method 13 Java Keywords • • • • • • • • • • • abstract boolean break catch char class default do double false final finally goto if implementation int interface long null package private return short static synchronized this transient true try while bytecase const continue elseextend floatfor import instanceof native new protected public super switch throw thows voidvolatile 14 The First Java Application Example // A first program in Java Begin class definition public class Welcome { public static void main ( String args[] ) { System.out.println(“Welcome to Java Programming”); } } Declaration of main method Arguments of main method 15 The First Java Application Example • System.out is a standard output object to display text in text mode • println is a method of System.out • To call a method, use . between a class / object’s name and the method – System.out.println(“…”); – car.getLevel(); 16 The First Java Application Example • Save as a name of Welcome.java • compile Welcome.java – javac Welcome.java – See the output • run – java Welcome – See the output 17 Java Application Example II // Printing multiple lines in a dialog box import javax.swing.JOptionPane; public class Welcome2 { public static void main (String args[] ) { JOptionPane.showMessageDialog ( null, “Welcome\nto\nJava\nProgramming!”); System.exit(0); // terminate the program } } 18 Java Application Example II • import javax.swing.JOptionPane; to call a class named JOptionPane into the class. • JOptionPane.showMessageDialog ( null, “Welcome\nto\nJava\nProgramming!”); call a method named showMessageDialog of JOptionPane class. • System.exit(0); is applied as the last command of the class to return resource to the system. 19 Java Application Example II • Save the file as Welcome2.java • compile Welcome2.java – javac Welcome2.java – See the output • run – java Welcome2 – See the output 20 The First Applet // The first applet in Java import javax.swing.JApplet; import java.awt.Graphics; inheritance public class WelcomeApplet extends JApplet { public void paint ( Graphics g ) { g.drawString(“Welcome to Java Programming”, 25, 25); } } 21 The First Applet • Save the file as WelcomeApplet.java • compile WelcomeApplet.java – javac WelcomeApplet.java – See the result • run – Create a html file to call the applet <html> <applet code=“WelcomeApplet.class” width=300 height=30> </applet> </html> – Save the .html file. Name the file in any name such as welcome.html – run by typing appletviewer welcome.html – Or call the .html in any browser program. 22 End of Chapter01 Introduction to Java 23