[go: up one dir, main page]

0% found this document useful (0 votes)
23 views111 pages

c02 JSE Intro Syntax Arrays OOP Jlib DevOps Docker

The document discusses Java arrays, including how to define, declare, create, initialize, print, sum, find the largest element, randomly shuffle, shift elements, and copy arrays in Java. It also covers using the enhanced for-each loop to traverse arrays.

Uploaded by

catanoiuandrei22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views111 pages

c02 JSE Intro Syntax Arrays OOP Jlib DevOps Docker

The document discusses Java arrays, including how to define, declare, create, initialize, print, sum, find the largest element, randomly shuffle, shift elements, and copy arrays in Java. It also covers using the enhanced for-each loop to traverse arrays.

Uploaded by

catanoiuandrei22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 111

Lecture 2

Java SE – Programming

presentation
Java Programming – Software App Development
Assoc. Prof. Cristian Toma Ph.D.

D.I.C.E/D.E.I.C – Department of Economic Informatics & Cybernetics


www.dice.ase.ro
cristian.toma@ie.ase.ro – Business Card
Agenda for Lecture 2 – Summary of JSE

1 2 3
Java Standard
Arrays
Java OOP
analogy with
C++ (cont.)
Advanced SW
Eng. & DevOps
Topics
1
Java Standard Edition – Uni- & Multi- dimensional Arrays

Java Arrays
1.1 Java Arrays – defining and usage
Opening Problem
ASSIGNMENT 03 - Read one hundred numbers, compute their
average, and find out how many numbers are above the average.
double[] myList = new double[10];

Array is a data myList reference


myList[0] 5.6
structure that myList[1] 4.5
represents a Array reference myList[2] 3.3
collection of the variable
myList[3] 13.2
same types of myList[4] 4
data. Array element at
myList[5] 34.33 Element value
index 5
myList[6] 34

myList[7] 45.45

myList[8] 99.993

myList[9] 11123

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Declaring Array Variables
• datatype[] arrayRefVar;
Example: double[] myList;
• datatype arrayRefVar[]; // This style is allowed, but not preferred
Example: double myList[];

Creating Arrays
arrayRefVar = new datatype[arraySize];

Example:
myList = new double[10];

myList[0] references the first element in the array.


myList[9] references the last element in the array.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Declaring, creating, initializing Using the Shorthand Notation
double[] myList = {1.9, 2.9, 3.4, 3.5};
This shorthand notation is equivalent to the following statements:
double[] myList = new double[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
This shorthand syntax must be in one statement.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Printing arrays
for (int i = 0; i < myList.length; i++) {
System.out.print(myList[i] + " ");
}
Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
Finding the largest element
double max = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) max = myList[i];
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Random shuffling
for (i nt i = 0; i < myList .length ; i++) { m y List
// G enerat e an in dex j r andomly i [0]
int index = ( int) (Math.r andom() [1 ]
* myList .length );
. sw a p
// S wap my List[i] with m yList[j ] .
doub le tem p = myL ist[i]; .
myLi st[i] = myLis t[index ]; [in de x]
myLi st[ind ex] = t emp;
A ran d om in de x
}

Shifting Elements
double temp = myList[0]; // Retain the first element

// Shift elements left myList


for (int i = 1; i < myList.length; i++) {
myList[i - 1] = myList[i];
}

// Move the first element to fill in the last position


myList[myList.length - 1] = temp;

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Enhanced for Loop (for-each loop)
JDK 1.5 introduced a new for loop that enables you to traverse the complete array
sequentially without using an index variable. For example, the following code
displays all elements in the array myList:

for (double value : myList)


System.out.println(value);

In general, the syntax is

for (elementType value : arrayRefVar) {


// Process the value
}

You still have to use an index variable if you wish to traverse the array in a
different order or change the elements in the array.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Copying Arrays
Often, in a program, you need to duplicate an array or a part of an
array. In such cases you could attempt to use the assignment
statement (=), as follows:

list2 = list1;
Before the assignment After the assignment
list2 = list1; list2 = list1;

list1 list1
Contents Contents
of list1 of list1

list2 list2
Contents Contents
of list2 of list2
Garbage

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Copying Arrays
Using a loop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new int[sourceArray.length];

for (int i = 0; i < sourceArrays.length; i++)


targetArray[i] = sourceArray[i];

The arraycopy Utility


arraycopy(sourceArray, src_pos, targetArray, tar_pos,
length);

Example:
System.arraycopy(sourceArray, 0, targetArray, 0,
sourceArray.length);
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Passing Arrays to Methods
public static void printArray(int[] array)
{
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}

Invoke the method

int[] list = {3, 1, 2, 6, 4, 2};


printArray(list);

Invoke the method


printArray(new int[]{3, 1, 2, 6, 4, 2});

Anonymous array
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Pass By Value
Java uses pass by value to pass arguments to a method. There
are important differences between passing a value of variables
of primitive data types and passing arrays.

• For a parameter of a primitive type value, the actual value is


passed. Changing the value of the local parameter inside the
method does not affect the value of the variable outside the
method.

• For a parameter of an array (or reference) type, the value of


