It's all about Java

Tuesday, 21 October 2025

CHEAT SHEET for java List interface, LinkedList and ArrayList

Consider Below points before choosing List concrete implementations for your use case: 

  • ArrayList is a linear data structure and  re-sizable/dynamic array
 
  • Initial size can be given. and if not given the default size is 10
 
  • It is always good practice to give initial size and advised not to give very large number as size
 
  • Good programmers prefer ArrayList to LinkedList if retrieval operations is done more frequently than insertion and deletions of elements.
 
  • Better choose LinkedList if insertion and deletion operations are done frequently compared to retrieval operations.
 
  • Lists are not sorted. if required use Collections.sort() to sort
 
  • Collections.sort() uses optimized merge sort [tim sort] on ArrayList
 
  • However legacy merge sort also can be used using a switch. But it is going to be deprecated  in near future 

  • The major difference between ArrayList and LinkedList is RandomAccess behavior 

  • ArrayLists uses Random access indexing where as LinkedLists uses
          Sequential access indexing
 
  • LinkedList carries extra overhead in the form of reference to the next and previous elements other than element's data itself

  • LinkedList uses Doubly LinkedList data structure internally

  • Deque is the java DoubleLinkedList data structure implementation

  • Before choosing List implementation ask yourself below questions 
                    a. Are you aware of initial size? 
                    b. Which operations are used more frequently? [insertion, deletion,
                       retrieval, search, sort are few among others]

  • Search Algorithm: 
                  a. Collections class uses binary search if List is ArrayList
                  b. Collections class uses linear search if List is of type LinkedList


  • ArrayList and LinkedList both uses fast fail iterations.

  • If another thread tries to modify list while iterating, java runtime throws ConcurrentModificationException. This can be considered as fast fail behavior  

Wednesday, 15 October 2025

Coding and best practices

 Write code or write code in right way...

Writing a coding solution to a problem can be done in many ways. If the solution can be obtained with less number of code lines, then it is easy to understand and maintain.

But what if code is large with having millions of lines. Moreover maintenance and readability becomes difficult as the code base grows... Right? 

In this blog I want to share my views and experiences on writing code efficiently and effectively with less maintenance efforts.

Aspects to consider while coding:

  • Naming convention
  • Cyclomatic Complexity
  • SOLID principles
  • DRY Principle
  • Managing dead code
  • Comments


Naming conventions:

Every programming language mandates few norms in order to name variables, classes and functions etc. For ex: Java uses class names first letter to be a alphabet and rest to be lower case letters. 

But if we dig little bit more, naming conventions are not limited to only upper case or lower case letters and using alphanumeric letters etc.

Naming a variable must be enough concise as naming your first child's name...

Few bad names for variables and constants:

          int final static TWO = 2;  [wrong]

          int final static MAX_LENGHT = 2; [Correct] 

  

Cyclomatic Complexity:

Cyclomatic complexity is a measure or metric to estimate complexity level of a snippet or block of code. 

Cyclomatic complexity and best practices:

  • Never put more than three conditional checks (&& operator and || operator etc.) in one if condition
  • Never exceed more than thirty lines in one function or method.
  • Never exceed more than 1000 lines in one class or one file.

SOLID principles:

Usually developing a large and scalable application, requires better design. Adhering to SOLID principles yields better scalable and easily maintainable applications.

There are five SOLID principles. They are:

  1. Single Responsibility Principle
  2. Open/Closed Principle
  3. Liskov's Substitution Principle
  4. Interface Segregation Principle
  5. Dependency Inversion Principle

DRY Principle:  

DRY stands for Do Not Repeat Yourself. Sometimes, There could be a need to use same or similar set of lines of code to be used multiple times in the same application. For ex. String utilities, DB connections or updating records in database etc.

Writing frequently used code snippets multiple times is not a good practice. Instead move this code to a function or method and use this method in places where ever required.

Simple use cases are:

  • DB Connections
  • File IO Operations
  • String Utilities etc. 

Managing Dead Code:

Sometimes there are some lines or instructions in code base, which are completely not executed in all cases at all. This is considered as dead code. 

Dead code reduces the quality with respect to the readability.  


Comments:

Comments increases the readability of the program for programmers. Comments are written at class level, method level and variable level [Constants, instance variables and class variables] differently. 

The reason for this difference is, the pattern of comments is identified by API document generator tools and generates API documents.


Conclusion:

What all I tried to share through this post is, writing code for a specific problem can be done in different ways. But making sure that the program is more readable, maintenance free and easily scalable is also very important. 

The above few solutions or hacks will definitely help in developing better applications. 

Hope you enjoyed reading the post. Comments are welcomed. Thank you!! 

Happy coding!..

Sunday, 28 September 2025

Atomicity with Java Programming Language

 Atomicity with Java

What is Atomicity


Atomicity, in computer science, is considered to be a property [ALL-OR-NOTHING], that the state of a resource in an operation is not controlled by a different flow of execution at the same time [Even in time slicing operations]

We know, Java is not pure object oriented programming language, due to primitive data types' design behavior.

Most of the Java experts, while designing and developing applications thrives hard, to create, most secured applications considering "Atomicity in multi threaded application".

Does Java language helps in developing Secured application..?

The answer is no, Java programming language does not give secured applications at all, especially in multi threaded environment.

As we know, Java supports 2 types of variables/data types. 
  1. Primitive data types
  2. User defined data types

Primitive data types:      

Java supports 8 different primitive data types. They are: 

  1.  boolean    1 byte
  2.  short        2 bytes
  3.  int            4 bytes
  4.  char         2 bytes
  5.  float         4 bytes
  6.  long         8 bytes
  7.  double     8 bytes
  8.  byte         1 byte

User Defined data types:


In Java, user defined data types are created using "new" operator. For example, to create Employee object, the following instruction is coded

Employee employee = new Employee();

Now, the employee in the above statement, is a reference to an Employee class and called as user defined data type.

Now, coming to the point [Why Java is not secured..?], the primitive data types, long and double are not atomic variables. That means they are not executed in a single operation. 

long variable's size is 64 bits. And it is divided into 32 bits + 32 bits while copying from one memory location to another memory location. and all user defined data types are based on primitive data types.

And these two primitive variables [long and double] breaks the atomicity law. 

And developers can not bypass long and double variables in their program to create application. It is almost impossible to create the application without these variables.

Especially, in multi threaded based application, these non atomic variables may give unpredictable results. 

Popular posts

CHEAT SHEET for java List interface, LinkedList and ArrayList

Consider Below points before choosing List concrete implementations for your use case:  ArrayList is a linear data structure and  re-s...