Mobile Software Engineering Crash Course - C02 Java Primer

Preview:

DESCRIPTION

 

Citation preview

Mobile

Software

Engineering

L02 – Java Primer

Mohammad Shaker

FIT of Damascus - AI dept.

MohammadShakerGtr@gmail.com

Mobile SE – August 2012

IDE

eclipse, netbeans, etc.

eclipse

eclipse Juno, 2012

btw, why eclipse?!

eclipse Downloadhttp://www.eclipse.org/

eclipse Download

eclipse Juno – Packages and Projects

JavaJVM – Java Virtual Machine

JavaWrite once, run anywhere

Javaeverything is an object!

Or is it?

Javaeverything is an object!

Or is it?

APIhttp://java.sun.com/javase/6/docs/api/

Tutorial http://java.sun.com/docs/books/tutorial/java/TOC.html

Variables - Naming

• Subsequent characters also can be numbers

• Case sensitive

• No spaces

• Examples:

name

firstName

phoneNumber

Reserved Words

abstract continue for new switch

assert*** default goto* package synchronized

boolean do if private this

break double implements protected throw

byte else import public throws

case enum**** instanceof return transient

catch extends int short try

char final interface static void

class finally long strictfp** volatile

const* float native super while

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html

Primitive Types

byte

short

int

long

float

double

boolean

char

Primitive Types

byte

short

int

long

float

double

boolean

char

Everything

is an

object?

Primitive Types

byte

short

int

long

float

double

boolean

char

Everything

is an

object?

Objects

String

BigDecimal

Arrays

int[] grades;

grades = new int[15];

Control flow

if (boolean) {

// perform this code

} else {

// otherwise, run this

}

Example on first step!

Car myCar = new Car();

Example on first step!

Car myCar = new Car();

Example on first step!

Car myCar = new Car();

Car yourCar = myCar;

Point loc = myCar.getLocation();

New paradigms

• Stack

• Heap

• Garbage collection

Setters and Getters

Let’s get our hands dirty

Let’s get our hands dirty

Objects

Packages vs Namespaces

void main(string[] args)

Instance vs Class methodsvoid shootBall(Point point)

static void main(string[] args)

Inheritance and Polymorphismextends, super, @Override

Some diff.protected in a form of package scope not class scope

Some diff.final vs const vs readonly

interfaces

Interfaces

extends

Collections

http://docs.oracle.com/javase/tutorial/collections/interfaces/index.html

Example

ArrayList<String> arrList = new ArrayList<String>();

arrList.add(“A”);

arrList.add(“AB”);

arrList.add(“ABC”);

arrList.add(“ABCD”);

// using indices

arrList.set(0, arrList.get(1));

arrList.set(1, “foo”);

// out of bound?

arrList.set(9, “bar”);

Example

• Suppose an ArrayList A1

• What does the following line mean?

List<String> list =

new ArrayList<String>(A1);

for-each Construct

• Looping by indices

• for-each, parallel execution

for (Object o : collection)

System.out.println(o);

Collection Operations

List<Type> list1 = new ArrayList<Type>();

// …

List<Type> list2 = new ArrayList<Type>();

// …

List<Type> list3 = new ArrayList<Type>(list1);

list3.addAll(list2);

ListAlgorithms

• sort— sorts a List using a merge sort algorithm, which provides a fast, stable sort. (A stable sort is one that does not reorder equal elements.)

• shuffle— randomly permutes the elements in a List.

• reverse— reverses the order of the elements in a List.

• rotate— rotates all the elements in a List by a specified distance.

• swap— swaps the elements at specified positions in a List.

• replaceAll— replaces all occurrences of one specified value with another.

• fill— overwrites every element in a List with the specified value.

• copy— copies the source List into the destination List.

• binarySearch— searches for an element in an ordered List using the binary search algorithm.

• indexOfSubList— returns the index of the first sublist of one List that is equal to another.

• lastIndexOfSubList— returns the index of the last sublist of one List that is equal to another.

See more @ http://docs.oracle.com/javase/tutorial/collections/interfaces/list.html

Iterators

Multithreadingsynchronized, wait,

notify, notifyall

Exception Handling, IO...

Design PatternsSingleton, Factory, Proxy, Template.. etc.

Gang of four

Implementing Singleton with Java

“live”

Thx for listening

Recommended