260 likes | 278 Views
Learn about the JDK, its tools, Java API, differences from JRE, Java vs. C/C++, Java program structure, bytecode, JVM, and JDK editions.
E N D
CS241001 Software StudioJava 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? • 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
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
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.
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,…
C/C++ vs. Java 1010 1010 JVM Compiler Linux CC++ 1010 1010 Byte-code java Compiler JVM Compiler Windows 1010 1010 JVM Compiler Mac
Install JDK SE • http://www.oracle.com/technetwork/java/javase/downloads/index.html
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.
Environment Setup • Install • Environment setup • Add “C:\Program Files\Java\jdk(版本號碼)\bin” to “Path” which is in “system variables”.
Introduce Java SE API specifications • http://java.sun.com/javase/6/docs/api/
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!!"); } }
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 • …..
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
Java-execute • Javaw.exe : A non console version java interpreter, only on windows platform. • -classpath • -jar jarfile • -verbose
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[]); }
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 }
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.*;
Create an Instance • HelloJava helloj = new HelloJava(); • helloj.sayHello(); Hello!! HelloJava Factory helloj
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) { …} • }
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.*/ • …
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); • } • }