4.17k likes | 4.43k Views
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
E N D
Java Programming Daher Thabit DaherChief 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) • Threading • Java Applets • I/O Streams
What is Java ? • Primarily, Java is a programming language, an object-oriented language developed at Sun Microsystems.
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.
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
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
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
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
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
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.
Why Java ? • Simple • Object-oriented • Byte-code Interpreted • Secure • Multithreaded • Garbage Collected • Portable • High-performance
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
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
Portablility / Platform Independence • “write once; run anywhere” continued
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
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)
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
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.
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)
The Java Platform • The Java Virtual Machine (Java VM) • The Java Application Programming Interface (Java API)
Java Program Structure • Basic syntax of a Java class: <modifier> class <name> { <attribute_declaration> <constractor_declaration> <method_declaration> }
HelloWorld.java class HelloWorld { public static void main (String args []) { System.out.println (“Hello World “); } }
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”; }
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; }}
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.
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 */
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 .
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
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.*;
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
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
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
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)
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;
Variables • Variable Declaration int myInteger; • Variable Initialization myInteger = 10; int count = 0; • Final Variables (constants) final int aFinalVariable = 0;
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
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
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;
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
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:
Expressions count++; Order of evaluation may matter x * y * z // Order is unimportant x + y / 100 // Order is important here
Branching • Same as in C: • if() • switch() • conditional expression ? :
The if/else Statements if (expression) { statement(s) } if (response == OK) { // code to perform OK action } else { // code to perform Cancel action }
The switch Statement • The syntax of the switch statement is: switch (expression) { case value1: statement-list1 case value2: statement-list2 case … }