introduction to java java language language introduction
play

Introduction to Java Java Language Language Introduction to - PowerPoint PPT Presentation

Introduction to Java Java Language Language Introduction to plw@ku.ac.th


  1. Introduction to Java Java Language Language Introduction to ปรีดา เลิศพงศวิภูษณะ plw@ku.ac.th ภาควิชาวิศวกรรมคอมพิวเตอร คณะวิศวกรรมศาสตร มหาวิทยาลัยเกษตรศาสตร 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 1

  2. What is Java? ? What is Java � Java is a high-level, third generation programming language, like C, Fortran, Smalltalk, Perl and many others. � Java is most similar to C. But it is not C. � You can use Java to write computer applications that crunch numbers, process words, play games, store data or do any of the thousands of other things computer software can do. 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 2

  3. Java is a Platform Java is a Platform � Java is a platform for application development. � Java solves the problem of platform-independence by using byte code. � It looks a lot like machine language, but unlike machine language Java byte code is exactly the same on every platform. � Java programs that have been compiled into byte code still need an interpreter to execute them on any given platform. � The interpreter reads the byte code and translates it into the native language of the host machine on the fly. 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 3

  4. Java is Simple Java is Simple � Java was designed to make it much easier to write bug free code. � Java has considerably more functionality than C, primarily because of the large class library. � Java is easy to read and write. � Java makes to providing bug-free code is automatic memory allocation and deallocation. � The language is small so it’s easy to become fluent. � The language is interpreted so the compile-run-link cycle is much shorter. � It’s very difficult to write a Java program that will crash your system. 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 4

  5. Java is Object- -Oriented Oriented Java is Object � In object-oriented programs data is represented by objects. � Objects have 2 sections, fields (instance variables) and methods. Fields tell you what an object is. Methods tell you what an object does. � Object-Oriented programming advantages including: – Simpler, easier to read programs – More efficient reuse of code – Faster time to market – More robust, error-free code 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 5

  6. Java is Platform Independent Java is Platform Independent � Java was designed to not be cross-platform in source form like C, but also in compiled binary form. � Java is compiled to an intermediate form called byte-code. � To make Java cross-platform is the elimination of undefined or architecture dependent constructs. � A Java program never really executes natively on the host machine. � A special native program called the Java interpreter reads the byte code and executes the corresponding native machine � Thus to port Java programs to a new platform all that is needed is to port the interpreter and some of the library routines. 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 6

  7. Java is Safe Java is Safe � Java was designed from the ground up to allow for secure execution of code across a network, even then the source of that code was untrusted and possibly malicious. � There are no pointers in Java. Cannot access arbitrary addresses in memory. � Java implements a robust exception handling mechanism to deal with both expected and unexpected errors. � Java, by making it easier to write bug-free code. 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 7

  8. Java is High- -Performance Performance Java is High � Java byte code can be compiled on the fly to code that rivals C++ in speed using a “just-in-time compiler”. � Working on native-machine-architecture compilers for Java. � Does not require a separate interpreter. 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 8

  9. Java is Multi- -Threaded Threaded Java is Multi � Java is inherently multi-threaded. � The Java environment can ensure that a malicious applet doesn’t steal all of the host’s CPU cycles. � A single Java program can have many different threads executing independently and continuously. � Hard to find bugs. 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 9

  10. Java is Dynamically linked Java is Dynamically linked � Java does not have an explicit link phase. � Java source code is divided into – .java files, roughly one per each class in program – .class files, containing byte code. � The compiler searches the current directory and directories specified in the CLASSPATH environment variable to find other classes explicitly referenced by name in each source code file. � class that were unknown to a program when it was compiled can still be loaded into it at runtime. 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 10

  11. Java is Garbage Collected Java is Garbage Collected � Don’t Need to explicitly allocate or deallocate memory in Java. � Memory is allocated as needed, both on the stack and the heap, and reclaimed by the garbage collector. � There’s no malloc() , free() or destructor methods. � There are constructors and these do allocate memory on the heap, but this is transparent to the programmer. � Most Java virtual machines use an inefficient, mark and sweep garbage collector. 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 11

  12. The Hello World Application class HelloWorld { public static void main (String args[]) { System.out.println("Hello World!"); } } 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 12

  13. Compiling and Running Hello World Compiling and Running Hello World � At command-line prompt and type : javac nofile.java � Unix prompt and type : % javac HelloWorld.java % java HelloWorld HelloWorld � Windows type : C:> javac HelloWorld.java C:> java HelloWorld HelloWorld C:> 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 13

  14. for loops for loops class Count { public static void main (String args[]) { int i; for (i = 0; i < 50; i = i+1) { System.out.println(i); } } } 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 14

  15. for loops ( (variable inside variable inside) ) for loops class Count { public static void main (String args[]) { for (int i = 0; i < 50; i = i+1) { System.out.println(i); } } } 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 15

  16. Increment and decrement operators Increment and decrement operators � Using ++ for increment and -- for decrement class Count { public static void main (String args[]) { for (int i = 0; i < 50; i++) { System.out.println(i); } } } 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 16

  17. Print statements Print statements class PrintArgs { public static void main (String args[]) { for (int i = 0; i < args.length; i++) { System.out.println(args[i]); } } } 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 17

  18. Print statements II II Print statements � You can concatenate arguments to println() with a plus sign (+), for example, System.out.println(“There are ” + args.length + “ command line arguments”); � Using print() instead of println() does not break the line, for example, System.out.print(“There are ”); System.out.print(args.length); System.out.print(“ command line arguments”); System.out.println(); 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 18

  19. Fibonacci Numbers Numbers Fibonacci class Fibonacci { public static void main (String args[]) { int low = 1; int high = 0; System.out.println(low); while (high < 50) { System.out.println(high); int temp = high; high = high + low; low = temp; } } } 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 19

  20. Variables and Data Types � There are eight primitive data types in Java: – boolean – byte – short – int – long – float – double – char 23 มิถุนายน 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 20

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend