48
Mobile Website Development Overview of Java Facilitated by: Michael Wakahe Tawi Commercial Services Ltd Jul 2011

Overview of Java

  • Upload
    tawi123

  • View
    206

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Overview of Java

Mobile Website DevelopmentOverview of Java

Facilitated by:Michael WakaheTawi Commercial Services LtdJul 2011

Page 2: Overview of Java

Table of Contents

Introduction

Data Types and Operators

Program Control Statements

Methods, Classes & Objects

Other Topics

ExerciseCopyright © Tawi Commercial Services Ltd. 2015. All Rights

Reserved.

Page 3: Overview of Java

Introduction

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 4: Overview of Java

Introduction

Originally developed by James Gosling at Sun

Microsystems - 1991

Derives much of its syntax from C and C++

Applications are typically compiled to

bytecode (class file) that can run on any Java

Virtual Machine (JVM) Copyright © Tawi Commercial Services Ltd. 2015. All Rights

Reserved.

Page 5: Overview of Java

Introduction

Is a general-purpose, concurrent, class-based,

object-oriented language

“Write once, run anywhere"

Current stable release: Java Standard Edition 6

(1.6.0)

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 6: Overview of Java

Introduction

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 7: Overview of Java

Introduction

Can be broken down into:

Java Card

Micro Edition (ME)

Standard Edition (SE)

Enterprise Edition (EE)

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 8: Overview of Java

Introduction/*

This is a simple Java program.Call this file Example.java. Compile and run in Eclipse.

*/class Example {

// A Java program begins with a call to main().public static void main(String args[]) {

System.out.println("Java drives the Web.");}

}

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 9: Overview of Java

Introduction/*This demonstrates a variable.Call this file Example2.java. Compile and run in Eclipse.*/class Example2 {

public static void main(String args[]) {int var1; // this declares a variableint var2; // this declares another variablevar1 = 1024; // this assigns 1024 to var1System.out.println("var1 contains " + var1);var2 = var1 / 2;System.out.print("var2 contains var1 / 2: ");System.out.println(var2);

}}

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 10: Overview of Java

Data Types and

Operators

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 11: Overview of Java

Data Types and Operators

• Contains 2 general categories of built-in data

types: object-oriented and non-object

oriented.

• There are eight primitives

• Primitive means these types are not objects

but rather normal binary valuesCopyright © Tawi Commercial Services Ltd. 2015. All Rights

Reserved.

Page 12: Overview of Java

Data Types and OperatorsType Meaning

boolean Represents true/false values

byte 8-bit integer

char Character

double Double-precision floating point

float Single-precision floating point

int Integer

long Long integer

short Short integer

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 13: Overview of Java

Data Types and Operators

• Contains 2 general categories of built-in data

types: object-oriented and non-object

oriented.

• There are eight primitives

• Primitive means these types are not objects

but rather normal binary valuesCopyright © Tawi Commercial Services Ltd. 2015. All Rights

Reserved.

Page 14: Overview of Java

Data Types and Operators

• An operator is a symbol that tells the compiler

to perform a specific mathematical or logical

manipulation.

• Java has four general classes of operators:

arithmetic, bitwise, relational, and logical.

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 15: Overview of Java

Data Types and Operators

Arithmentic operator examples:

+ implies addition

/ implies division

% implies modulus

++ implies increment

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 16: Overview of Java

Data Types and Operators

Relational refers to the relationships that values can have with one another

Examples include:

== implies Equal to

!= implies Not Equal to

> implies Greater than

<= implies Less than or Equal toCopyright © Tawi Commercial Services Ltd. 2015. All Rights

Reserved.

Page 17: Overview of Java

Data Types and Operators

Logical refers to the ways in which true and false values can be connected together

Examples include:

& implies AND

| implies OR

! implies NOT

&& implies Short-circuit ANDCopyright © Tawi Commercial Services Ltd. 2015. All Rights

Reserved.

Page 18: Overview of Java

Data Types and Operators

The outcome of the relational and logical

operators is a boolean value.

The Assignment operator: var = expression;

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 19: Overview of Java

Data Types and Operators

• Approximately 50 keywords are currently

defined in the Java language

• Examples: enum, true, false, null, import, do,

break, for, int

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 20: Overview of Java

Data Types and Operators

An identifier is a name given to a method, a

variable, or any other user-defined item Identifiers can be from one to several

characters long Variable names may start with any letter of

the alphabet, an underscore, or a dollar sign.

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 21: Overview of Java

Program Control

Statements

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 22: Overview of Java

Program Control Statements

• The if statement

• if(condition) statement;

• Example:

if(10 < 11) System.out.println("10 is less than 11");

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 23: Overview of Java

Program Control Statements

