1 / 36

Introduction to Java Programming

Introduction to Java Programming. Cheng-Chia Chen February 2010. Course web page. Lecture 1. Introduction. Cheng-Chia Chen. Contents. What is Java? Features of Java Evolution of Java Develop first Java program. What is Java?. Java is a programming language,

mercia
Download Presentation

Introduction to Java Programming

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. Introduction to Java Programming Cheng-Chia Chen February 2010

  2. Course web page

  3. Lecture 1. Introduction Cheng-Chia Chen

  4. Contents • What is Java? • Features of Java • Evolution of Java • Develop first Java program

  5. What is Java? Java is • a programming language, • defined in The Java language specification (v1,v2,v3) • a virtual machine, • Defined in The java virtual machine (v1, v2) • a platform • Standard edition (j2se 6.0 java se 6): • Java platform, standard edition 6 • Enterprise edition(j2ee 5.0  java ee 5): • Java platform, enterprise edition 5 (for enterprise-class service-oriented architecture (SOA) and web applications.) • Micro edition (j2me  java me): • Java Platform, Micro edition (for mobile&embeded devices)

  6. servers & enterprise computers Desktop & personal computers High-end consumer devices Low-end devices J2ME smartcards Java 2 Platform editions and their target markets

  7. Java SE 6 Platform at a Glance

  8. Features of the java language: Java is • simple • object-oriented • distributed • interpreted • robust • secure • architecture-neutral • portable • high performance • multithreaded • dynamic

  9. Java is Simple • Intentionally created to be syntactically similar to C/C++ • Eliminates traditionally troublesome features of C/C++ • Pointer arithmetic • Multiple inheritance • Implicit type coercions • Explicit memory management • Preprocessor • Eliminates features of C/C++ • struct, typedef,union • generic, enum (both recovered since jdk5.0) • (Programmer controlled) operator overloading (but preserve method overloading) • Features included as part of base language: • Threads • Exception handling • reflection

  10. Java is Object-oriented • Java is inherently object-oriented. • Although many object-oriented languages began strictly as procedural languages, Java was designed from the start to be object-oriented. • Object-oriented programming (OOP) is a popular programming approach that is replacing traditional procedural programming techniques. • One of the central issues in software development is how to reuse code. • Object-oriented programming provides great flexibility, modularity, clarity, and reusability through encapsulation, inheritance, and polymorphism.

  11. What is object-oriented ? • Systems are built from sets of classes • Classes are instantiated at runtime to give objects • Objects communicate via messages passing • Everything is part of a class • supported OO Concepts: • Data abstraction and Encapsulation • Inheritance • Polymorphism • Dynamic Binding • e.g. variable of Object type can hold everything • Logical cluster of classes == package

  12. Java is a Distributed language • Distributed computing involves several computers working together on a network. • Java is designed to make distributed computing easy. • Since networking capability is inherently integrated into Java, writing network programs is like sending and receiving data to and from a file. • Network programming support built into JDK class library: • TCP sockets ,UDP packets ,IP addresses • URLs ,RMI (Remote Method Invocation) • Web Service • Security features designed into language

  13. Java is an Interpreted language • We need an interpreter to run Java programs. • The programs are compiled into the Java Virtual Machine code called java bytecode. • The bytecode is machine-independent and can run on any machine that has a Java interpreter, which is part of the Java Virtual Machine (JVM).

  14. Java is Robust • characteristics of Java • Strongly-typed language (cf Smalltalk and VisualBasic) • Compile-time and runtime checking • No pointer arithmetic • Exception handling • Automatic memory management • Java compilers can detect many problems that would first show up at execution time in other languages. • Java eliminated certain types of error-prone programming constructs found in other languages. • Runtime exception-handling feature provides programming support for robustness.

  15. Java is secure • Designed with security in mind. • Java implements several security mechanisms to protect your system against harm caused by stray programs. • Allow users to download untrusted code over a network and run it in a secure environment in which it cannot do any harm. • Configurable security levels and restrictions. • subjected to intense scrutiny by security experts with [potentially serious ] bugs found and fixed. • become a big news if new bugs found!! • One of the best mainstream platforms with the strongest security guarantee.

  16. Java is Architecture-neutral • Byte-codes are architecture neutral • Write once, run anywhere • With a Java Virtual Machine (JVM), you can write one program that will run on any platform. • Performance suffers somewhat by using bytecodes

  17. Java is portable (source code level) • Source code : • Primitive type sizes are explicit - not architecture dependent • Strings and characters are (16-bit) Unicode compliant • easier for internationalization. • Byte code: • run everywhere where a JVM is available • User experience : • GUI libraries give a native graphic library-independent mechanism for creating quality graphical interfaces (sort of) • "They gave us a library that is good for writing programs that look equally mediocre on the different systems."(Core Java, page 9)

  18. High performance • Interpreting leads to quicker development cycle • Depends what you compare it to • "Slightly faster than VB" - (Core Java, page 9) • JITC(Just-In-Time Compiler) help greatly in this respect • Sun’s Java HotSpot is Newest high performace JIT compiler. • Can use native code for mission-critical performance sections of code • JNI: Java Native Interface • Sacrifice portability.

  19. Multithreaded • Based on well-known 20 year old Hoare monitor synchronization • Thread support built into language • Thread synchronization primitives supplied • Garbage collector runs permanently as a low priority background thread

  20. Dynamic • Class linking, layout, name resolution and object references not resolved until run-time • Runtime Type Information (RTTI) available • Can check the type of objects at run-time • java.reflect.* package • Class class for dynamic instantiation • Can create objects of types unkown until runtime. • String sexClassName = getSex(); • Object p = Class.forName(sexClassName).instance(); • If(p instanceof Male) {…} • else if (p instanceof Female) { … } • else {… }

  21. An Example /** * The HelloJava class implements an application that * simply displays "Hello Java!" to the standard output. */ class HelloJava { public static void main(String[] args) { // Display "Hello Java!" System.out.println("Hello Java!"); } }

  22. Early history of Java • Green Project (1990) • need a small PL for developing consumer device operating software • Requirements: small size, robust, portable, extremely reliable ,real-time performance • Oak // the resulting language • Originally used C++, then realized a new language was needed • Original requirements same as for current language • Java (1993) // OAK was renamed Java • Intended market never eventuated (no technology/product sold) • WWW starting to takeoff • Language design "based on" many current OO languages (e.g., C++, Eiffel, Smalltalk, Cedar/Mesa, Objective C) • 1995/5/23 Sun launched Java • JDK 1.0 released early 1996/1/23 ( 211 classes / 8 pkgs) • Early History Website: • http://java.sun.com/features/1998/05/birthday.html

  23. Evolution of the Java Language (8 packages) (23 packages) (59 packages) (76 packages) 2002 (135 packages) (166 packages) (202 packages)

  24. Develop your first Java Program 1. Required software 2. Creating Your First Application a. Create a Java Source File b. Compile the Source File c. Run the Program

  25. Required Softwares To write your first program, you will need: 1. The Java Platform, Standard Edition. 2. A text editor. Ex: • NotePad, • Ultraedit, • EditPlus2

  26. 2. Creating Your First Application • The first program, HelloWorldApp, will simply display : “Hello World!". • Steps: • a. Create a Java source file. • > Notepad HelloWorldApp.java • b. Compile the source file into a bytecode file. • > javac HelloWorldApp.java • c. Run the program contained in the bytecode file. • >java HelloWorldApp

  27. HelloJava.java /** * The HelloWorldApp class implements an application that * simply displays "Hello World!" to the standard output. */ class HelloWorldApp { public static void main(String[] args) { // Display "Hello World!" System.out.println("Hello World!"); } }

  28. Java Jargon

  29. Java 2 SDK Installation Instructions (for WIN32) 1. Download java SDK standard edition 2. Run the Java SDK executable (*.exe). • determine where to install java se. (ex: c:\java\jdk6) • > set JAVA_HOME= c:\java\jdk6 3. Update the PATH variable • so that you can type ‘java’ instead of ‘c:\java\jdk6\bin\java’ to invoke java tools. • > path=%JAVA_HOME%\bin;%PATH% 4. Check the CLASSPATH variable • Used by java tools to determine where to find your personal (nonsystem) java class files • Types of java byte codes (class files): • System : java tools know where to find them. • Extensions: put in %JAVA_HOME%\jre\lib\ext • Personal: via CLASSPATH or –cp/–classpath options 5. Start using the Java SDK tools! • java, javac, javadoc, jdb, javap,…

  30. Create Your first Java application using Eclipse IDE • install Eclipse SDK • Download Eclipse SDK (choose Classic) • unzip to a dir (c://eclipse) • Start Eclipse • go to c:\eclipse • run eclipse.exe • choose Java perspective • create a java project ( helloWorld ) • create a package chapter1 using package widard • create HelloWorld class using class wizard • run HelloWorld class • Reference: Eclipse Tutorial | One-Page Startup Instruction

  31. How java code are structured ? • A java application consists of • = many packages [from JRE + from 3rd party + your own ] • A package consists of • many classes • many subpackages • Note: importing a package does not mean importing its subpackages. • A java source file xxx.java is called a compilation unit, which may • contain multiple java class definitions and • may be compiled into multiple java byte code (***.class ).

  32. A class has two names: • simple name • fully qualified name = package name. SimpleName • Ex: • java.lang.String • javax.swing.JButton • The package/subpackage/class structure is not only logically analogous to directory/subdirectory/file in OS file system but in fact they are stored physically in file system in this way. • Namely, if package p corresponds to a directory d then subpackage p.q of p would be stored in subdirecty q of d, and class p.A of p would be stored in file named A.class or A.java.

  33. Ex • How does java machine find a class or src x.y.z.A in the package x.y.z ? • Notes: • package x.y.z is a subpackage of x.y, which is a subpackage of x. • we called package x, java, javax etc, top-level packages. • java find the location of top-level packages from a list of directory given by a system variable called CLASSPATH. • Ex: If CLASSPATH = .;d:\my\root Then • package x is the dir d:\my\root\x or ./x • package x.y is the dir d:\my\root\x\y or ./x/y • package x.y.z is the dir d:\my\root\x\y\z or ./x/y/z • class or src .y.z.A is the file d:\my\root\x\y\z\A.class or d:\my\root\x\y\z\A.java or …

More Related