Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
64 views

Java Technology: Prepared By: Ustaza/ Hiba Hassan

This document provides an outline and introduction to a lecture on Java technology. It covers 10 topics that will be discussed including introduction to Java, programming fundamentals, object-oriented programming, graphical user interfaces, and threads. It describes how Java works by using a Java virtual machine to compile code into bytecode that can run on any system with a Java interpreter. It also discusses Java features like being simple, robust, architecture neutral, and secure code execution through class loading and bytecode verification.

Uploaded by

divine serpent
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Java Technology: Prepared By: Ustaza/ Hiba Hassan

This document provides an outline and introduction to a lecture on Java technology. It covers 10 topics that will be discussed including introduction to Java, programming fundamentals, object-oriented programming, graphical user interfaces, and threads. It describes how Java works by using a Java virtual machine to compile code into bytecode that can run on any system with a Java interpreter. It also discusses Java features like being simple, robust, architecture neutral, and secure code execution through class loading and bytecode verification.

Uploaded by

divine serpent
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Java Technology

Lecture 1 Prepared by: Ustaza/ Hiba Hassan

Lectures Outline
1- Introduction
2- Java Programming Fundamentals
- Identifiers & keywords - Control Structures.

3- Object oriented programming - Overloading


- Polymorphism - Inheritance

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.

How Java Works

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

Java source code

compiler

JVM Byte code

How Java Works (cont.)

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.

Class Loader performs the following tasks:


responsible for loading all classes needed for the Java program. adds security by separating the namespaces for the classes of the local file system from those that are imported from network sources

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

Editing, Compiling & Execution

Free downloads are available on


http://java.sun.com

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!); } }

Editing, Compiling & Execution (cont.)

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

Editing, Compiling & Execution (cont.)


To execute in Textpad, click on run java application. Using Notepad, we use the command java in the MSDos prompt, i.e.
Java Hello

There are 2 categories of Java programs,


Standalone applications,
runs on its own.

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.

Programming Styles (cont.)

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.

Programming Basics (cont.)

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.

Some Primitive Types

Programming Basics (cont.)

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

Programming Basics (cont.)

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.)

Increment & Decrement Operators

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;

It must be the first statement in a source code file.

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)

The JOptionpane class is in the javax.swing package, to import it, we type,


import javax.swing.JOptionPane; or import javax.swing.*; To demonstrate try the following example:

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

Comments can be written in 3 different styles,


1- /* A multi line

comment .*/
2- // A single line comment. 3- /** A multi line

java doc comment.*/

You might also like