1 / 26

CS241001 Software Studio Java Lab 1

CS241001 Software Studio Java Lab 1. Meng-Ting Wang mtwang@pllab.cs.nthu.edu.tw PLLAB, Computer Science Department, National Tsing-Hua University. Introduction to the JDK “Hello Java Example” Tools of the JDK: javac, java, javap, jdb, javah…. Java Development Toolkit. What is JDK?

arthurclark
Download Presentation

CS241001 Software Studio Java Lab 1

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. CS241001 Software StudioJava Lab 1 Meng-Ting Wang mtwang@pllab.cs.nthu.edu.tw PLLAB, Computer Science Department, National Tsing-Hua University

  2. Introduction to the JDK • “Hello Java Example” • Tools of the JDK: javac, java, javap, jdb, javah…

  3. Java Development Toolkit • What is JDK? • It is a pack of programs which is used for programming, compiling and executing Java program. It includes the Java library, the execution runtime, java compiler and debugger, and JNI tool. • What's the difference between "JDK" and "JRE"? • JDK (Java Development Kit) is the package for develop java programs. • JRE (Java Runtime Environment) is an implementation of the Java Virtual Machine which actually executes Java programs

  4. Java API • Application Programming Interface is an interface in computer science that defines the ways by which an application program may request services from libraries and/or operating systems. Source: http://www.wikipedia.com

  5. C/C++ vs. Java • Sorting • Bubble sort, merge sort, insertion sort… • Math • Data structure • Linked-list… • Array visit, add, and remove… • Heap, stack, queue, string…. In Java SIMPLY CALL THE API !!! In C/C++ - You need to write a high performance code by yourself. In C/C++ - You need to writea high performance code by yourself.

  6. What else java API support….

  7. JDK vs. JRE Java Program A.java public class A{ public static void main (Stirng[] args) { System.out.println (“Hello World!!”); } } Java program Tools Java, javac, jar, javap, javah, jdb,… JDK Bytecode javac:Compile APIs Lang, util, collections, JDBC, math,... JRE A.class 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3; //String Hello World! 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/Str ing;)V 8: return Java VM java:Execute Platform: Linux, windows, Solaris,…

  8. C/C++ vs. Java 1010 1010 JVM Compiler Linux CC++ 1010 1010 Byte-code java Compiler JVM Compiler Windows 1010 1010 JVM Compiler Mac

  9. Install JDK SE • http://www.oracle.com/technetwork/java/javase/downloads/index.html

  10. JDK Editions • Java SE- Java Platform, Standard Edition • Java EE- Java Platform, Enterprise Edition • Java ME- Java platform, Micro Edition • JavaFX-  A software platform for creating and delivering rich Internet applications (RIAs) that can run across wide variety of connected devices. 

  11. Environment Setup • Install • Environment setup • Add “C:\Program Files\Java\jdk(版本號碼)\bin” to “Path” which is in “system variables”.

  12. Environment Setup

  13. Introduce Java SE API specifications • http://java.sun.com/javase/6/docs/api/

  14. Hello Java!! • Create a Hellojava.java file using Notepad++ • javac HelloJava.java • java HelloJava public class HelloJava{ public static void main (String[] args) { System.out.println ("Hello Java!!"); } }

  15. Tools for Programming Java • IDE: Eclipse, Netbeans, IntelliJ,… • Editor: NotePad++, JEdit, EditPlus,… • Tools of JDK: • javac: Compile the java program into class file • java : Execute the class file. • javap: Dump bytecode from class file • jdb: java debugger • Javah: Generate the JNI header file • …..

  16. Javac-compile • -verbose • -sourcepath • default: .;C:\Program Files\Java\jre6\lib\ext\QtJava.zip • -classpath • Default: .;C:\Program Files\Java\jre6\lib\ext\QtJava.zip • -encoding • Specify character encoding used by source files • CP950, Big5, UTF8,… • -g Generate all debugging info • -verbose Output messages about what the compiler is doing • -d <directory> Specify where to place generated class files • -encoding <encoding> • -help Print a synopsis of standard options

  17. Java-execute • Javaw.exe : A non console version java interpreter, only on windows platform. • -classpath • -jar jarfile • -verbose

  18. javap - The Java Class File Disassembler • Javap HelloJava Compiled from "HelloJava.java" public class HelloJava extends java.lang.Object{ public HelloJava(); public static void main(java.lang.String[]); }

  19. Javap -c HelloJava Compiled from "HelloJava.java" public class HelloJava extends java.lang.Object{ public HelloJava(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3; //String Hello World!! 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;) 8: return }

  20. Java Namespace • Package: • import package tw.edu.nthu.cs.pllab.cs241001.examples; cs241001.pllab.cs.nthu.edu.tw + examples ※Rule: Reverse website address + package name import tw.edu.nthu.cs.pllab.cs241001.examples.*; System.out.println(); ※Default import java.lang.*;

  21. Create an Instance • HelloJava helloj = new HelloJava(); • helloj.sayHello(); Hello!! HelloJava Factory helloj

  22. Typewriter simulation

  23. TypeWriter.java(1/2) • //package ?? • public class TypeWriter{ • private String msgArray[] = {"Y","o","u"," ","h","a","v","e"," ","t","o"," ","p","r","i" ,"n" , "t", " ", • "t","h","e"," ","r","i","g","h","t"," ","m","e","s","s" , "a" , "g","e", " ","o","u","t"," ", • "u","s","i","n" ,"g" , " " ,"t","h", "e", " ","e","x","a", "c", "t"," ","m","e","s","s" , "a" , "g","e", " ","l","e","n","g","t","h","."}; • private String msgArray2[] = {"C","o","n","g","r","a","d","u","l","a","t","i","o","n","s" , " " , • "y", "o", "u","'","v","e"," ","a","l","r","e","a","d","y"," ","m","e" , "t" , " ", • "t","h", "e", " ","r","e","q","u","i","r","e","m","e","n","t","."}; • private int sleepMS; • public TypeWriter(int sleepMS){ • this.sleepMS = sleepMS; } • public void sayHello(){ • System.out.println (“Hello, welcome to CS241001.”); } • private void pause(){ • try{ • Thread.sleep(sleepMS); • }catch(InterruptedException e) { …} • }

  24. TypeWriter.java(2/2) • public void say (int startIndex, int stopIndex){ • try{ • for (int i = startIndex ; i <= stopIndex ; i++ ) • { • System.out.print(); • pause(); • } • if(){ • System.out.println("\nTry differnet index number; this one is too small."); • } • else if(stopIndex==this.msgArray.length-1){ • System.out.println(" "); • for (int i = 0 ; i <= this.msgArray2.length ; i++ ) { • System.out.print(msgArray2[i]); • pause();} • } • }catch(Exception e){ • //System.out.println("exp"+this.msgArray.length+" "+stopIndex); • /*Tell the user the number is too large here.*/ • …

  25. TypeWriterCaller.java • //import ?? • public class TypeWriterCaller{ • TypeWriter TW; • public TypeWriterCaller(int end){ • TW = new TypeWriter(200); • TW.sayHello(); • TW.say(0, end); • } • public static void main(String[] args){ • int end = Integer.parseInt(args[0]); • TypeWriterCaller TWC= new TypeWriterCaller(end); • } • }

More Related