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

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

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

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

Java Tutorial 2

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

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

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

Primitive Data Types

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

Wrapper Classes Introduction

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

• 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.

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

Examples

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

(NumberFormatException)

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

Some Methods for Wrapper Classes

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

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

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");   } }

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

Another Example for Generic Type

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

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.

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

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

• 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.

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

• 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.

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

• 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.

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

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

• 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.

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

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);

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

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

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

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

For each!

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

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

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

مفاهیم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; } }

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

• 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!".

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

// 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"); } }

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

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

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