Top Banner
Java object oriented programming language
99

Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Jun 24, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Java object oriented

programming language

Page 2: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Bibliography

B. Eckel, Thinking in Java. 4th Edition, Prentice Hall,

2006

K. Arnold, J. Gosling, D. Holmes, The Java Programming

Language. 4th Edition, Addison-Wesley, 2005

Page 3: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Basic characteristics that represent pure approach to

object-oriented programming:

everything in an object

a program is a collection of objects which communicate

by calling methods

each object can be made up of other objects

every object has a type

An object has state, behavior and identity.

Page 4: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Comparison of Java and C++

C++ was designed for systems and applications

programming, as an extention the C programming language.

C++ has added support for statically typed object-oriented

programming, exception handling, lifetime-based resource

management (RAII), generic programming, and template

metaprogramming, in particular. It also added a standard

library which includes generic containers and algorithms

(STL), as well as many other general purpose facilities.

Page 5: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Java is a general-purpose, concurrent, class-based, object-

oriented computer programming language that is specifically

designed to have as few implementation dependencies as

possible. It relies on a java virtual machine to be secure and

highly portable. It is bundled with an extensive library

designed to provide a complete abstraction of the underlying

platform. Java is a statically typed object-oriented language

that uses similar (but incompatible) syntax to C++.

Comparison of Java and C++

Page 6: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Main differences are:

C++ Java

programming paradigm

procedural or object- oriented

object-oriented

compatibility backwards compatible,

including C

backwards compatibility with previous versions

focus execution efficiency developer productivity

efficiency machine code that runs

on the hardware bytecode that runs on

a virtual machine

freedom trusts the programmer imposes some

constraints to the programmer

inheritance multibase single rooted hierarchy

memory access

arbitrary only through objects

Page 7: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

C++ Java

memory management

manual memory deallocation

built-in garbage collection

external data and functions

yes no

boolean type no yes

primitive types

platform dependent fixed size

operators overloading immutable

preprocessor yes no

scope operator yes no

conditional expressions

integral boolean

arrays direct access size checking

Main differences are:

Page 8: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

C++ Java

goto statement

yes no, but there are labeled break and

continue

forward declarations

yes no

field initialize no yes

storage of non-primitive

objects stack or heap heap

concurrent programming

library third party standard

client/server programming

library third party standard

Main differences are:

Page 9: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

The primitive data types are:

primitive type size minimum

value maximum

value wrapped

type

boolean --- --- --- Boolean

char 16 bits Unicode 0 Unicode 216-1 Character

byte 8 bits -128 +127 Byte

short 16 bits -215 +215-1 Short

int 32 bits -231 +231-1 Integer

long 64 bits -263 +263-1 Long

float 32 bits IEEE754 IEEE754 Float

double 64 bits IEEE754 IEEE754 Double

void --- --- --- Void

Page 10: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Variable declaration:

type VarIdent;

type VarIdent1, VarIdent2;

Variable definition:

type VarIdent=value;

type VarIdent1=value1, VarIdent2=value2;

Page 11: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Constant definition:

static final type ConstIdent=value;

Page 12: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Deafult values for primitive members of a class:

primitive type default

boolean false

char ‘\u0000’ (null)

byte 0

short 0

int 0

long 0

float 0.0

double 0.0

Page 13: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Class declaration:

class ClassName {

field declarations

method definitions

}

Method definition:

ReturnType MethodName(argument list) {

method body;

}

Page 14: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Object definition:

ClassName ObjectIdent = new ClassName(argument list);

String definition:

String ObjectIdent =”SequenceOfCharacters”;

Page 15: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Invoking a method:

ObjectIdent.MethodName (argument list);

VarIndent=ObjectIdent.MethodName (argument list);

Page 16: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

There are five places where data may be stored:

registers

stack

heap

constant storage

non-RAM storage

Page 17: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Including components:

import PackageName.ClassName;

import PackageName.*;

Page 18: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Array declaration:

ClassName[] ArrayIdentC;

ClassName ArrayIdentC [];

type[] ArrayIdent;

type ArrayIdent [];

Array definition:

type[] ArrayIdent = {val1,val2, /* ... */ valn};

Page 19: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

ArrayIdent = new ClassName[size];

Array definition:

ClassName[] ArrayIdent = new ClassName[size];

ArrayIdent = new type[size];

type[] ArrayIdent = new type[size];

Page 20: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Operators:

arithmetic

