Monday, December 4, 2023

10 Best Practices for Handling Null in Java

Hello guys, Dealing with null is an inevitable part of Java programming, but following best practices can help you manage it effectively and write more reliable code. In this article, we'll explore ten best practices for handling null in Java to improve code quality and avoid common pitfalls. If you can handle null then you will sure write better code as null is often the cause of Runtime errors in Java, you may heard of NullPointerException? No. If not then I can say that you have not coded enough in Java because it was the first error I got when I start writing Java program in my college days. At that time I don't know how to handle null better so it's long painful debugging until we find the cause and then do some workaround or fix but if you know how to handle null better then you can write code which can withstand test of time in production. 

Sunday, November 12, 2023

How to remove given character from String using Recursion and Iteration in Java? Coding Interview Question

Hello guys, in the past, I have discussed several popular and frequently asked Java programs from interviews and coding interview questions and today, I am going to share another popular String based coding interview question for Java developers. The questions goes as - write a method to remove all occurence of a given character from String in Java, you can not use a library method like replace() or remove() which can solve the problem for you. You have to build the logic by yourself. You have to provide both iterative and recursive solution of this question. Along with that, you have to provide some unit test as well, which you can write using JUnit or TestNG testing framework. 

Monday, November 6, 2023

Polymorphism and Open Closed Design Principle Example in Java

Java is an object-oriented programming language that allows developers to create complex software applications by organizing code into objects. One of the key features of object-oriented programming is Polymorphism. Polymorphism refers to the ability of objects to take on different forms, depending on the context in which they are used. In Java, polymorphism is achieved through two mechanisms: inheritance and interfaces. In this blog post, I will explain what polymorphism is in Java, how it works, and provide examples to illustrate its usage. We will also discuss the benefits of using polymorphism in your Java programs, as well as some best practices for implementing it effectively.

Saturday, October 28, 2023

Java 8 Stream.filter() example Example with Null and Empty String

The filter() is a method on Stream class, which accepts a Predicate (a functional interface with a method which return boolean) and returns a stream consisting of the elements of this stream that match the given predicate. For example, if stream is obtained from a list containing "SONY", "APPLE" and "GOOGLE" and predicate is elements with length greater than 4, then filter will return another stream containing only APPLE and GOOGLE, leaving SONY out because its length is not greater than 4. Java 8 provides advanced filtering capability using lambdas and predicates. 

Saturday, October 21, 2023

10 Example of Comparator and Sorting in Java 8

The Comparator class is used to provide code or logic for comparing objects in Java, while sorting a list of objects or a collection of objects. It's close cousin of Comparable which provides natural order sorting e.g. ascending and descending orders for numbers like int, short, long or float, and lexicographic order for String i.e. the order on which words are arranged in dictionaries. The Comparators are used while sorting arrays, lists and collections. You pass logic to compare objects and sorting methods like Collections.sort() use that logic to compare elements until they are arranged in sorted order. 

Monday, October 16, 2023

Top 14 Courses to Learn System Design and Software Architecture in 2024 - Best of Lot

Software design or System design is one of the tricky concepts to master. You can quickly learn a programming language like Java or Python, but creating software using that programming language is a totally different thing. That's where software design and coding skills come to the rescue. It's actually an art to structure your program, design your modules, specify boundaries and interactions with other parts of the system so that they are easier to read, maintain, and flexible to change. It's one of the most essential skills for a software developer but often gets ignored. If you look for an online course to learn Software design and architecture, you won't find many, forget about good ones.

Tuesday, October 10, 2023

How to Create and Extract a ZIP File in Java? Unzip Example Tutorial

In Java, you can use ZipFile class to unzip a .jar and .zip file in Java. These classes are defined in and package. Unfortunately there is no ZipUtil class, which provides straightforward methods to decompress zip files, but thankfully there are enough tools available in JDK to write your own method to extract zip file in Java. There are mainly two steps to unzip a zip file in Java, first, create all directories inside of the zip file, because zip archive flattens all directory and second create all file which are inside .zip file. Sinze files in .zip file is not in specific order, you need to iterate twice to perform these steps individually.

Monday, October 9, 2023

How to implement Level Order Traversal of Binary Tree in Java? Example Tutorial

Hello guys, if you have worked in Java then you know that binary tree is one of the essential data structure and quite an important one for programmers, I even mentioned that on my 10 essential data structures for programmers articles.  Binary tree related question are also quite common on coding interviews and we are going to see one today but before that, let's revise what is binary tree? Binary trees are hierarchical data structures composed of nodes, each having at most two children: a left child and a right child. Traversing a binary tree in level order involves visiting nodes level by level, starting from the root. 

Sunday, October 8, 2023

How to make Immutable class in Java? Mutable vs Immutable Objects

How I make immutable objects in Java? I've always thought that all objects are immutable, because if you change the content of an String example, it will always create you an new String object and point to that. However later I found that String is a special class and its specially designed as an Immutable class because its often cached. Obviously you cannot cache anything which is not constant and that make sense why String is Immutable in Java. But this encouraged me to learn more about Mutable and Immutable class in Java and how to create a custom Immutable class in Java.