Difference

C++ v.s. Java

C++ Java
Memory difference managed by developers using pointers. Support structures and union. Controlled by system, does not use pointers. Support threads and interfaces.
Inheritance Provide simple and multiple inheritance both. Does not support multiple inheritance.
Runtime error detection mechanism Programmer's responsibility System's responsibility
Libraries Comparatively available with low-level functionalities Provide wide range of classes for various high-level services.
Program Handling Methods and data can reside outside classes. Concept of global file, namespace scope available All method and data reside in class itself. Concept and Package is used.
Type semantics Supports consistent support between primitive and object types Different for primitive and object types
Portability Platform dependent as source code must be recompiled for different platform Uses concept of bytecode which is platform independent and can be used with platform specific JVM
Polymorphism Explicit for methods, supports mixed hierarchies Automatic uses static and dynamic binding

Interface v.s. Abstraction class

An Interface contains only the definition / signature of functionality, and if we have some common functionality as well as common signatures, then we need to use an abstract class. By using an abstract class, we can provide behavior as well as functionality both in the same time. Another developer inheriting abstract class can use this functionality easily, as they would only need to fill in the blanks.

Array v.s. ArrayList v.s. LinkedList

Array:

  • The size of the arrays is fixed
  • Inserting a new element in an array of elements is expensive, because room has to be created for the new elements and to create room existing elements have to shifted.

ArrayList :

  • Dynamic size, size can be increased or shrunk based on the total number of elements in the list

LinkedList :

  • Double-linked List
  • Dynamic size
    • Ease of insertion/deletion
    • nodes cannot be accessed directly instead we need to start from the head
  • Linked List are linear data structures where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part.
Property Array ArrayList
Size Array is fixed in size. Once declared, its size can’t be changed. ArrayList size can be changed after declaration. For example when we add or remove element in arraylist, its size increases or decreases accordingly.
Primitives Array can contain primitive data types as well as objects. ArrayList can contain objects only.
storage Array elements are stored at contiguous memory locations. ArrayList elements are not stored at contiguous memory locations.
Property ArrayList LinkedList
Implementation ArrayList internally implements dynamic array to store elements. LinkedList internally implements doubly linked list to store elements.
Accessing An elements can be retrieved or accessed in O (1) time. ArrayList is faster because it uses array data structure and hence index based system is used to access elements. An element can be accessed in O (n) time. LinkedList is slower because it uses doubly linked list and for accessing an element it iterates from start or end (whichever is closer).
Insertion Normally the insertion of element at end takes O (1) time. But in case array is full then array is resized by copying elements to new array, which makes insertion time O (n). Insertion of an element at start and end in LinkedList is faster, it takes O (1) time. Inserting at specified position is slower, it takes O (n) time.
Deletion Deletion of an element is slower and takes O (n) time because all the elements from old array is copied to new array. Deletion of element from start and end is faster and takes O (1) time. While deletion at specified position is slower and takes O (n)

HashSet v.s. HashMap v.s. HashTable

HashMap:

Hashing is a technique of converting a large String to small String that represents same String. A shorter value helps in indexing and faster searches.

  • store key/value pairs
  • The hash code is computed for the key and stored in the table,
  • HashMap can not use in synchronized situation. it is disadvantage and advantage too. Because it lack of the lock mechanism. it has better performance.
  • HashMap allows one null key and any number of null values.

HashSet :

  • HashSet internally uses a HashMap to store its elements.
  • HashSet stores elements based on their hashcode
  • HashSet is not thread-safe.

Library v.s. Framework

The key difference between a library and a framework is "Inversion of Control". When you call a method from a library, you are in control. But with a framework, the control is inverted: the framework calls you.

framework-vs-library

A library is just a collection of class definitions. The reason behind is simply code reuse, i.e. get the code that has already been written by other developers. The classes and methods normally define specific operations in a domain specific area. For example, there are some libraries of mathematics which can let developer just call the function without redo the implementation of how an algorithm works.

In framework, all the control flow is already there, and there's a bunch of predefined white spots that you should fill out with your code. A framework is normally more complex. It defines a skeleton where the application defines its own features to fill out the skeleton. In this way, your code will be called by the framework when appropriately. The benefit is that developers do not need to worry about if a design is good or not, but just about implementing domain specific functions.

Primitive data type v.s. objects difference

  1. A primitive is not composed of other data types. Where as an object can be and generally is.
  2. Primitives are passed by value, i.e. a copy of the primitive itself is passed. Whereas for objects, the copy of the reference is passed, not the object itself.
  3. Primitives are independent data types, i.e. there does not exist a hierarchy/super class for them. Whereas every Object is descendent of class "Object".
  4. Every object has some default methods of itself, which it inherits from the class Object (e.g. toString(), clone(), wait(), notify(), notifyAll(), etc.). The primitives does not have any method associated with themselves.
  5. Primitives are immutable, they can be used as so without any special care. Whereas for objects, special care needs to be taken, they are not immutable by default.
  6. With objects, there are two types of copies, Shallow and Deep. There is a significant difference between them. So the choice depends on how do we intend to use them. With primitives, there is no such difference, everything is by default deep copy only.

Shallow v.s. Deep copy

Breadth vs Depth; think in terms of a tree of references with your object as the root node.

Shallow:

Before Copy Shallow Copying Shallow Done

The variables A and B refer to different areas of memory, when B is assigned to A the two variables refer to the same area of memory. Later modifications to the contents of either are instantly reflected in the contents of other, as they share contents.

Deep:

Before Copy Deep Copying Deep Done

The variables A and B refer to different areas of memory, when B is assigned to A the values in the memory area which A points to are copied into the memory area to which B points. Later modifications to the contents of either remain unique to A or B; the contents are not shared.

equals() v.s. hashcode()

https://www.javaworld.com/article/3305792/learn-java/java-challengers-4-comparing-java-objects-with-equals-and-hashcode.html

every Object in Java includes an equals() and a hashcode() method, but they must be overridden to work properly.

As a rule, when you override equals() you must also override hashcode().

Comparing objects with equals()

We use the equals() method to compare objects in Java. In order to determine if two objects are the same, equals() compares the values of the objects’ attributes

The == operator compares whether two object references point to the same object.

Uniquely identifying objects with hashcode()

Executing hashcode() returns a unique ID for each object in your program, which makes the task of comparing the whole state of the object much easier.

Using equals() and hashcode() with collections

The Set interface is responsible for ensuring no duplicate elements will be inserted in a Set subclass. The following are some of the classes that implement the Set interface:

  • HashSet
  • TreeSet
  • LinkedHashSet

Redis v.s. main cache

Redis is more scalable than main cache because it is easy to config and create cluster

Write-through : update cache and update database - > return true
Write-round : update database -> return true
Write-back : update cache -> return true

results matching ""

    No results matching ""