0% found this document useful (0 votes)
5 views

Lecture 02

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)
5 views

Lecture 02

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/ 32

Introduction to Java: Extended

Swakkhar Shatabda

CSI 211: Object Oriented Programming, Summer 2016


Department of Computer Science and Engineering
United International University

United International University CSI 211: Object Oriented Programming 1


Arrays
Java arrays are references
Java arrays know their size
Java checks bounds when accessed
Java multi-dimensional arrays need not to be rectangular
Java arrays are initialized
Java arrays are dynamic

int myArray[]; // this is only a declaration - no allocation is done


myArray = new int[12]; // dynamic memory allocation

Obtaining an array is a two-step process.


1 First, you must declare a variable of the desired array type.
2 Second, you must allocate the memory that will hold the array,
using new, and assign it to the array variable.
United International University CSI 211: Object Oriented Programming 2
Arrays and References

Arrays are references


Garbage collection reclaims arrays that can not be accessed!
References are initialized to null
When done with a reference set it to null

Memory Leaks
In C/C++, dynamically allocated (using new/malloc)
arrays/objects are required to be released (using free/delete) to
avoid memory leaks. In java, this is done by garbage collection!
(programmers heaven/hell?)

United International University CSI 211: Object Oriented Programming 3


ArrayTestOne

United International University CSI 211: Object Oriented Programming 4


ArrayTestTwo

United International University CSI 211: Object Oriented Programming 5


Illustrated Example

Created by Java Visualizer: goo.gl/5CevSJ

United International University CSI 211: Object Oriented Programming 6


Illustrated Example

Created by Java Visualizer: goo.gl/5CevSJ

United International University CSI 211: Object Oriented Programming 7


Illustrated Example

Created by Java Visualizer: goo.gl/5CevSJ

United International University CSI 211: Object Oriented Programming 8


Illustrated Example

Created by Java Visualizer: goo.gl/5CevSJ

United International University CSI 211: Object Oriented Programming 9


Illustrated Example

Created by Java Visualizer: goo.gl/5CevSJ

United International University CSI 211: Object Oriented Programming 10


Illustrated Example

Created by Java Visualizer: goo.gl/5CevSJ

United International University CSI 211: Object Oriented Programming 11


Illustrated Example

Created by Java Visualizer: goo.gl/5CevSJ

United International University CSI 211: Object Oriented Programming 12


Illustrated Example

Created by Java Visualizer: goo.gl/5CevSJ

United International University CSI 211: Object Oriented Programming 13


Illustrated Example

Created by Java Visualizer: goo.gl/5CevSJ

United International University CSI 211: Object Oriented Programming 14


Dangling Pointers

Dangling pointer and wild pointers in computer programming are


pointers that do not point to a valid object of the appropriate type.

Two or more pointers point to the same memory on the heap


Using one of the pointers the memory is deallocated
Second pointer can still access the memory
Estimates are that up to 40% of development time in C/C++
are spent dealing with these two problems

Java Solution
No pointers
No explicit deallocation of memory
When memory can no longer be accessed, garbage collection
eventually reclaims the memory
United International University CSI 211: Object Oriented Programming 15
Array Initialization

All array elements are given an initial value that depend on the type

Array Initializers
One can initialize arrays to a list of values
The syntax is slightly different for declaration statements and
assignments
There is no way to indicate repeated values in the initializer

United International University CSI 211: Object Oriented Programming 16


Array Initialization

Wrong way to do!


double []trouble;
trouble = {1.2,4.5,6.7}; // compile error

United International University CSI 211: Object Oriented Programming 17


Multi-Dimensional Arrays

United International University CSI 211: Object Oriented Programming 18


Multi-Dimensional Arrays

Illustration created by Java Visualizer http://goo.gl/NGPRce


United International University CSI 211: Object Oriented Programming 19
API for Arrays

United International University CSI 211: Object Oriented Programming 20


String Class

United International University CSI 211: Object Oriented Programming 21


String Tokenizer

United International University CSI 211: Object Oriented Programming 22


Arguments to main()

United International University CSI 211: Object Oriented Programming 23


Control Statements: For each ...

United International University CSI 211: Object Oriented Programming 24


Control Statements: switch

United International University CSI 211: Object Oriented Programming 25


Method Overloading

United International University CSI 211: Object Oriented Programming 26


Final Variable

A final variable is a variable which has been initialized to a


fixed value which cannot be changed after initialization.
In older programming languages, a final variable would be
called a constant

Example
final static int zero = 0;

United International University CSI 211: Object Oriented Programming 27


Swing - An Introduction!

United International University CSI 211: Object Oriented Programming 28


Swing - An Introduction!

United International University CSI 211: Object Oriented Programming 29


Swing - An Introduction!

United International University CSI 211: Object Oriented Programming 30


Reading

Java How to Program: Chapter 6


Java The complete reference: Chapter 3,4

United International University CSI 211: Object Oriented Programming 31


Thats it!

Thank you

United International University CSI 211: Object Oriented Programming 32

You might also like