Core Java


Java is a programming language and a platform.

Platform: Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API, it is called a platform.

Java is a high level, robust, object-oriented and secure programming language.

Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995

Java was developed by James Gosling, who is known as the father of Java, in 1995.

Types of Java Applications:
1) Standalone Application
2) Web Application
3) Web Application
4) Mobile Application

Java Platforms / Editions: There are 4 platforms or editions of Java:
1) Java SE (Java Standard Edition)
2) Java EE (Java Enterprise Edition) - used to develop web and enterprise applications.
3) Java ME (Java Micro Edition) - It is a micro platform that is dedicated to mobile applications.
4) JavaFX - It is used to develop rich internet applications. It uses a lightweight user interface API.

What happens at runtime?
ClassFile
Classloader: It is the subsystem of JVM that is used to load class files.
Bytecode Verifier: Checks the code fragments for illegal code that can violate access rights to objects.
Interpreter: Read bytecode stream then execute the instructions.
Runtime
Hardware

JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine. It is a specification that provides a runtime environment in which Java bytecode can be executed. It can also run those programs which are written in other languages and compiled to Java bytecode.

JRE: Java Runtime Environment is a set of software tools which are used for developing Java applications. It is used to provide the runtime environment. It is the implementation of JVM. It physically exists. It contains a set of libraries + other files that JVM uses at runtime.

The Java Development Kit (JDK) is a software development environment which is used to develop Java applications and applets. It physically exists. It contains JRE + development tools.

JVM Architecture


Classloader:
Bootstrap ClassLoader
Extension ClassLoader
System/Application ClassLoader

Execution Engine:
A virtual processor
Interpreter
Just-In-Time(JIT) compiler


ArrayList - LinkedList

Manipulation with ArrayList is slow because it internally uses an array. If any element is removed from the array, all the other elements are shifted in memory. Manipulation with LinkedList is faster than ArrayList because it uses a doubly linked list, so no bit shifting is required in memory.

LinkedLists are preferred over ArrayLists for scenarios where frequent insertion and deletion of elements is required, as these operations are faster with LinkedLists. LinkedLists can also be more memory-efficient than ArrayLists.

Performance. A LinkedList consumes a bit more memory than an ArrayList since every node stores two references to the previous and next element. The insertion, addition, and removal operations are faster in a LinkedList because there is no resizing of an array done in the background.

Why is HashMap faster than ArrayList?
The ArrayList has O(n) performance for every search, so for n searches its performance is O(n^2). The HashMap has O(1) performance for every search (on average), so for n searches its performance will be O(n). While the HashMap will be slower at first and take more memory, it will be faster for large values of n.

Why retrieval is faster in ArrayList?
All elements in the ArrayList are located next to each other in the same memory space. This is why retrieving an element from an ArrayList is so fast: given the location of the start of the ArrayList, you can know exactly where any element is located in memory. LinkedLists, on the other hand, use non-contiguous memory.

Why are linked lists inefficient?
Linked lists do not provide a contiguous storage guarantee and you cannot hope to get this performance boost. This is also the reason why random iteration (accessing elements randomly) performs worse than forward iteration (accessing elements in order) for contiguous containers.

What is the drawback of linked list?
Memory Usage: The memory used by LinkedList is more because we also need to store the address of the next data. Accessing an element: We can not access any element of the LinkedList directly. We don't have direct access to every element of LinkedList.

Which collection is faster in Java?
If you need fast access to elements using index, ArrayList should be choice. If you need fast access to elements using a key, use HashMap. If you need fast add and removal of elements, use LinkedList (but it has a very poor seeking performance).

Which map is faster in Java?
HashMap will generally be fastest, since it has the best cache behavior ( HashMap iterates directly over the backing array, whereas TreeMap and LinkedHashMap iterate over linked data structures).

Why is manipulation with ArrayList so slow?
Manipulation with ArrayList is slow because it internally uses an array. If any element is removed from the array, all the other elements are shifted in memory. Manipulation with LinkedList is faster than ArrayList because it uses a doubly linked list, so no bit shifting is required in memory.

What is the complexity of ArrayList and LinkedList?
For ArrayList , insertion is O(1) only if added at the end. In all other cases (adding at the beginning or in the middle), complexity is O(N), because the right-hand portion of the array needs to be copied and shifted. The complexity of a LinkedList will be O(1) both for insertion at the beginning and at the end

Why insertion and deletion is difficult in ArrayList?
ArrayList internally uses and array to store the elements, when that array gets filled by inserting elements a new array of roughly 1.5 times the size of the original array is created and all the data of old array is copied to new array.

Which two Cannot be stored in an ArrayList?
ArrayLists cannot hold primitive data types such as int, double, char, and long (they can hold String since String is an object, and wrapper class objects (Double, Integer).

Comments

Popular posts from this blog

PL/SQL

JAVA8 Features

Build Automation