relational: == != < > <= >=

logical

bitwise

assignment : =

unary: + – ++ ––

binary: + – / %

extended : += –= = /= %=

&= |= ^= <<= >>=

unary: !

binary: && ||

unary: ~

binary: & | ^ >> << >>>

Page 21: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Operators:

miscellaneous:

ternary conditional: expr1 ? expr 2 : expr3

concatenation: +

casting: (type) expression

type comparision: ObjectIdent instanceof type

object allocation: new

Page 22: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Precedence of operators:

category operators associativity

primitive () [] . left to right

unary ++ –– + – ~ ! () new right to left

multiplicative / % left to right

additive + – + left to right

shift >> << >>> left to right

relational > < >= <= instanceof left to right

equality == != left to right

bitwise AND & left to right

bitwise XOR ^ left to right

bitwise OR | left to right

logical AND && left to right

logical OR || left to right

Page 23: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Precedence of operators:

category operators associativity

conditional ? : right to left

assignment = op= right to left

comma , left to right

Page 24: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Statements:

conditional

if (Boolean-expr)

statement

if (Boolean-expr)

statement1

else

statement2

Page 25: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Statements:

selection

switch (int-selector) {

case int-value1 : statement1; [break;]

case int-value2 : statement2; [break;]

case int-value3 : statement3; [break;]

// ...

default: statement;

}

Page 26: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Statements:

iterations

while (Boolean-expr)

statement

do

statement

while (Boolean-expr);

for (initialization ; Boolean-expr ; step-statement)

statement

Page 27: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Statements:

iterations

for (type VarIdent : set-expression)

statement

Page 28: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Statements:

jumps

break;

continue;

return value;

break label;

continue label;

labels

LabelIdent :

Page 29: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Comments:

// comment

/*

comment

*/

Page 30: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

The keyword this can be used only inside a non-static

method—it produces the reference to an object that

the method has been called for.

Page 31: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

The main method must be public, static, and void

and it must accept a single argument of type String[].

Page 32: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Access specifiers:

public

package (default)

private

protected

Access specifiers are placed in front of each definition

for each member in a class, whether it is a field or

a method. Each access specifier only controls access

for that particular definition.

Page 33: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Libraries:

package PackageName;

Packages define units of software that can be distributed

independently and combined with other packages to form

applications.

Page 34: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Including components:

import PackageName.ClassName;

import PackageName.*;

Page 35: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Reusing classes:

composition

aggregation

inheritance

Page 36: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

The super keyword is available in all non-static methods

of a class. In field access and method invocation,

super acts as a reference to the current object as an

instance of its superclass.

Page 37: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

An abstract class is a class that is declared abstract—it

may or may not include abstract methods. Abstract classes

cannot be instantiated, but they can be extended.

An abstract method is a method that is declared without

an implementation:

abstract ReturnType MethodName(argument list);

abstract class ClassName { // ...

}

Page 38: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

An interface is a reference type, similar to a class, that can

contain only constants, method signatures, default

methods, static methods, and nested types.

interface InterfaceName { // ...

}

The implements keyword is used to make a class that

conforms to a particular interface.

Page 39: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Generic class:

class ClassName<T1,T2,...,Tn> {

// ...

}

Generic type invocation:

ClassName<type> ObjectIdent;

Object definition:

ClassName<type> ObjectIdent = new ClassName<type>();

As long as the compiler can determine the type arguments

from the context, the constructor of a generic class may

be invoked with an empty set of type arguments (<>).

Page 40: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

By convention, type parameter names are single,

uppercase letters.

The most commonly used type parameter names are:

E – Element

K - Key

N - Number

T - Type

V - Value

Page 41: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Generic methods are methods that introduce their own

type parameters.

Bounded type parameter:

T extends UpperBound <T>

The question mark (?), called the wildcard, represents

an unknown type.

Bounded wildcards:

<? extends T>

<? super T>

Page 42: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

A collection (sometimes called a container) is simply an

object that groups multiple elements into a single unit.

Collections are used to store, retrieve, manipulate, and

communicate aggregated data.

A collections framework is a unified architecture for

representing and manipulating collections.

All collections frameworks contain the following:

interfaces

implementations

algorithms

Page 43: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Core collection interfaces:

Page 44: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

public interface Iterator<E>

Methods:

boolean hasNext()

E next()

void remove()

Page 45: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

public interface Collection<E>

Methods summary:

boolean add(E e)

