Java Tutorial 2 آرش نگهداری کیا مباحث جاوا اسلایدهای دوم.

Post on 04-Jan-2016

228 views 2 download

transcript

Java Tutorial 2

کیا نگهداری آرشجاوا مباحث

دوم اسالیدهای

Primitive Data Types

Wrapper Classes Introduction

• Why are there wrapper classes in Java?• Java is an object oriented programming language. I

think you could also ask - why do we have primitives and why is everything not just an object?

• Java designers kept the two separate to keep things simple. You use the wrappers when you need types that fit in the object oriented world - like polymorphism, collections etc. You use the primitives when you need efficiency.

Examples

• Integer intObj = new Integer (25); • Integer intObj2 = new Integer ("25"); • Integer intObj3 = new Integer ("Two"); • The third one throw run time exception

(NumberFormatException)

Some Methods for Wrapper Classes

مثالهایی نیز در اکلیپس مطرح خواهد شد

Generic Type Example

class Box <T> {   private T theObject;   public Box( T arg) { theObject = arg; }   ... } class Test {   public static void main(String[] args) {     Box <String> box = new Box <String> ("Jack");   } }

Another Example for Generic Type

List Interface

• ArrayList and LinkedList both implements List interface and their methods and results are almost identical. However there are few differences between them which make one better over another depending on the requirement.

تفاوتهای لیست آرایه ای با لینک لیست

• 1) Search: ArrayList search operation is pretty fast compared to the LinkedList search operation. get(int index) in ArrayList gives the performance of O(1) while LinkedList performance is O(n).

• Reason: ArrayList maintains index based system for its elements as it uses array data structure implicitly which makes it faster for searching an element in the list. On the other side LinkedList implements doubly linked list which requires the traversal through all the elements for searching an element.

• 2) Deletion: LinkedList remove operation gives O(1) performance while ArrayList gives variable performance: O(n) in worst case (while removing first element) and O(1) in best case (While removing last element).

• Conclusion: LinkedList element deletion is faster compared to ArrayList.

• Reason: LinkedList’s each element maintains two pointers (addresses) which points to the both neighbor elements in the list. Hence removal only requires change in the pointer location in the two neighbor nodes (elements) of the node which is going to be removed. While In ArrayList all the elements need to be shifted to fill out the space created by removed element.

• 3) Inserts Performance: LinkedList add method gives O(1) performance while ArrayList gives O(n) in worst case. Reason is same as explained for remove.

• 4) Memory Overhead: ArrayList maintains indexes and element data while LinkedList maintains element data and two pointers for neighbor nodes hence the memory consumption is high in LinkedList comparatively.

شباهتهای لیست آرایه ای با لینک لیست

• Both ArrayList and LinkedList are implementation of List interface.

• They both maintain the elements insertion order which means while displaying ArrayList and LinkedList elements the result set would be having the same order in which the elements got inserted into the List.

Example of an ArrayList

List<int[]> b= new ArrayList();int[] z = new int[]{1, 9, 3};b.add(z);int x = b.get(0)[2]; // x = 3System.out.println(x);

Example of an ArrayList

List<Integer> list = new ArrayList<Integer>();

int h = 2;list.add(h);boolean bl = list.contains(2);System.out.println(bl); //trueSystem.out.println(list.contains(3)); //false

.دو مثال دیگر در اکلیپس نمایش داده خواهد شد

For each!

با پایینی هم ارز است فقط در پایینی از ساختار قدیمی حلقه استفاده شده است

مفاهیمinterface و abstract

An interface is an empty shell, there are only the signatures (name / params / return type) of the methods. The methods do not contain anything. The interface can't do anything. It's just a pattern. E.G (pseudo code):

// I say all motor vehicles should look like this: interface MotorVehicle { void run(); int getFuel(); } // my team mate complies and writes vehicle looking that way class Car implements MotorVehicle { int fuel; void run() { print("Wrroooooooom"); } int getFuel() { return this.fuel; } }

• Implementing an interface consumes very little CPU, because it's not a class, just a bunch of names, and therefore there is no expensive look-up to do. It's great when it matters such as in embedded devices.

• Abstract classes• Abstract classes, unlike interfaces, are classes. They are more

expensive to use because there is a look-up to do when you inherit from them.

• Abstract classes look a lot like interfaces, but they have something more : you can define a behavior for them. It's more about a guy saying, "these classes should look like that, and they have that in common, so fill in the blanks!".

// I say all motor vehicles should look like this : abstract class MotorVehicle { int fuel; // they ALL have fuel, so why not let others implement this? // let's make it for everybody int getFuel() { return this.fuel; } // that can be very different, force them to provide their // implementation abstract void run(); } // my team mate complies and writes vehicle looking that way class Car extends MotorVehicle { void run() { print("Wrroooooooom"); } }

مفهوم چند شکلی با یک مثال