# 50.001 W1L2 (Introduction to Java II) ## Static Typing Unlike Python, Java uses static typing. This means user has to declare the type of the variable. ```python= # In python, # we dont have to # declare the type x = 4 ``` ```java= // In Java, we have to // declare the type int x = 4; ``` As a result of static typing, it is more stable. In Python it is sometimes impossible to resolve the variable type before it is executed e.g. ```python= var1 = 1 userInput = int(input("enter a number: ")) if userInput > 1: var2 = "hello" else: var2 = 10 var3 = var1 + var2 print var3 ``` This program will run. However, the behaviour might be unexpected due to the fact that the type of var2 is not determined until the program is executed. In a static typed language, the typing error could be detected when compiling the code. Dynamic typed language can't do this and type error will only be detected during execution time. ## Primitive Data Types | Type | Size | Range | Default | | ------- |:------- |:----------------------------------- |:-------- | | boolean | 1 bit | **true** or **false** | false | | byte | 8 bits | [-128,127] | 0 | | short | 16 bits | [-32,768, 32,767] | 0 | | char | 16 bits | ['\u0000','\uffff'] or [0,65535] | '\u0000' | | int | 32 bits | [-2,147,483,648 to 2,147,483,647] | 0 | | long | 64 bits | [-2<sup>63</sup>, 2<sup>63</sup>-1] | 0 | | float | 32 bits | 32-bit IEEE 754 floating-point | 0.0 | | double | 64 bits | 64-bit IEEE 754 floating-point | 0.0 | ## Object Data Types In these object data types, size doesn't need to be specified, unlike arrays. (similar to vector in C++) ArrayList and LinkedList operations are similar. However, the underlying mechanisms are different. ### ArrayList vs LinkedList * ArrayList stores element in an array. If capacity is excedeed, a larger new array will be created and all the elements are copied to the new array * LinkedList stores elements in a linked list data structure (it is not stored consecutively in the memory). * Hence, ArrayList is more efficient to support random access through an index ### ArrayList It is faster to do a random access since all the elements are allocated consecutively in the memory. Time taken to do a random access is O(1). Insertion, however, is slower to do in ArrayList. Adding an element within the ArrayList takes at most O(n) to do since it has to shift all the elements that come after that particular index to make space for this new element. ```java= public static void main (String [] args){ ArrayList<Integer> arr = new ArrayList<Integer>(); arr.add(10); System.out.println("First element is: "+arr.get(0)); arr.set(0,100); System.out.println("First element is: "+arr.get(0)); } ``` ### LinkedList It is an "array" of tuple of value and pointer that points to the next value in the "array". LinkedList is allocated randomly in the memory (not consecutive). LinkedList takes more time to do a random access since it has to iterate through from the first element to the desired element. Worst case: O(n), where n is the size of the LinkedList. However, insertion is faster to do on LinkedList. Let's say we want to add an element at index 4. The only operations required are to change the pointer of the element at index 3 to point at the newly allocated memory cell. ### Performance Comparison ```java= public class PerformanceComparison { public static void main(String[] args) { Integer[] a= new Integer[50000]; //difference between int and Integer is that Integer is an object data type and it supports //more operations List<Integer> list= new LinkedList<Integer>(Arrays.asList(a)); //asList returns a fixed-size list by the backed list long started = System.nanoTime(); int totalCount = 100000; for(int i=0;i<totalCount;i++){ list.get(25000); } long end = System.nanoTime(); long duration = end-started; System.out.println("Time taken: " + duration / 1000000.0 + " ms"); } } ``` From this performance comparison, ArrayList is ~1000x faster for random access. LinkedList is ~100x faster for insertion.