Download presentation
Presentation is loading. Please wait.
Published byRuby Willis Modified over 8 years ago
1
CS241001 Software Studio Java 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 C/C++ - You need to write a high performance code by yourself. In Java SIMPLY CALL THE API !!! In C/C++ - You need to write a high performance code by yourself.
6
What else java API support….
7
JDK vs. JRE A.java public class A{ public static void main (Stirng[] args) { System.out.println (“Hello World!!”); } Java VM Java Program javac:CompileBytecode A.class 0: aload_0 1: invokespecial #1; //Method java/lang/Object." ":()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 Tools Java, javac, jar, javap, javah, jdb,… APIs Lang, util, collections, JDBC, math,... java:Execute JRE JDK Platform: Linux, windows, Solaris,… Java program
8
C/C++ vs. Java C C++ Compiler 1010 Compiler java Byte- code JVM Linux Windows Mac 1010 Compiler 1010 Compiler 1010 JVM 1010 JVM 1010
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/
15
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!!"); }
16
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 – …..
17
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 Specify where to place generated class files -encoding -help Print a synopsis of standard options
18
Java-execute Javaw.exe : A non console version java interpreter, only on windows platform. -classpath -jar jarfile -verbose
19
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[]); }
20
Compiled from "HelloJava.java" public class HelloJava extends java.lang.Object{ public HelloJava(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object." ":()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 } Javap -c HelloJava
21
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.*;
22
Create an Instance HelloJava helloj = new HelloJava(); helloj.sayHello(); Hello!! HelloJava Factory helloj
23
Typewriter simulation
24
TypeWriter.java(1/2) 1.//package ?? 2.public class TypeWriter{ 3. private String msgArray[] = {"Y","o","u"," ","h","a","v","e"," ","t","o"," ","p","r","i","n", "t", " ", 4. "t","h","e"," ","r","i","g","h","t"," ","m","e","s","s", "a", "g","e", " ","o","u","t"," ", 5. "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","."}; 6. 7. private String msgArray2[] = {"C","o","n","g","r","a","d","u","l","a","t","i","o","n","s", " ", 8. "y", "o", "u","'","v","e"," ","a","l","r","e","a","d","y"," ","m","e", "t", " ", 9. "t","h", "e", " ","r","e","q","u","i","r","e","m","e","n","t","."}; 10. private int sleepMS; 11. 12. public TypeWriter(int sleepMS){ 13.this.sleepMS = sleepMS; } 14. 15.public void sayHello(){ 16. System.out.println (“Hello, welcome to CS241001.”); } 17. 18.private void pause(){ 19. try{ 20. Thread.sleep(sleepMS); 21. }catch(InterruptedException e) { … } 22. }
25
TypeWriter.java(2/2) 23.public void say (int startIndex, int stopIndex){ 24. try{ 25.for (int i = startIndex ; i <= stopIndex ; i++ ) 26. { 27.System.out.print(); 28. pause(); 29.} 30. if(){ 31. System.out.println("\nTry differnet index number; this one is too small."); 32. } 33. else if(stopIndex==this.msgArray.length-1){ 34. System.out.println(" "); 35. for (int i = 0 ; i <= this.msgArray2.length ; i++ ) { 36. System.out.print(msgArray2[i]); 37.pause(); } 38. } 39. }catch(Exception e){ 40. //System.out.println("exp"+this.msgArray.length+" "+stopIndex); 41. /*Tell the user the number is too large here.*/ 42.…
26
TypeWriterCaller.java 1.//import ?? 2.public class TypeWriterCaller{ 3. TypeWriter TW; 4.public TypeWriterCaller(int end){ 5. 6.TW = new TypeWriter(200); 7.TW.sayHello(); 8.TW.say(0, end); 9.} 10. public static void main(String[] args){ 11.int end = Integer.parseInt(args[0]); 12.TypeWriterCaller TWC= new TypeWriterCaller(end); 13.} 14.}
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.