1 / 416

Java Programming

Java Programming. Daher Thabit Daher Chief Architect E-mail: daher@rss.gov.jo. Contents. Getting Started Variables, Data Types, Operators, & Expressions Objects Data Structure Inheritance & Polymorphism Exceptions Java Database Connectivity (JDBC) T hread ing Java Applets

callie
Télécharger la présentation

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. Java Programming Daher Thabit DaherChief Architect E-mail: daher@rss.gov.jo

  2. Contents • Getting Started • Variables, Data Types, Operators, & Expressions • Objects Data Structure • Inheritance & Polymorphism • Exceptions • Java Database Connectivity (JDBC) • Threading • Java Applets • I/O Streams

  3. What is Java ? • Primarily, Java is a programming language, an object-oriented language developed at Sun Microsystems.

  4. Some History • In 1991, Sun Microsystems set up a research project to develop a language for programming ‘intelligent’ consumer electronics • e.g. video recorders, TVs • The language was called Oak (later changed to Java). Developed by James Gosling, and others.

  5. August 1993: the project was cancelled. • The Web became popular during 1993. • July 1994: Sun restarted work on Java as a Web programming language • Java contains networking features, platform portability, and a small runtime system • Java released May 1995 • Netscape supported Java in Navigator 2.0, • May 1996: JDK v.1.0 released. continued

  6. February 1997: JDK v.1.1 released • major changes in the event model used by the GUI; inner classes introduced • December 1998: JDK v.1.2 released • also known as Java 2 • much improved GUIs (SWING), graphics • September 2000: JSDK v.1.3 released • improved networking, sound, security • see http://java.sun.com/j2se/1.3/ docs/relnotes/features.html

  7. Java, JDK, JSDK, JRE • There are several 'Java' names: • Java is the name of the language • JDK is the old name for JSDK • JSDK = "Java Software Development Kit" • JSDK 1.3 • JSDK 1.4 is available now (October 2001) continued

  8. JSDK contains all the libraries (packages), compiler, and other tools for writing/running/debugging Java code. • JRE = "Java Runtime System" • a cut-down version of JSDK with only the packages/tools needed for running Java code • most often used by Web browsers

  9. JSDK Installation • The current JSDK (v.1.3), its documentation, and excellent tutorials are here at: ftp://ftp.coe.psu.ac.th/pub/java/ • The filenames are: • j2sdk1_3_0-win.exe • j2sdk-1_3_0-update1-doc.zip • sun-java-tutorial.zip JSDK 1.3 for Windows continued

  10. Update Autoexec.bat • Add the bin path for Java to the PATH environment variable: set PATH=%PATH%;c:\jdk1.3.0_02\bin • This says where the Java tools (e.g. javac) are located.

  11. Why Java ? • Simple • Object-oriented • Byte-code Interpreted • Secure • Multithreaded • Garbage Collected • Portable • High-performance

  12. Java Advantages • Productivity • object orientation (reuse) • standard, large libraries (packages) • Java Beans (component model) • Simpler/safer than C, C++ • no pointer arithmetic, automatic garbage collection, array bounds checking, etc. continued

  13. GUI features • the event model • the SWING and Abstract Windowing Toolkit (AWT) packages • Multimedia • graphics, animations, audio, video, etc. • Network support • communication with other machines/apps • variety and standards: • sockets, RMI, CORBA • Multithreading / concurrency • can run several ‘threads’ at once continued

  14. Portablility / Platform Independence • “write once; run anywhere” continued

  15. Supports native code • can integrate legacy code • Java Development Kit (JDK) is free • Good programming environments • Visual Age ,Symantec Visual Cafe, Borland’s Jbuilder,Forte for Java,WSAD4.0-5.0 • do not use them when first learning Java • Good for teaching computing • many important ideas in one place • It’s new! continued

  16. Java Disadvantages • Java’s security restrictions makes some code hard to write: • cannot “see” much of a local machine • Slow Internet connections • makes it difficult to download medium/large applets • Lots to learn • Java language (small) and Java libraries(very large)

  17. Writing Java code • There are two kinds of Java program: • Java applications • ordinary programs; standalone • Java applets • run in a browser (or appletviewer) • attached to Web pages, so can be downloaded easily from anywhere • applets have access to browser features

  18. Java Bytecodes • The Java compiler (javac) generates bytecodes • a set of instructions similar to machine code • not specific to any machine architecture • A class file (holding bytecodes) can be run on any machine which has a Java runtime environment.

  19. The Bytecode Advantage Java runtime (Pentium) javac (Pentium) Java runtime (Mac) javac (Mac) Java bytecode(.class file) Java code(.java file) Java runtime (UNIX) javac (UNIX)

  20. The Java Platform • The Java Virtual Machine (Java VM) • The Java Application Programming Interface (Java API)

  21. Java Program Structure • Basic syntax of a Java class: <modifier> class <name> { <attribute_declaration> <constractor_declaration> <method_declaration> }

  22. HelloWorld.java class HelloWorld { public static void main (String args []) { System.out.println (“Hello World “); } }

  23. Declaring Attributes <modifier> <type> <name> [=<default_value>]; type:byte-short-int-long-char-float-double,boolean . • Examples: Public class DeclarAttribute { public int x; private float y =1000.0; private String name =“Daher Thabit”; }

  24. Declaring Methods <modifier> <return_type> <name>(<parameter>){ statements; } Parameter : (parameter_type parameter_name) Examples: public class Thing { private int x; public int getX(){ return x; } public void setX(int new_x){ x = new_x; }}

  25. Access Control • Variables and methods can be at one of four access levels: • public, protected,default,or private. • Classes can be at the public or default.

  26. Comments • Comments in a program are also called inline documentation • They should be included to explain the purpose of the program and describe processing steps • Java comments can take two forms: // comment runs to the end of the line /* comment runs to terminating symbol, even across line breaks */

  27. The Java API The Java Application Programmer Interface (API) is a collection of classes that can be used as needed • The println and print methods are part of the Java API; they are not part of the Java language itself Both methods print information to the screen; the difference is that println moves to the next line when done,but print does not .

  28. Class Libraries • The Java API is a class library, a group of classes that support program development • Classes in a class hierarchy are often related by inheritance • The classes in the Java API is separated into packages • The System class, for example, is in package java.lang • Each package contains a set of classes that relate in some way

  29. Import Statment • Basic syntax of the package statement: Import <pkg_name>.<sub_pkg_name>.<class_name>; Examples: import java.util.list; import java.io.*; import java.sql.*;

  30. The Java API Packages • Some packages in the Java API: java.applet java.awt java.beans java.io java.lang java.math java.net java.rmi java.security java.sql java.text java.util

  31. String Concatenation and Addition • The + operator serves two purposes • When applied to two strings, they are combined into one (string concatenation) • When applied to a string and some other value (like a number), that value is converted to a string and they are concatenated • When applied to two numeric types, they are added together arithmetically

  32. Command Line Arguments • Names.java • The main method accepts extra information on the command line when a program is executed > java Names Daher • Each extra value is called command line argument • In Java, command line arguments are always read as a list of character strings

  33. Variables Variable (Way) • Primitive type (byte,short,int,long,char,float,double,boolean) • Reference type (String,array,…..) Variable (Place) • inside a method (local) • outside a method –member variable (global)

  34. Data Types

  35. Data Types: Examples public static void main (String args[]) { int count; String title; boolean isAsleep; . . . } You can give variables initial values: int numShoes, mySize, myAge = 33; String myName = ”ali”; boolean amTired = true; int a = 4, b = 5, c = 6;

  36. Variables • Variable Declaration int myInteger; • Variable Initialization myInteger = 10; int count = 0; • Final Variables (constants) final int aFinalVariable = 0;

  37. Operators • Unary operators, ex. ++ • prefix or postfix notation operator operand or operand operator • Binary operators, ex. = • infix notation operand1 operator operand2 • Tertiary operator ?: • infix notation expression ? Operand1 : operand2

  38. The Conditional Operator • Java has a conditional operator that evaluates a boolean condition that determines which of two expressions is evaluated • The result of the chosen expression is the result of the entire conditional operator • Its syntax is: condition ? expression1 : expression2 • If the condition is true, expression1 is evaluated; if it is false, expression2 is evaluated • It is similar to an if-else statement, except that it is an expression that returns a value

  39. Arithmetic Operators

  40. The Increment and Decrement Operators • The increment and decrement operators can be applied in prefix (before the variable) or postfix (after the variable) form • When used alone in a statement, the prefix and postfix forms are basically equivalent. That is, count++; is equivalent to ++count;

  41. Example If count currently contains 45, then total = count++; assigns 45 to total and 46 to count If count currently contains 45, then total = ++count; assigns the value 46 to both total and count

  42. Relational Operators

  43. Bitwise Operators

  44. Assignment Operators

  45. Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y Assignment Operators • There are many such assignment operators, always written as op= , such as:

  46. Expressions count++; Order of evaluation may matter x * y * z // Order is unimportant x + y / 100 // Order is important here

  47. Operator Precedence

  48. Branching • Same as in C: • if() • switch() • conditional expression ? :

  49. The if/else Statements if (expression) { statement(s) } if (response == OK) { // code to perform OK action } else { // code to perform Cancel action }

  50. The switch Statement • The syntax of the switch statement is: switch (expression) { case value1: statement-list1 case value2: statement-list2 case … }

More Related