boolean addAll(Collection<? extends E> c)

void clear()

boolean contains(Object o)

boolean containsAll(Collection<?> c)

boolean equals(Object o)

int hashCode()

boolean isEmpty()

Iterator<E> iterator()

Page 46: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

boolean remove(Object o)

boolean removeAll(Collection<?> c)

boolean retainAll(Collection<?> c)

int size()

Object[] toArray()

<T> T[] toArray(T[] a)

Page 47: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

public interface Set<E>

The Set interface contains only methods inherited from

Collection and adds the restriction that duplicate

elements are prohibited.

The Java platform contains three general-purpose Set

implementations: HashSet, TreeSet, and

LinkedHashSet.

Page 48: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

public interface SortedSet<E>

Methods summary:

Comparator<? super E> comparator()

E first()

SortedSet<E> headSet(E toElement)

E last()

SortedSet<E> subSet(E fromElement, E

toElement)

SortedSet<E> tailSet(E fromElement)

Methods inherited from the Collection interface:

add, addAll, clear, contains, containsAll,

equals, hashCode, isEmpty, iterator, remove,

removeAll, retainAll, size, toArray, toArray

Page 49: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

public interface List<E>

Methods summary:

boolean add(E e)

void add(int index, E element)

boolean addAll(Collection<? extends E> c)

boolean addAll(int index, Collection<?

extends E> c)

void clear()

boolean contains(Object o)

boolean containsAll(Collection<?> c)

boolean equals(Object o)

E get(int index)

int hashCode()

int indexOf(Object o)

boolean isEmpty()

Page 50: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Iterator<E> iterator()

int lastIndexOf(Object o)

ListIterator<E> listIterator()

ListIterator<E> listIterator(int index)

E remove(int index)

boolean remove(Object o)

boolean removeAll(Collection<?> c)

boolean retainAll(Collection<?> c)

E set(int index, E element)

int size()

List<E> subList(int fromIndex, int toIndex)

Object[] toArray()

<T> T[] toArray(T[] a)

Page 51: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

The Java platform contains two general-purpose List

implementations: ArrayList and LinkedList.

Most polymorphic algorithms in the Collections class

apply specifically to List:

sort(), shuffle(), reverse(), rotate(),

swap(), replaceAll(), fill(), copy(),

binarySearch(), indexOfSubList(),

lastIndexOfSubList()

Page 52: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

public interface Queue<E>

Specific methods:

boolean add(E e)

E element()

boolean offer(E e)

E peek()

E poll()

E remove()

Methods inherited from the Collection interface:

addAll, clear, contains, containsAll, equals,

hashCode, isEmpty, iterator, remove,

removeAll, retainAll, size, toArray, toArray

Page 53: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

public interface Deque<E>

Specific methods:

boolean add(E e)

void addFirst(E e)

void addLast(E e)

boolean contains(Object o)

Iterator<E> descendingIterator()

E element()

E getFirst()

E getLast()

Iterator<E> iterator()

boolean offer(E e)

boolean offerFirst(E e)

boolean offerLast(E e)

Page 54: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

E peek()

E peekFirst()

E peekLast()

E poll()

E pollFirst()

E pollLast()

E pop()

void push(E e)

E remove()

boolean remove(Object o)

E removeFirst()

boolean removeFirstOccurrence(Object o)

E removeLast()

boolean removeLastOccurrence(Object o)

int size()

Page 55: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Methods inherited from the Collection interface:

addAll, clear, containsAll, equals, hashCode,

isEmpty, removeAll, retainAll, toArray,

Page 56: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

public interface Map<K,V>

Specific methods:

void clear()

boolean containsKey(Object key)

boolean containsValue(Object value)

Set<Map.Entry<K,V>> entrySet()

boolean equals(Object o)

V get(Object key)

int hashCode()

boolean isEmpty()

xSet<K> keySet()

V put(K key, V value)

void putAll(Map<? extends K,? extends V> m)

Page 57: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

V remove(Object key)

int size()

Collection<V> values()

The Java platform contains three general-purpose Map

implementations: HashMap, TreeMap, and

LinkedHashMap.

Page 58: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

public interface SortedMap<K,V>

Specific methods:

Comparator<? super K> comparator()

Set<Map.Entry<K,V>> entrySet()

K firstKey()

SortedMap<K,V> headMap(K toKey)

Set<K> keySet()

K lastKey()

SortedMap<K,V> subMap(K fromKey, K toKey)