the parameter contains a reference to an array / object; this
reference is passed to the method. Any changes to the array /
object that occur inside the method body will affect the
original array / object that was passed as the argument.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Simple Example – Pass by Value
public class Test {
public static void main(String[] args) {
int x = 1; // x represents an int value
int[] y = new int[10]; // y represents an array of int
values

m(x, y); // Invoke m with arguments x and y

System.out.println("x is " + x);


System.out.println("y[0] is " + y[0]);
}

public static void m(int number, int[] numbers) {


number = 1001; // Assign a new value to number
numbers[0] = 5555; // Assign a new value to numbers[0]
}
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Call Stack – Pass by Value
Stack Heap
Space required for
method m
int[] numbers:reference
The arrays are
int number: 1 0 stored in a
0 heap.
Space required for the
main method
int[] y: reference Array of
int x: 1 0 ten int
values is

When invoking m(x, y), the values of x and y are


passed to number and numbers. Since y contains the
reference value to the array, numbers now contains
the same reference value to the same array.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Call Stack – Pass by Value
Stack Heap
Space required for
method m
int[] numbers:reference
The arrays are
int number: 1001 5555 stored in a
0 heap.
Space required for the
main method
int[] y: reference Array of ten int
int x: 1 values is stored here
0

When invoking m(x, y), the values of x and y are


passed to number and numbers. Since y contains the
reference value to the array, numbers now contains
the same reference value to the same array.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Heap – Pass by Value
Heap

The arrays are


5555 stored in a
0 heap.
Space required for the
main method
int[] y: reference
int x: 1 0

The JVM stores the array in an area of memory,


called heap, which is used for dynamic memory
allocation where blocks of memory are allocated and
freed in an arbitrary order.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Returning an Array from a Method
public static int[] reverse(int[] list) {
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
} list

return result;
} result

int[] list1 = new int[]{1, 2, 3, 4, 5, 6};