The general form of the if, using blocks of statements, is:if(condition){statement sequence}else{statement sequence}

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 24: Overview of Java

Program Control Statements

• A common programming construct that is based upon the nested if is the if-else-if ladder.

if(condition)statement;

else if(condition)statement;

else if(condition)statement;

...elsestatement;

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 25: Overview of Java

Program Control Statements

• The for Loop:

• for(initialization; condition; iteration) statement;

• Example

for(count = 0; count < 5; count = count+1)

System.out.println("This is count: " + count);

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 26: Overview of Java

Program Control Statements

• The Switch statement: switch(expression) {

case constant1:statement sequencebreak;

case constant2:statement sequencebreak;

case constant3:statement sequencebreak;

...default:

statement sequence}

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 27: Overview of Java

Program Control Statements

int i = …

switch(i) {case 0:

System.out.println("i is zero");break;

case 1:System.out.println("i is one");break;

case 2:System.out.println("i is two");break; break;

default:System.out.println("i is three or more");

}

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 28: Overview of Java

Program Control Statements

The general form of the while loop is: while(condition) statement;

Example:// print the alphabet using a while loopchar ch;ch = 'a';while(ch <= 'z') {

System.out.print(ch);ch++;

}

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 29: Overview of Java

Program Control Statements

• The general form of the do-while loop is

do {statements;

} while(condition);

• Very similar to while loop

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 30: Overview of Java

Methods, Classes & Objects

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 31: Overview of Java

Classes & Objects

Java’s basic unit of encapsulation is the class

A class defines the form of an object

It specifies both the data and the code that

will operate on that data

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 32: Overview of Java

Classes & Objects

Java uses a class specification to construct

objects

Objects are instances of a class.

Thus, a class is essentially a set of plans that

specify how to build an object

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 33: Overview of Java

Classes & Objects• The general form of a class definition:class classname {

// declare instance variablestype var1;type var2;// ...type varN;// declare methodstype method1(parameters) {

// body of method}type method2(parameters) {

// body of method}// ...type methodN(parameters) {

// body of method}

}

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 34: Overview of Java

Classes & Objects

How objects are declared:

Classname referenceName = new

Classname(arguments); The general form of a method is:

ret-type name( parameter-list ) {// body of method

}

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 35: Overview of Java

Classes & Objects

A constructor initializes an object when it is created.• A simple example that uses a constructor:

// A simple constructor.class MyClass {

int x;MyClass() {

x = 10;}

}

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 36: Overview of Java

Other Topics

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 37: Overview of Java

Other Topics

• Casting: A cast is an instruction to the

compiler to convert one type into another.

• A cast has this general form:

(target-type) expression

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 38: Overview of Java

Other Topics

• Inheritance is the process by which one object

can acquire the properties of another object.

• Use of hierarchies.

• The inheritance mechanism that makes it

possible for one object to be a specific

instance of a more general caseCopyright © Tawi Commercial Services Ltd. 2015. All Rights

Reserved.

Page 39: Overview of Java

Other Topics

Polymorphism - “one interface, multiple

methods.”

This means that it is possible to design a

generic interface to a group of related

activities.

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 40: Overview of Java

Other Topics

• Packages are groups of related classes.

• Packages help organize your code and provide another layer of encapsulation.

• An interface defines a set of methods that will be implemented by a class.

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 41: Overview of Java

Other Topics

An interface does not, itself, implement any

method.

It is a purely logical construct.

Packages and interfaces give you greater

control over the organization of your program

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 42: Overview of Java

Other Topics

An exception is an error that occurs at run

time.

Using Java’s exception handling subsystem you

can, in a structured and controlled manner,

handle run-time errors.

Use try-catch and ThrowableCopyright © Tawi Commercial Services Ltd. 2015. All Rights

Reserved.

Page 43: Overview of Java

Other Topics Java has built-in support for multithreaded

programming. A multithreaded program contains two or more

parts that can run concurrently. Each part of such a program is called a thread,

and each thread defines a separate path of execution.

Thus, multithreading is a specialized form of multitasking.

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 44: Overview of Java

Exercise

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 45: Overview of Java

Exercise

• Program to simulate a School

• Attributes of school include:

– Name

– Location

• School has Teacher and Students

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 46: Overview of Java

Exercise

• Teacher has:

– First & Last Name

– Course taught

– Years employed

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 47: Overview of Java

Exercise

• Student has:

– First & Last Name

– 5 subjects with grades

– Can get average grade

– Has a fee balace

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.

Page 48: Overview of Java

The End

Michael [email protected]+254 (0)20 239 3052

www.tawi.mobi

Copyright © Tawi Commercial Services Ltd. 2015. All Rights Reserved.