SortedMap<K,V> tailMap(K fromKey)

Collection<V> values()

Methods inherited from the Map interface:

clear, containsKey, containsValue, equals,

get, hashCode, isEmpty, put, putAll, remove,

size

Page 59: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

public class Collections<E>

Methods summary:

static <T> boolean addAll(Collection<? super T>

c, T... elements)

static <T> Queue<T> asLifoQueue(Deque<T> deque)

static <T> int binarySearch(List<? extends

Comparable<? super T>> list, T key)

static <T> int binarySearch(List<? extends T>

list, T key, Comparator<? super T> c)

Fields summary:

static List EMPTY_LIST

static Map EMPTY_MAP

static Set EMPTY_SET

Page 60: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

static <E> Collection<E>

checkedCollection(Collection<E> c, Class<E>

type)

static <E> List<E> checkedList(List<E> list,

Class<E> type)

static <K,V> Map<K,V> checkedMap(Map<K,V> m,

Class<K> keyType, Class<V> valueType)

static <E> Set<E> checkedSet(Set<E> s, Class<E>

type)

static <K,V> SortedMap<K,V>

checkedSortedMap(SortedMap<K,V> m, Class<K>

keyType, Class<V> valueType)

static <E> SortedSet<E>

checkedSortedSet(SortedSet<E> s, Class<E>

type)

Page 61: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

static <T> void copy(List<? super T> dest,

List<? extends T> src)

static boolean disjoint(Collection<?> c1,

Collection<?> c2)

static <T> Enumeration<T> emptyEnumeration()

static <T> Iterator<T> emptyIterator()

static <T> List<T> emptyList()

static <T> ListIterator<T> emptyListIterator()

static <K,V> Map<K,V> emptyMap()

static <T> Set<T> emptySet()

static <T> Enumeration<T>

enumeration(Collection<T> c)

static <T> void fill(List<? super T> list,

T obj)

static int frequency(Collection<?> c, Object o)

Page 62: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

static int indexOfSubList(List<?> source,

List<?> target)

static int lastIndexOfSubList(List<?> source,

List<?> target)

static <T> ArrayList<T> list(Enumeration<T> e)

static <T extends Object & Comparable<? super

T>>

static <T> T max(Collection<? extends T> coll,

Comparator<? super T> comp)

static <T extends Object & Comparable<? super

T>>

static <T> T min(Collection<? extends T> coll,

Comparator<? super T> comp)

static <T> List<T> nCopies(int n, T o)

static <E> Set<E> newSetFromMap(Map<E,Boolean>

map)

Page 63: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

static <T> boolean replaceAll(List<T> list, T

oldVal, T newVal)

static void reverse(List<?> list)

static <T> Comparator<T> reverseOrder()

static <T> Comparator<T>

reverseOrder(Comparator<T> cmp)

static void rotate(List<?> list, int distance)

static void shuffle(List<?> list)

static void shuffle(List<?> list, Random rnd)

static <T> Set<T> singleton(T o)

static <T> List<T> singletonList(T o)

static <K,V> Map<K,V> singletonMap(K key, V

value)

static <T extends Comparable<? super T>> void

sort(List<T> list)

Page 64: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

static <T extends Comparable<? super T>> void

sort(List<T> list)

static <T> void sort(List<T> list, Comparator<?

super T> c)

static void swap(List<?> list, int i, int j)

static <T> Collection<T>

synchronizedCollection(Collection<T> c)

static <T> List<T> synchronizedList(List<T>

list)

static <K,V> Map<K,V> synchronizedMap(Map<K,V>

m)

static <T> Set<T> synchronizedSet(Set<T> s)

static <K,V> SortedMap<K,V>

synchronizedSortedMap(SortedMap<K,V> m)

static <T> SortedSet<T>

synchronizedSortedSet(SortedSet<T> s)

Page 65: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

static <T> Collection<T>

unmodifiableCollection(Collection<? extends

T> c)

static <T> List<T> unmodifiableList(List<?

extends T> list)

static <K,V> Map<K,V> unmodifiableMap(Map<?

extends K,? extends V> m)

static <T> Set<T> unmodifiableSet(Set<? extends

T> s)

static <K,V> SortedMap<K,V>

unmodifiableSortedMap(SortedMap<K,? extends

V> m)

static <T> SortedSet<T>

unmodifiableSortedSet(SortedSet<T> s)

Page 66: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Streams are ordered sequences of data that have a source

