Java Technology: Prepared By: Ustaza/ Hiba Hassan
Java Technology: Prepared By: Ustaza/ Hiba Hassan
Lectures Outline
1- Introduction
2- Java Programming Fundamentals
- Identifiers & keywords - Control Structures.
Cont.
4- Graphical User Interface (GUI) 5- Event Handling 6- Exception Handling 7- Java Input/ Output 8- Swing Components 9- Threads 10- Intro to Network programming.
Introduction
History The rapid growth of the internet presented a huge demand for interactive and network programming. In early 1990s Sun Microsystems developed a simple, object oriented language which they named Java. It was based on networking concepts making it easy to use. It is a high-level language like FORTRAN, COBOL, Pascal and Basic. Its syntax is based on C & C++.
Javas Features
Simple, object-oriented & familiar. Robust and secure. Architecture neutral and portable. High performance. Interpreted, threaded & dynamic. Simple; by omitting pointers & memory management. Robust; by being simple, many programming errors are avoided. Also it provides exception handling that is used to handle errors. Architecture neutral; a compiled Java program can run on different processors with different operating systems. Threads; allow multitasking, i.e. many programs run in parallel.
Java compiler translates the high-level language (program) into low-level m/c instructions by using Java Virtual Machine (JVM). The resulting output is called byte code. A java byte code interpreter is used to execute the program.
Interpreter Processor 1 Interpreter Processor 2
compiler
Regardless of the type of computer the program was compiled on, any computer with a Java interpreter can execute the compiled Java code. Eliminating the need for a specific hardware, make JVM technique invaluable for networking. Definitions
Compiler: translates a program from 1 language to another. Interpreter: is a program that executes the code.
Code Security
Java technology applications are typically general-purpose programs that run on any machine where the Java runtime environment (JRE) is installed. Code security is attained in Java through the implementation of its Java Runtime Environment (JRE). JRE runs code compiled for a JVM and performs several tasks:
Cont.
class loading (through the class loader), code verification (through the bytecode verifier) and finally code execution.
Cont.
After loading all the classes, the memory layout of the executable is then determined. This adds protection against unauthorized access to restricted areas of the code since the memory layout is determined during runtime
Bytecode verifier tests the format of the code fragments and checks the code fragments for illegal code that can violate access rights to objects
There are many versions of Java, each has its own tools. The most familiar is platform 2, standard edition. It is available in a Java Development Kit (JDK) 1.1 -1.6. Editors may be Notepad, Textpad, Jbuilder etc. Example: Hello.java
/*Displays Hello World*/ public class Hello { public static void main(String [] args) { System.out.println(Hello World!); } }
Notes: The program should be the same name as the class with an extension of .java, i.e. Hello.java. Java is case sensitive. If no errors are found , the result will be the byte code program Hello.class. To compile it, using textpad you just click on compile java application. Using Notepad, we locate its directory in the MSDos prompt and type in it the command javac, i.e.
javac Hello.java
Applets
Embedded on web pages. Viewed in a browser or by using Appletviewer.
Programming Techniques
There are 2 kinds of programming techniques. They are program-driven & Event-driven. Java supports both of them. Program-driven applications
Executes code in a step-by-step method. Transforms i/p into o/p without user intervention. Typically uses character mode, i.e. no graphical interface. Example Hello.java
Programming Styles
Event-driven applications
Wait for an event to spur an action. User intervention invokes a programmed response. Typically have a GUI (Graphical User Interface).
There are 2 programming styles, and they are both supported by Java. They are,
Procedural programming, & Object-oriented programming.
Procedural programming:
Focuses on procedures needed to solve the problem. Separate functions and data. Must check with many cases to deal with different types of data.
Cont.
Object-oriented programming
Combines data and operations into an object that has its own attributes (states) and behavior. Leave it to objects to provide the correct operation of their type. Example, a bicycle may have attributes such as current gear, number of gears, two wheelsetc. It could have behaviors such as changing gear, slowing down, acceleratingetc.
Programming Basics
Identifiers:
Identifiers are used to name java language entities, i.e. variables, methodsetc. It starts with a Unicode letter, underscore character(_) or dollar sign ($), followed by those characters & the nos. 0-9. However, the dollar sign ($) is intended for use by compiler generated identifiers, so we should avoid using it. Identifiers are case-sensitive.
Keywords:
Are reserved & can not be used as identifiers. A list is given in the next slide.
Primitive types:
There are 8 primitive types; boolean, byte, char, short, int, long, double, float.
Initialization:
By declaring a variable, a memory location is reserved for it. Example : int size; size Every memory location hold unknown values, called Garbage. To get rid of them we initialize a variable in its declaration, e.g. int size=100; size 100
Output:
There are 2 forms of standard o/p statement,
System.out.print System.out.println
System.out.print (Hello) will print Hello leaving the cursor on the same line. System.out.println (World!) will cause the cursor to move to the left of the next line. Hello World!
Cont.
Arithmetic Expressions:
Most should be familiar from C & C++. The next example illustrates the precedence rules of arithmetic operators in Java.
Example: Precedence.java
// Illustrate precedence rules for arithmetic operators. public class Precedence { public static void main (String [ ] args) { int a=3, b=4, c=5, noParen, sameParen, changeParen; noParen = a + 7 *b; sameParen = a + (7*b); changeParen = (a+7)*b; System.out.println("noParen = " + noParen + " sameParen = " + sameParen + " changeParen = " + changeParen); noParen = c/a + 4; sameParen = (c/a) + 4; changeParen = c / (a + 4);
Example (cont.)
System.out.println("noParen = " + noParen + " sameParen = " + sameParen + " changeParen = " + changeParen); noParen = c - a % b - a; sameParen = (c - (a%b)) - a; changeParen = (c-a) % (b - a); System.out.println("noParen = " + noParen + " sameParen = " + sameParen + " changeParen = " + changeParen); } }
Example (cont.)
Methods
A method contains code for an operation. Every standalone program should have a main() method. main() is the first method executed by Java interpreter. The main() method must be declared as public, static & void. Public means that anyone can use it.
Methods (cont.)
Static means that this method, main(), is part of a class and not an object. Void means that it returns no values. Main() has one argument- an array of string arguments defined as (String [] args). Example: ArgsTest.java
class ArgsTest { public static void main(String [] args) { for(int i=0; i<args.length; i++) {
Cont.
System.out.println(args[+i]=+args[i]); } } }
Notice how java accesses command-line arguments. Methods in java are similar to functions in other languages. However, it must be declared inside a class. A class may have more than one method.
Packages
Java programs declares classes and interfaces. Classes define variables, methodsetc. Interfaces define a collection of methods that are implemented by classes. Java classes and interfaces are organized into packages. Which means that packages correspond to directories in the computer file system.
Packages (cont.)
It also means that classes & interfaces with the same name may be created as long as each one is stored in a different package. To identify a package, we use the package statement;
package packageName;
Cont.
To import classes & interfaces from other packages, there are 3 forms of import statements, they are,
import packageName.className; import packageName.interfaceName; import packageName.*;
To illustrate how to use a package, assume that we want to enter 3 nos. into a pop up window and get their sum.
Packages (cont.)
showInputDialog method will pop up a window, however, it is defined in a class called JOptionPane. So to call it we type,
JOptionPane.showInputDialog(Enter the first number)
Example
/* Uses a Swing input dialog to input values * from the keyboard. Outputs the sum of the * three inputs. */ import javax.swing.*; public class ReadInteger { public static void main(String[] args) { int x, y, z, result; String input; input = JOptionPane.showInputDialog("Enter the first number");
Cont.
x = Integer.parseInt(input); input = JOptionPane.showInputDialog("Enter the second number"); y = Integer.parseInt(input); input = JOptionPane.showInputDialog("Enter the third number"); z = Integer.parseInt(input); result = x + y + z; JOptionPane.showMessageDialog(null,"The result is " + result); System.exit(0); } }
Example O/P
Comments
comment .*/
2- // A single line comment. 3- /** A multi line