int[] list2 = reverse(list1);

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
Declare result and create array
public static int[] reverse(int[] list) {
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 0 0 0 0 0 0

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 0 and j = 5
public static int[] reverse(int[] list) {
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 0 0 0 0 0 0

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (= 0) is less than 6
public static int[] reverse(int[] list) {
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 0 0 0 0 0 0

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 0 and j = 5
public static int[] reverse(int[] list) { Assign list[0] to result[5]
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 0 0 0 0 0 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);

After this, i becomes 1 and j


public static int[] reverse(int[] list) { becomes 4
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 0 0 0 0 0 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.5 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);

i (=1) is less than 6


public static int[] reverse(int[] list) {
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 0 0 0 0 0 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 1 and j = 4
public static int[] reverse(int[] list) { Assign list[1] to result[4]
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 0 0 0 0 2 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 2 and
public static int[] reverse(int[] list) { j becomes 3
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 0 0 0 0 2 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (=2) is still less than 6
public static int[] reverse(int[] list) {
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 0 0 0 0 2 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 2 and j = 3
public static int[] reverse(int[] list) { Assign list[i] to result[j]
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 0 0 0 3 2 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 3 and
public static int[] reverse(int[] list) { j becomes 2
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 0 0 0 3 2 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (=3) is still less than 6
public static int[] reverse(int[] list) {
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 0 0 0 3 2 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 3 and j = 2
public static int[] reverse(int[] list) { Assign list[i] to result[j]
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 0 0 4 3 2 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 4 and
public static int[] reverse(int[] list) { j becomes 1
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 0 0 4 3 2 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (=4) is still less than 6
public static int[] reverse(int[] list) {
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 0 0 4 3 2 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 4 and j = 1
public static int[] reverse(int[] list) { Assign list[i] to result[j]
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 0 5 4 3 2 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 5 and
public static int[] reverse(int[] list) { j becomes 0
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 0 5 4 3 2 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (=5) is still less than 6
public static int[] reverse(int[] list) {
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 0 5 4 3 2 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 5 and j = 0
public static int[] reverse(int[] list) { Assign list[i] to result[j]
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 6 5 4 3 2 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 6 and
public static int[] reverse(int[] list) { j becomes -1
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 6 5 4 3 2 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (=6) < 6 is false. So exit the
public static int[] reverse(int[] list) { loop.
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6

result 6 5 4 3 2 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
Return result
public static int[] reverse(int[] list) {
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

list 1 2 3 4 5 6
list2

result 6 5 4 3 2 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays

Searching Arrays
Searching is the process of looking for a specific element in an array;
for example, discovering whether a certain score is included in a list of
scores. Searching is a common task in computer programming. There
are many algorithms and data structures devoted to searching. In this
section, two commonly used approaches are discussed, linear search
and binary search.
public class LinearSearch {
/** The method for finding a key in the list */
public static int linearSearch(int[] list, int key) {
for (int i = 0; i < list.length; i++)
if (key == list[i]) [0] [1] [2] …
return i; list
return -1;
} key Compare key with list[i] for i = 0, 1, …
}

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays

Linear Search
The linear search approach compares the key
element, key, sequentially with each element
in the array list. The method continues to do
so until the key matches an element in the list
or the list is exhausted without a match being
found. If a match is made, the linear search
returns the index of the element in the array
that matches the key. If no match is found, the
search returns -1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays
Linear Search Animation
Key List

3 6 4 1 9 7 3 2 8

3 6 4 1 9 7 3 2 8

3 6 4 1 9 7 3 2 8

3 6 4 1 9 7 3 2 8

3 6 4 1 9 7 3 2 8

3 6 4 1 9 7 3 2 8
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays
From Idea to Solution – Linear Search
/** The method for finding a key in the list */
public static int linearSearch(int[] list, int key) {
for (int i = 0; i < list.length; i++)
if (key == list[i])
return i;
return -1;
}

Trace the method


int[] list = {1, 4, 4, 2, 5, -3, 6, 2};
int i = linearSearch(list, 4); // returns 1
int j = linearSearch(list, -4); // returns -1
int k = linearSearch(list, -3); // returns 5
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays

Binary Search
For binary search to work, the elements in the
array must already be ordered (SORTED).
Without loss of generality, assume that the array
is in ascending order.

e.g., 2 4 7 10 11 45 50 59 60 66 69 70 79

The binary search first compares the key with the


element in the middle of the array.
For objects / references a Comparable<E>
interface must be implemented in class E.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays

Binary Search, cont.


Consider the following three cases:
§ If the key is less than the middle element,
you only need to search the key in the first
half of the array.
§ If the key is equal to the middle element,
the search ends with a match.
§ If the key is greater than the middle
element, you only need to search the key in
the second half of the array.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays

Binary Search

Key List

8 1 2 3 4 6 7 8 9

8 1 2 3 4 6 7 8 9

8 1 2 3 4 6 7 8 9

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays

Binary Search, cont.


key is 11 low mid high

key < 50 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
list 2 4 7 10 11 45 50 59 60 66 69 70 79
low mid high

[0] [1] [2] [3] [4] [5]


key > 7 list 2 4 7 10 11 45

low mid high

[3] [4] [5]


key == 11 list 10 11 45

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays

Binary Search, cont.


key is 54 low mid high

key > 50 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
list 2 4 7 10 11 45 50 59 60 66 69 70 79
low mid high

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
key < 66 list 59 60 66 69 70 79

low mid high

[7] [8]
key < 59 list 59 60

low high

[6] [7] [8]


59 60
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays

Binary Search, cont.


The binarySearch method returns the index of the
element in the list that matches the search key if it
is contained in the list. Otherwise, it returns

-insertion point - 1.

The insertion point is the point at which the key


would be inserted into the list.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays

From Idea to Solution – Binary Search


/** Use binary search to find the key in the list */
public static int binarySearch(int[] list, int key) {
int low = 0;
int high = list.length - 1;

while (high >= low) {


int mid = (low + high) / 2;
if (key < list[mid])
high = mid - 1;
else if (key == list[mid])
return mid;
else
low = mid + 1;
}

return -1 - low;
}

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays
The Arrays.binarySearch Method
Since binary search is frequently used in programming, Java provides several
overloaded binarySearch methods for searching a key in an array of int, double,
char, short, long, and float in the java.util.Arrays class. For example, the following
code searches the keys in an array of numbers and an array of characters.

int[] list = {2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79};
System.out.println("Index is " +
java.util.Arrays.binarySearch(list, 11));
Return is 4
char[] chars = {'a', 'c', 'g', 'x', 'y', 'z'};
System.out.println("Index is " +
java.util.Arrays.binarySearch(chars, 't'));
Return is –4 (insertion point is
3, so return is -3-1)

For the binarySearch method to work, the array must be pre-sorted in increasing
order.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays

Sorting Arrays
Sorting, like searching, is also a common task in
computer programming. Many different algorithms
have been developed for sorting. Mainly are used
two simple, intuitive sorting algorithms: selection
sort and insertion sort.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Selection Sort
Selection sort finds the largest number in the list and places it last. It then finds the largest number
remaining and places it next to last, and so on until the list contains only a single number. Figure
shows how to sort the list {2, 9, 5, 4, 8, 1, 6} using selection sort.
swap

Select 1 (the smallest) and swap it 2 9 5 4 8 1 6


with 2 (the first) in the list
swap
The number 1 is now in the
Select 2 (the smallest) and swap it 1 9 5 4 8 2 6 correct position and thus no
with 9 (the first) in the remaining longer needs to be considered.
list swap

The number 2 is now in the


Select 4 (the smallest) and swap it 1 2 5 4 8 9 6 correct position and thus no
with 5 (the first) in the remaining longer needs to be considered.
list
The number 6 is now in the
5 is the smallest and in the right 1 2 4 5 8 9 6 correct position and thus no
position. No swap is necessary longer needs to be considered.
swap

The number 5 is now in the


Select 6 (the smallest) and swap it 1 2 4 5 8 9 6 correct position and thus no
with 8 (the first) in the remaining longer needs to be considered.
list swap

The number 6 is now in the


Select 8 (the smallest) and swap it 1 2 4 5 6 9 8 correct position and thus no
with 9 (the first) in the remaining longer needs to be considered.
list

The number 8 is now in the


Since there is only one element 1 2 4 5 6 8 9 correct position and thus no
remaining in the list, sort is longer needs to be considered.
completed
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Selection Sort
From Idea to Solution - Selection Sort
for (int i = 0; i < list.length; i++)
{
select the smallest element in list[i..listSize-1];
swap the smallest with list[i], if necessary;
// list[i] is in its correct position.
// The next iteration apply on list[i..listSize-1]
}
list[0] list[1] list[2] list[3] ... list[10]

list[0] list[1] list[2] list[3] ... list[10]

list[0] list[1] list[2] list[3] ... list[10]

list[0] list[1] list[2] list[3] ... list[10]

list[0] list[1] list[2] list[3] ... list[10]

...

list[0] list[1] list[2] list[3] ... list[10]


Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.5 Java Arrays – Selection Sort
for (int i = 0; i < listSize; i++)
{
select the smallest element in list[i..listSize-1];
swap the smallest with list[i], if necessary;
// list[i] is in its correct position.
// The next iteration apply on list[i..listSize-1]
}
Expand
double currentMin = list[i];
int currentMinIndex = i;
for (int j = i; j < list.length; j++) {
if (currentMin > list[j]) {
currentMin = list[j];
currentMinIndex = j;
}
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Selection Sort
for (int i = 0; i < listSize; i++)
{
select the smallest element in list[i..listSize-1];
swap the smallest with list[i], if necessary;
// list[i] is in its correct position.
// The next iteration apply on list[i..listSize-1]
}
Expand
double currentMin = list[i];
int currentMinIndex = i;
for (int j = i; j < list.length; j++) {
if (currentMin > list[j]) {
currentMin = list[j];
currentMinIndex = j;
}
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Selection Sort
for (int i = 0; i < listSize; i++)
{
select the smallest element in list[i..listSize-1];
swap the smallest with list[i], if necessary;
// list[i] is in its correct position.
// The next iteration apply on list[i..listSize-1]
}
Expand
if (currentMinIndex != i) {
list[currentMinIndex] = list[i];
list[i] = currentMin;
}

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Selection Sort – Wrap it in a Method
/** The method for sorting the numbers */
public static void selectionSort(double[] list) {
for (int i = 0; i < list.length; i++) {
// Find the minimum in the list[i..list.length-1]
double currentMin = list[i];
int currentMinIndex = i;
for (int j = i + 1; j < list.length; j++) {
if (currentMin > list[j]) {
currentMin = list[j];
currentMinIndex = j;
}
}
// Swap list[i] with list[currentMinIndex] if necessary;
if (currentMinIndex != i) {
list[currentMinIndex] = list[i];
list[i] = currentMin; Invoke it
}
} selectionSort(yourList)
}

60
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Sort

The Arrays.sort Method


Since sorting is frequently used in programming, Java provides several
overloaded sort methods for sorting an array of int, double, char, short,
long, and float in the java.util.Arrays class. For example, the following
code sorts an array of numbers and an array of characters.

double[] numbers = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5};


java.util.Arrays.sort(numbers);

char[] chars = {'a', 'A', '4', 'F', 'D', 'P'};


java.util.Arrays.sort(chars);

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.2 Java Arrays – Multidimensional Arrays

Motivations: Thus far, you have used one-dimensional arrays to


model linear collections of elements. You can use a two-
dimensional array to represent a matrix or a table. For example,
the following table that describes the distances between the cities
can be represented using a two-dimensional array.
Distance Table (in miles)

Chicago Boston New York Atlanta Miami Dallas Houston

Chicago 0 983 787 714 1375 967 1087


Boston 983 0 214 1102 1763 1723 1842
New York 787 214 0 888 1549 1548 1627
Atlanta 714 1102 888 0 661 781 810
Miami 1375 1763 1549 661 0 1426 1187
Dallas 967 1723 1548 781 1426 0 239
Houston 1087 1842 1627 810 1187 239 0
1723 1548 781 1426 0 239
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/07slide.ppt
1.2 Java Arrays – Multidimensional Arrays

Declare/Create Two-dimensional Arrays


// Declare array ref var
dataType[][] refVar;

// Create array and assign its reference to variable


refVar = new dataType[10][10];

// Combine declaration and creation in one statement


dataType[][] refVar = new dataType[10][10];

// Alternative syntax
dataType refVar[][] = new dataType[10][10];

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/07slide.ppt
1.2 Java Arrays – Multidimensional Arrays

Two-dimensional Array Illustration


0 1 2 3 4 0 1 2 3 4 0 1 2
0 0 0 1 2 3

1 1 1 4 5 6

2 2 7 2 7 8 9

3 3 3 10 11 12

4 4 int[][] array = {
{1, 2, 3},
matrix = new int[5][5]; matrix[2][1] = 7; {4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};

matrix.length? 5 array.length? 4
matrix[0].length? 5 array[0].length? 3
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/07slide.ppt
1.2 Java Arrays – Multidimensional Arrays

Declaring, Creating, and Initializing Using Shorthand Notations

You can also use an array initializer to declare, create and


initialize a two-dimensional array. For example,

int[][] array = { int[][] array = new int[4][3];


{1, 2, 3}, array[0][0] = 1; array[0][1] = 2; array[0][2] = 3;
{4, 5, 6}, Same as array[1][0] = 4; array[1][1] = 5; array[1][2] = 6;
{7, 8, 9}, array[2][0] = 7; array[2][1] = 8; array[2][2] = 9;
{10, 11, 12} array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;
};

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/07slide.ppt
1.2 Java Arrays – Multidimensional Arrays

Lengths of Two-dimensional Arrays


int[][] x = new int[3][4];

x
x[0][0] x[0][1] x[0][2] x[0][3] x[0].length is 4
x[0]
x[1] x[1][0] x[1][1] x[1][2] x[1][3] x[1].length is 4

x[2]
x[2][0] x[2][1] x[2][2] x[2][3] x[2].length is 4
x.length is 3

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/07slide.ppt
1.2 Java Arrays – Multidimensional Arrays

Ragged Arrays
Each row in a two-dimensional array is itself an array. So,
the rows can have different lengths. Such an array is
known as a ragged array. For example,
int[][] matrix = {
{1, 2, 3, 4, 5},
{2, 3, 4, 5},
matrix.length is 5
{3, 4, 5}, matrix[0].length is 5
matrix[1].length is 4
{4, 5}, matrix[2].length is 3
{5} matrix[3].length is 2
matrix[4].length is 1
};
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/07slide.ppt
1.2 Java Arrays – Multidimensional Arrays

Ragged Arrays

int[][] triangleArray = { 1 2 3 4 5
{1, 2, 3, 4, 5},
{2, 3, 4, 5}, 2 3 4 5
{3, 4, 5},
{4, 5}, 3 4 5
{5}
}; 4 5

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/07slide.ppt
Section Conclusion
Fact: Java is natural
In few samples it is simple to remember:
selections, loops, methods, arrays – it is like
in C/C++ syntax.
2
Java OOP

Java OOP & JSE


2. Summary of Java SE - OOP
Analogy with C++ and it will be reloaded in next lecture

What is a class?

What is a package of classes in Java?

What is an object / instance?

How many bytes has an object inside JVM?

Why do we need clone, equals and hash methods?

Demo & memory model for the Certificate Java class


2. Summary of Java SE - OOP
DEMO: Java Arrays demo and other samples + Java Libraries: JARs +
Java 9 or 11 MODULES
Section Conclusions
Object Oriented Programming in Java – class,
object, object’s deep vs. shallow copy, interface,
interface as type, abstract class, inheritance,
polymorphism.

Class is used to encapsulate attributes (fields |


features | characteristics) and methods
(behavior | ‘interface’) in order to produce
instances / objects.

Object is a instance of a class with certain values


Object Oriented Programming for attributes and with same methods class for
for easy Java sharing all the generated instances.

In OOP there are two major relationships: “has a”


(composition) and “is a” (inheritance)
3
Repositories: Git, Build Automation: ANT / Maven / Gradle, DevOps Continous Build
Integration: Jenkins, Deployment in Cloud: Docker vs. IaaS VMs

Advanced SW Eng. & DevOps Topics


3.1 Java Library
What is a Java library?

What are the advantages and disadvantages of Java libraries?

How can be solved in Java multiple dependencies or inclusions of the same class in the compilation
phase? How were solved these problems in C/C++?

How should be created a Java library and how should be used – command line vs. IDE?

>jar -cvf archive_name.jar files_names_to_compress


>javac -classpath .:archieve_name.jar *.java
>java –classpath .:archive_name.jar file_with_main_class
3.1 JDK 9 and 11+ Modules
Java 9 Modules:
Java SE 9: Jigsaw Project
Jigsaw project is going to introduce completely new concept of Java SE 9: Java Module System.
It is very big and prestigious project from Oracle Corp in Java SE 9 release. Initially they have
started this project as part of Java SE 7 Release. However with huge changes, it’s postponed to
Java SE 8 then again postponed. Now it has been released with Java SE 9 in 2017.
Main Goals of Jigsaw Project:
• The Modular JDK - As we know, Current JDK system is too big. So they have decided to
divide JDK itself into small modules to get a number of benefits (We will discuss them soon
in the coming sections).
• Modular Source Code - Current source code jar files are too big, especially rt.jar is too big
right. So they are going to divide Java Source code into smaller modules.
• Modular Run-Time Images - The main goal of this Feature is “Restructure the JDK and JRE
run-time images to accommodate modules”.
• Encapsulate Most Internal APIs - The main goal of this feature is “Make most of the JDK’s
internal APIs inaccessible by default but leave a few critical, widely-used internal APIs
accessible”.
• Java Platform Module System - The main goal of this Feature is “Allowing the user to create
their modules to develop their applications”.
• jlink: The Java Linker - The main goal of this jlink Tool is “Allowing the user to create
executable to their applications”.
Copyright: https://www.journaldev.com/13106/java-9-modules
3.1 JDK 9 and 11+ Modules
Java 9 Modules:
Problems of Current Java 8 System?
In this section, we will discuss about “Why we need Java SE 9 Module System” that means the
problems of Current Java System.
Java SE 8 or earlier systems have following problems in developing or delivering Java Based
applications.
• As JDK is too big, it is bit tough to scale down to small devices. Java SE 8 has introduced 3
types of compact profiles to solve this problem: compact1, compact2 and compact3. But it
does not solve this problem.
• JAR files like rt.jar etc are too big to use in small devices and applications.
• As JDK is too big, our applications or devices are not able to support better Performance.
• There is no Strong Encapsulation in the current Java System because “public” access
modifier is too open. Everyone can access it.
• As JDK, JRE is too big, it is hard to Test and Maintain applications.
• As public is too open, They are not to avoid the accessing of some Internal Non-Critical APIs
like sun.*, *.internal.* etc.
• As User can access Internal APIs too, Security is also big issue.
• Application is too big.
• Its a bit tough to support Less Coupling between components.

Copyright: https://www.journaldev.com/13106/java-9-modules
3.1 JDK 9 and 11+ Modules
Java 9 Modules:

Advantages of Java SE 9 Module System

Java SE 9 Module System is going to provide the following benefits:

• As Java SE 9 is going to divide JDK, JRE, JARs etc, into smaller modules, we can use whatever
modules we want. So it is very easy to scale down the Java Application to Small devices.
• Ease of Testing and Maintainability.
• Supports better Performance.
• As public is not just public, it supports very Strong Encapsulation. (Don’t worry its a big
concept. we will explore it with some useful examples soon).
• We cannot access Internal Non-Critical APIs anymore.
• Modules can hide unwanted and internal details very safely, we can get better Security.
• Application is too small because we can use only what ever modules we want.
• Its easy to support Less Coupling between components.
• Its easy to support Single Responsibility Principle (SRP).

Copyright: https://www.journaldev.com/13106/java-9-modules
3.1 JDK 9 and 11+ Modules
Java 9 Modules:
Compare JDK 8 and JDK 9
We know what a JDK software contains. After installing JDK 8 software, we can see couple of
directories like bin, jre, lib etc in Java Home folder. However Oracle Corp / OpenJDK has
changed this folder structure a bit differently as shown below.

Copyright: https://www.journaldev.com/13106/java-9-modules
3.1 JDK 9 and 11+ Modules
Java 9 Modules - Compare JDK 8 and JDK 9 Directory Structure:
3.1 JDK 9 and 11+ Modules
Java 9 Modules – SDK Distro:

Here JDK 9 does NOT contain JRE.

In JDK 9, JRE is separated into a


separate distribution folder. JDK 9
software contains a new folder
“jmods”. As of today, “jmods”
contains 97 modules. The “jmods”
folder is available at
${JAVA_HOME}/jmods. These are
known as JDK Modules.
Copyright: https://www.journaldev.com/13106/java-9-modules
3.1 JDK 9 and 11+ Modules
Java 9 Modules:
What is Java 9 Module?
A Module is a self-describing collection of Code, Data and some Resources. It is a set of related
Packages, Types (classes, abstract classes, interfaces etc) with Code & Data and Resources.
Each Module contains only a set of related code and data to support Single Responsibility
(Functionality) Principle (SRP).
The main goal of Java 9 Module System is to
support:
Modular Programming in Java.
In Simple Terms,
Module = Code + Data
Module Descriptor (“module-info.java”) is a
one of the Resources in Java 9 Module.
As of now, Java 9 Module System has 97 modules in Early Access JDK. Oracle Corp has separated JDK jars
and Java SE Specifications into two set of Modules.
• All JDK Modules starts with “jdk.*”
• All Java SE Specifications Modules starts with “java.*”
Java 9 Module System has a “java.base” Module. It’s known as Base Module. It’s an Independent module
and does NOT dependent on any other modules. By default, all other Modules dependent on this module.
That’s why “java.base” Module is also known as the root of the Java 9 Modules.
It’s default module for all JDK Modules and User-Defined Modules.
Copyright: https://www.journaldev.com/13106/java-9-modules
3.1 JDK 9 and 11+ Modules
Java 9 Modules:
Compare Java 8 and Java 9 Applications
We have already developed many Java applications
using Java 5, 6,7, or 8. We know how a Java 8 or
earlier applications looks like and what it contains.

In a Java 8 or earlier applications, Top level component a Package. It groups a set related of types into a group. It also contains a set of resources.
Java 9 Applications does not have much difference with this. It just introduced a new component called “Module”, which is used to group a set of related
Packages into a group. And one more new component that Module Descriptor (“module-info.java”). That’s it.
Like Java 8 applications have Packages as a Top level components, Java 9 applications have Module as Top Level components.
NOTE: -Each Java 9 Module have one and only one Module and one Module Descriptor. Unlike Java 8 Packages, We cannot create multiple modules into a
single Module. In brief we can say a Java 9 Module contains the following main components:
• One Module + Module Name
• Module Descriptor
• Set of Packages
• Set of Types and Resources
Here Resources may be module-info.java (Module Descriptor) or any other properties or XML.
Copyright: https://www.journaldev.com/13106/java-9-modules
3.1 JDK 9 and 11+ Modules
Java 9 Modules:
The two main goals of Java 9 Module System:
• Reliable Configuration
In Java 9 Module System, a Module is a First class citizen.
• Strong Encapsulation
In Simple Terms, Java has these many First class citizens:
• In OOP (Object Oriented Programming) = Object
• In FP (Functional Programming) = Function or Lambda
Java 9 Module Basics • In Module System (Modular Programming) = Module
We should remember the following important points about Java 9 Module:
• Each module has a unique Name.
• Each module has some description in a source file.
• A Module description is expressed in a source file called “module-info.java”.
• As “module-info.java” file describes a Module, it is also known as “Module Descriptor”.
• A Module Descriptor is a Java file. It is not an XML, Text file or Properties file.
• By convention, we should use same name “module-info.java” for Module Descriptor.
• By convention, Module Descriptor file is placed in the top level directory of a Module.
• Each Module can have any number of Packages and Types.
• As of now, year 2017, JDK 9 have 97 modules.
• We can create our own modules.
• One Module can dependent on any number of modules.
• Each Module should have one and only one Module Descriptor (“module-info.java”).
Using Java SE 9, we develop Modules that means we can do Modular Programming in Java.
Copyright: https://www.journaldev.com/13106/java-9-modules
3.1 JDK 9 and 11+ Modules
Java 9 Modules:

Steps to Develop a Java 9 Module

Follow these steps one by one to develop and test the “HelloWorld” Module.
• Create Module name folder, for example “com.hello”.
• Create Module Packages, for example “com.hello”.
• Create our Java component, for example “HelloWorld.java”.
• Create Module Descriptor, for example “module-info.java”.
• Define Module Description in Module Descriptor, for example “exports
com.hello;” of “module-info.java” in “com.hello” module.
• Create Module Jars if required.
• Test the modules.
• These steps are common for almost all modules development.

Copyright: https://www.journaldev.com/13106/java-9-modules | https://www.journaldev.com/13543/javase9-simple-module-cmd-prompt-part3


3.1 JDK 9 and 11+ Modules
Java 9 Modules:

Copyright: https://www.journaldev.com/13106/java-9-modules
3.1 GraalVM

GraalVM is an extension of the Java virtual machine to support more languages and execution
modes. The Graal project includes a new high performance Java compiler, itself called Graal, which
can be used in a just-in-time configuration on the HotSpot VM, or in an ahead-of-time configuration on
Copyright: https://www.graalvm.org/ the SubstrateVM.
3.1 GraalVM

Copyright: https://www.graalvm.org/
3.1 GraalVM

Copyright: https://www.graalvm.org/
3.2 Build Automation: Apache ANT
Optional steps to build the lectures with Gradle – all lectures are independent by any
Build
Automation system (e.g. Gradle, MVN, ANT, etc.) and the labs are projects in Eclipse IDE:

Apache ANT is a Java based build tool from Apache Software Foundation. Apache Ant's
build files are written in XML and they take advantage of being open standard, portable
and easy to understand.

Features:
• Ant is the most complete Java build and deployment tool available.
• Ant is platform neutral and can handle platform specific properties such as file
separators.
• Ant can be used to perform platform specific tasks such as modifying the modified
time of a file using 'touch' command.
• Ant scripts are written using plain XML. If you are already familiar with XML, you can
learn Ant pretty quickly.
• Ant is good at automating complicated repetitive tasks.
• Ant comes with a big list of predefined tasks.
• Ant provides an interface to develop custom tasks.
• Ant can be easily invoked from the command line and it can integrate with free and
commercial IDEs. Copyright: https://www.tutorialspoint.com/ant/ant_introduction.htm
3.2 Build Automation: Apache ANT

ant compile jar run Copyright: https://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html


3.2 Build Automation: Apache ANT

ant compile
ant jar
ant run Copyright: https://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html
3.2 Build Automation: Apache MVN - Maven
Optional steps to build the lectures with Gradle – all lectures are independent by any Build
Automation system (e.g. Gradle, MVN, ANT, etc.) and the labs are projects in Eclipse IDE:

Apache Maven (MVN) is a software project management and comprehension tool. Based on
the concept of a project object model (POM), Maven can manage a project's build, reporting
and documentation from a central piece of information. Using maven we can build and
manage any Java based project.

Features:

• Maven uses Convention over Configuration, which means developers are not required to
create build process themselves. Developers do not have to mention each and every
configuration detail. Maven provides sensible default behavior for projects. When a
Maven project is created, Maven creates default project structure. Developer is only
required to place files accordingly and he/she need not to define any configuration in
pom.xml.
• Simple project setup that follows best practices.
• Consistent usage across all projects.
• Dependency management including automatic updating.

Copyright: https://www.tutorialspoint.com/maven/maven_overview.htm
3.2 Build Automation: Apache MVN - Maven
Apache Maven (MVN) Features:
• A large and growing repository of libraries.
• Extensible, with the ability to easily write plugins in Java or scripting languages.
• Instant access to new features with little or no extra configuration.
• Model-based builds − Maven is able to build any number of projects into predefined
output types such as jar, war, metadata.
• Coherent site of project information − Using the same metadata as per the build process,
maven is able to generate a website and a PDF including complete documentation.
• Release management and distribution publication − Without additional configuration,
maven will integrate with your source control system such as CVS and manages the
release of a project.
• Backward Compatibility − You can easily port the multiple modules of a project into
Maven 3 from older versions of Maven. It can support the older versions also.
• Automatic parent versioning − No need to specify the parent in the sub module for
maintenance.
• Parallel builds − It analyzes the project dependency graph and enables you to build
schedule modules in parallel. Using this, you can achieve the performance improvements
of 20-50%.
• Better Error and Integrity Reporting − Maven improved error reporting, and it provides
you with a link to the Maven wiki page where you will get full description of the error.
Copyright: https://www.tutorialspoint.com/maven/maven_overview.htm | https://www.tutorialspoint.com/maven/maven_creating_project.htm
3.2 Build Automation: Apache MVN - Maven

Copyright: https://maven.apache.org/guides/getting-started/index.html | https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html


3.2 Build Automation: Apache MVN - Maven
The POM
The pom.xml file is the core of a project's configuration in Maven. It is a single configuration file
that contains the majority of information required to build a project in just the way you want.
The POM is huge and can be daunting in its complexity, but it is not necessary to understand all
of the intricacies just yet to use it effectively. This project's POM is:

mvn clean build


mvn package
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
Copyright: https://maven.apache.org/guides/getting-started/index.html | https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
3.3 Build Automation: Gradle
Optional steps to build the lectures with Gradle – all lectures are independent by any Build
Automation system (e.g. Gradle, MVN, ANT, etc.) and the labs are projects in Eclipse IDE:

Gradle is an advanced general purpose build management system based on Groovy.

Features:

• It supports automatic download and configuration of project dependencies (libraries).

• It supports Maven and Ivy (ANT) repositories for retrieving project dependencies,
allowing reuse of the artifacts of existing build systems.

• It supports multi-project and multi-artifact builds.

(Optional full Gradle presentation @ISM – Cyber-Security Master – www.ism.ase.ro,


by Marius Popa @ Oracle)

https://gradle.org/install/
https://plugins.gradle.org/
https://github.com/jabedhasan21/java-hello-world-with-gradle/blob/master/README.md
https://www.tutorialspoint.com/gradle/index.htm
3.3.1. Gradle Intro and Basics
Projects and tasks in Gradle:

• A Gradle build consists of one or more projects.

• A project using Gradle describes its build via a build.gradle file. build.gradle file is located in the
root folder of the project. build.gradle file is based on a Domain Specific Language (DSL) written
in Groovy. task hello {
doLast {
println 'Hello Gradle'
• build.gradle build file defines a project and its tasks. }
println 'Project name: ' + rootProject.name

• A task represents a piece of work which a build performs.


3.3.2. Installing and configuring Gradle

• Requirement: JDK (Java Development Kit) installation.

• Installing: Download the latest release of Gradle from http://gradle.org/gradle-


download/ | https://gradle.org/install/ for the usage on the command line. Add
the /bin folder to PATH environment variable.

• Gradle daemon: avoid starting the Java virtual machine for every build. Usage:
org.gradle.daemon=true in gradle.properties in the ${HOME}/.gradle; Executing
gradle with the --daemon parameter on the command line or gradle --stop to stop it.

• Set specific JVM options: GRADLE_OPTS environment variable. Eg. export


GRADLE_OPTS=-Xmx1024m defines 1 GB as maximum heap size.
3.3.3. Gradle plug-ins

• Plug-ins extend Gradle core functionality. Extension to Gradle adding some


preconfigured tasks.

• Gradle ships with a number of plug-ins (java), and custom plug-ins can be developed.

• Adding a plug-in in build.gradle file: apply plugin: 'pluginname' (Eg. apply plugin:
‘java’).

• Registry for Gradle plug-ins: https://plugins.gradle.org/.


3.3.3. Gradle plug-ins
build.gradle
task hello {
doLast {
println 'Hello Gradle'
println 'Project name: ' + rootProject.name
}
}

build.gradle

apply plugin: 'java'


task hello {
doLast {
println 'Hello Gradle'
println 'Project name: ' + rootProject.name
}
}
3.3.4. Gradle dependency management
• Managing the classpath of Gradle projects: adding JAR files, directories or other
projects to the build path of the application.

• Automatic download of Java library dependencies, specifying the dependency in


Gradle build file.

• A Java library is identified by Gradle via its project’s groupId:artifactId:version (also


known as GAV in Maven).

• Adding a dependency: new entry in the dependency section in build.gradle file.

repositories {
jcenter()
}

dependencies {
compile fileTree(dir: 'libs',
include: ['*.jar'])
testCompile 'junit:junit:4.12'
}
3.3.5. Gradle tasks
• Default Gradle tasks: tasks for introspection of Gradle itself (eg. task tasks).

• Custom Gradle tasks: tasks created by developers (eg. task hello). Running the gradle
tasks task, the hello task will be listed under Other tasks. Tasks without a group are
considered as private tasks. Groups can be applied with the group property and a
description can be applied by using the description property .
task hello {

group 'greetings'
description 'The hello task greets Gradle by
saying "Hello Gradle" and the root project name'

doLast {
println 'Hello Gradle'
println 'Project name: ' + rootProject.name
}
}
3.3.6. Building Java projects

• Java plug-in: provides tasks to compile Java source code, run unit tests, create Javadoc and
create a JAR file.
sourceSets {
• Default project layout: main {
java {
- Java source code: src/main/java. srcDir 'src'
}
- Java tests: src/test/java. }
test {
• Different project structure: sourceSets. java {
srcDir 'test'
}
}
}

• Start the build: gradle build for HelloWorld.java.


3.4 Jenkins/Hudson – DevOps Build Automation

export JAVA_HOME=/opt/software/java/jdks/jdk1.8.0_161
export CLASSPATH=.:$JAVA_HOME/jre/lib
export CATALINA_HOME=/opt/software/apache-tomcat-9.0.4
export
PATH=.:$JAVA_HOME/bin:$CATALINA_HOME/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/
bin:/sbin:/bin:/usr/games

cd $CATALINA_HOME
bin/startup.sh

http://127.0.0.1:8080
http://127.0.0.1:8080/jenkins/

bin/shutdown.sh
3.4 Jenkins/Hudson – DevOps Build Automation
3.5 Deployment: Std. OS vs. VMs vs. Docker Containers
Docker image:
https://hub.docker.com | https://hub.docker.com/r/critoma/ubuntu-java-node-py-dev/
Install Docker in Linux (similar steps used for Windows):
https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/#install-from-a-package
https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/
sudo dpkg -i /path/to/package.deb
sudo docker run hello-world
docker search critoma
docker pull critoma/ubuntu-java-node-py-dev
docker run -it critoma/ubuntu-java-node-py-dev

Resources:
https://www.tutorialspoint.com/docker/docker_architecture.htm
https://www.tutorialspoint.com/docker/docker_containers_and_shells.htm
https://docs.docker.com/install/linux/docker-ce/ubuntu/
https://docs.docker.com/engine/installation/linux/linux-postinstall/
https://docs.docker.com/get-started/
https://docs.docker.com/get-started/part2/
https://docs.docker.com/get-started/part6/

https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04
https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes#purging-all-unused-or-dangling-images,-
containers,-volumes,-and-networks
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04
http://fabiorehm.com/blog/2014/09/11/running-gui-apps-with-docker/
https://github.com/fgrehm/docker-eclipse
http://fabiorehm.com/blog/2014/09/11/running-gui-apps-with-docker/
https://stackoverflow.com/questions/19234831/where-are-docker-images-stored-on-the-host-machine
3.5 Deployment: Std. OS vs. VMs vs. Docker Containers
https://hub.docker.com/r/critoma/ubuntu-java-node-py-dev/
3.5 Deployment: Std. OS vs. VMs vs. Docker Containers

Download Oracle VM VirtualBox OVA:


https://drive.google.com/drive/folders/1SI7uzNPbQ3YsFo3_sbkE3R5lf2f1psZO?usp=sharing
Download Docker Container: https://hub.docker.com/r/critoma/ubuntu-java-node-py-dev/
?
Questions & Answers!
What’s
Thanks!Your Message?

Java Application Development


End of Lecture 2 – summary of Java SE

You might also like