(input streams) or destination (output streams).

I/O package java.io

Page 67: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

The package java.io has two major parts:

character streams and byte streams.

Byte streams are used to perform input and output of

8-bit bytes. Each byte stream class is descended from

InputStream or OutputStream abstract classes.

Page 68: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

InputStream declares methods to read from

a particular source:

public abstract int read() throws IOException

public int read(byte[] buf, int offset,

int count) throws IOException

public int read(byte[] buf) throws

IOException

public long skip(long count) throws

IOException

public int available() throws IOException

public void close() throws IOException

Page 69: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

OutputStream provides methods for writing bytes

into a destination:

public abstract void write(int b) throws

IOException

public void write(byte[] buf) throws

IOException

public void flush() throws IOException

public void close() throws IOException

Page 70: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

The abstract classes for reading and writing streams of

characters are Reader and Writer.

Methods in Reader are:

public int read() throws IOException

public abstract int read(char[] buf, int

offset, int count) throws IOException

public int read(char[] buf) throws IOException

public int read(java.nio.CharBuffer buf)

throws IOException

public long skip(long count) throws IOException

public boolean ready() throws IOException

public abstract void close() throws IOException

Page 71: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Methods in Writer are:

public void write(int ch) throws IOException

public abstract void write(char[] buf, int

offset, int count) throws IOException

public void write(char[] buf) throws

IOException

public void write(String str, int offset, int

count) throws IOException

public void write(String str) throws

IOException

public abstract void flush() throws IOException

public abstract void close() throws IOException

Page 72: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Stream objects which implement formatted output are

instances of either PrintWriter, a character stream

class, or PrintStream, a byte stream class.

Both PrintStream and PrintWriter implement the same

set of methods for converting internal data into formatted

output. Two levels of formatting are provided:

print() and println() format individual values

in a standard way.

format() formats almost any number of values based on

a format string, with many options for precise formatting.

Page 73: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

The Java platform supports three standard streams:

Standard Input, accessed through System.in

Standard Output, accessed through System.out

Standard Error, accessed through System.err

Page 74: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

The java.io package defines several types of streams:

Filter streams

Buffered streams

Piped streams

In-memory streams allow to use in-memory data

structures as a source or destination for a stream:

ByteArray streams

CharArray streams

String streams

Page 75: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

The File streams:

FileInputStream, FileOutputStream, FileReader

and FileWriter allow to treat a file as a stream for input

or output.

The RandomAccessFile class provides a more

sophisticated file mechanism - nonsequential, random

access to a file's contents. To access a file randomly, we

need to open the file, seek a particular location, and then

read from or write to that file.

Page 76: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

public RandomAccessFile(String name, String

mode) throws FileNotFoundException

public RandomAccessFile(File file, String mode)

throws FileNotFoundException

public long getFilePointer() throws IOException

public void seek(long pos) throws IOException

public int skipBytes(int count) throws

IOException

public long length() throws IOException

public void setLength(long newLength) throws

IOException

Page 77: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

The File class:

provides several common methods which are useful with

filenames and pathnames.

Page 78: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

public long lastModified()

public long length()

public boolean renameTo(File newName)

public boolean delete()

public boolean createNewFile()

public boolean mkdir()

public boolean mkdirs()

public boolean setLastModified(long time)

public boolean setReadOnly()

public String[] list()

public String[] list(FilenameFilter filter)

public static File[] listRoots()

Page 79: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

public static File createTempFile(String

prefix, String suffix, File directory) tHRows

IOException

public static File createTempFile(String

prefix, String suffix) tHRows IOException

public void deleteOnExit()

Page 80: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

The Java.nio.file package provides high performance

support for file I/O, albeit with more complexity.

Page 81: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

An exception is is an event that occurs during the execution

of a program that disrupts the normal flow of instructions.

Creating an exception object and handing it to the runtime

system is called throwing an exception.

An ordered list of methods, known as the call stack, consists

of those methods which had been called to get to the method

where the error occurred.

A block of code that can handle the exceeption is called an

exception handler.

A chosen exception handler is said to catch the exception.

Page 82: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Three kinds of exceptions:

checked

errors

runtime exceptions

Page 83: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Exception throwing:

throw expression;

Catching exceptions:

try {

statements;

} catch (exception_type1 ident1) {

statements;

} catch (exception_type1 ident1) {

statements;

} finally {

statements;

}

Page 84: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Annotations are form of metadata; they provide information

about a program that is not part of the program itself.

Annotations have no direct effect on the operation of the

code they annotate. A key benefit of annotations is their

suitability for automatic analysis, generally via annotation

processing tools.

Annotations have a number of uses; the most important are

information for the compiler and information for the runtime

processing.

Page 85: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Annotation format:

@name (

elem1_name = value1;

elem2_name = value2;

...

)

The program elements that can be annotated are all those

for which modifiers can be specified: type declarations

(classes, interfaces, enums, and annotation types), field

declarations, method and constructor declarations, local

variable declarations, and even parameter declarations.

Page 86: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

The predefined annotation types defined in java.lang

are:

@Deprecated

@Override

@SuppressWarnings

Annotation retention policy describes the various policies

for retaining annotations:

CLASS - annotations are to be recorded in the class file by

the compiler but need not be retained by the JVM at run

time.

RUNTIME - annotations are to be recorded in the class file

by the compiler and retained by the JVM at run time, so

they may be read reflectively.

SOURCE - annotations are to be discarded by the compiler.

Page 87: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Reflection is a mechanism (also known as RTTI - runtime

type information) commonly used by programs which

require the ability to examine or modify the runtime

behavior of applications running in the JVM, by browsing

of class types and their members, and programmatic

manipulation of objects.

Page 88: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

In concurrent programming, there are two basic units of

execution: processes and threads.

A process has a self-contained execution environment. A

process generally has a complete, private set of basic run-

time resources; in particular, each process has its own

memory space.

Threads exist within a process — every process has at least

one. Threads share the process's resources, including

memory and open files.

Page 89: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Each thread is associated with an instance of the

class Thread.

An application that creates an instance of Thread must

provide the code that will run in that thread. There are

two ways to do this:

creating Thread object

using Runnable object

Page 90: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Creating Thread object:

Thread throbject = new Thread();

Page 91: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

The Thread class itself implements Runnable, though its

run method does nothing. An application can subclass

Thread, providing its own implementation of run.

In both cases Thread.start is invoked in order to start

the new thread.

The Runnable interface defines a single method, run,

meant to contain the code executed in the thread. The

Runnable object is passed to the Thread constructor.

Page 92: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Threads communicate primarily by sharing access to

fields and the objects reference fields refer to. This form

of communication can make two kinds of errors possible:

thread interference and memory consistency errors. The

tool needed to prevent these errors is synchronization.

Page 93: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

Interference happens when two operations, running in

different threads, but acting on the same data, interleave.

This means that the two operations consist of multiple

steps, and the sequences of steps overlap.

Memory consistency errors occur when different threads

have inconsistent views of what should be the same data.

Java provides two basic synchronization tools:

synchronized methods and synchronized statements.

Page 94: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

A class whose objects must be protected from interference

in a multithreaded environment usually has appropriate

methods declared synchronized.

Synchronization forces execution of the two threads to be

mutually exclusive in time.

Page 95: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

The synchronized statement enables to execute

synchronized code that acquires the lock of any object, not

just the current object, or for durations less than the entire

invocation of a method. The synchronized statement has

two parts: an object whose lock is to be acquired and a

statement to execute when the lock is obtained.

synchronized (expr) {

statements

}

Page 96: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

A Java applet is a special kind of Java program that a

browser enabled with Java technology can download from

the internet and run.

An applet is typically embedded inside a web page and runs

in the context of a browser.

An applet must be a subclass of the java.applet.Applet

class. The Applet class provides the standard interface

between the applet and the browser environment.

Page 97: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

There are some important differences between an applet

and a standalone Java application:

an applet is a Java class that extends the

java.applet.Applet class

main() method is not invoked on an applet, and an applet

class will not define main()

applets are designed to be embedded within an HTML

page (when a user views an HTML page that contains an

applet, the code for the applet is downloaded to the user's

machine)

JVM is required to view an applet; the JVM can be either

a plug-in of the Web browser or a separate runtime

environment

Page 98: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming

A socket is one end-point of a two-way communication link

between two programs running on the network.

Socket classes are used to represent the connection

between a client program and a server program.

The java.net package provides two classes:

Socket and ServerSocket which implement the client

side of the connection and the server side of the

connection, respectively.

Page 99: Java object oriented programming languagehome.agh.edu.pl › ~meszka › java_notes.pdf · Java is a general-purpose, concurrent, class-based, object-oriented computer programming