Why Dependency Injection is a Best Practice?

As a Java programmer, you've likely encountered the term "Dependency Injection" (DI) in your development journey. Dependency Injection is more than just a buzzword; it's a design principle that can significantly improve your code's quality, maintainability, and testability. In this article, we'll delve deep into the world of Dependency Injection, exploring why it's considered a best practice, and I will also showcase practical examples of Dependency injection using popular frameworks like Spring and Google Guice. You can use those examples to learn how to write code using Dependency Injection in Java. 

15+ Spring Framework Quizzes for Java Programmers (Free)

Hello guys, ever since I have started creating practice test on Udemy a lot of people ask me if I can practice a few questions as quiz in my blog here for free. I though its a great idea because it allows my readers to not just see the questions but also practice it and that's why I am creating this kind of quizzes. If you guys like it then I will probably create many more. Any way, preparing for IT certification like Oracle's Java certification or VMware's Spring Certification required a lot of hard-work. I have seen many experienced Java developers failing these certifications and losing money and time due to overconfidence and lack of preparation. A structured and complete certification preparation involves reading books, joining course and doing practice questions

How to print pyramid pattern of stars and numbers in Java? Example

Hello guys, printing patterns of stars and numbers is a common programming exercises and is often asked during coding interviews as well. If you are learning to program or preparing for coding interviews, knowing how to print a pattern can really help. You can print the Pyramid pattern of stars or numbers using loops and print methods in Java. There are two print methods you need to know, System.out.print() and System.out.println(), the difference between print() and println() is that println adds a new line character at the end i.e. it appends \n automatically. which means the next time you write something will begin at the new line. On the other hand, if you use print() then the text is appended to the same line.

How to Find Prime Factors of Integer Numbers in Java - Factorization Algorithm Example

One of the common homework/tasks in programming courses is about Prime Factorization. You are asked to write a program to find prime factors of a given integer number. The prime factors of a number are all of the prime numbers that will exactly divide the given number. For example, prime factors of 35 are 7 and 5, both are prime in themselves and exactly divide 35. The last time I did this exercise was when I was in college, and it was something like, writing a program that asks the user for an integer input and then displays that number's prime factorization on the command line.  

How to Count number of Set bits or 1's of Integer in Java? Example

There are multiple ways to count the number of 1's or set bits in an integer number in Java. You can use a bitwise and bit shift operator on your own, or, you can use Java API to count the number of set bits. Java 1.5 added two utility methods called bitCount(int i) which returns a number of 1's in your integer number, java.lang.Long class has a similar bitCount(long number) for long primitives. As I have said earlier, Coding questions are an important part of any Java interview, and from that recursion and bitwise operations are most popular. 

How to Add Two Integer Numbers without using Plus + or ++ Arithmetic Operator in Java - Recursion example

In this article, we will take a look at another interview question about adding two numbers, but without using the + or ++ operator. The interview starts with a simple statement, Can you write a function to add two numbers (integers) without using + or plus arithmetic operator in Java? If you are good at maths, it wouldn’t take more than a second to say that, we can use subtraction or - operator to add two numbers because a-(-b)== a+b. Well, that’s correct, but the real question starts when the interviewer quickly points out that, you can not use any arithmetic operator including +,-,*,/++, or --

How to Check if Integer Number is Power of Two in Java - 3 examples

How to check if an integer number is a power of 2 in Java is one of the popular programming interview questions and has been asked in many interviews. Surprisingly, this problem which looks simple enough to answer doesn't turn out that simple if for many developers. Many Java programmers, both freshers and less experienced,  struggle to write code for a function, which can check if the number is the power of 2 or not. 

Java Program to find factorial of number in Java - Example Tutorial

How to find the factorial of a number in Java in both recursive and iterative ways is a common Java interview question mostly asked at the fresher level. It’s not just popular in Java interviews but also in other programming languages like C or C++. It's also famous  In our last article we have seen how to check if a number is prime or not and in this Java programming tutorial, we will see a simple Java program to find the factorial of a number in Java by using recursion and iteration. The same program can also be used to print factorial of any number or print a range of factorial as well.

How to choose a Collection class in Java? Flowchart Example

One of the key skill of a Java programmer is his mastery over Collection framework, he must know when to use which collection class in Java. He must remember that Map is for key value pair, which List and Set is for storing values only. He should know that List is ordered collection, which allows duplicate but Set is unordered but doesn't allow duplicates. He should also be able to choose between Sorted Set, Map and other collection class which provides ordering e.g. LinkedHashMap and LinkedHashSet. To your surprise this is not at all a difficult task, all you need to remember is virtue of different collection class in Java. This article, help you to choose right collection class depending upon your requirement, by simply going through a flow chart and selecting collection where your requirement and their properties matches. 

How to Check If Number is Even or Odd without using Modulus or Remainder Operator? Example

Write a Java program to find if a number is odd or even is one of the basic programming exercises, and anyone will be happy to see this in actual Java Interviews, wouldn't you? By the way, did I said easy, well there is a little twist there, you need to check odd and even without using modulus (%) or remainder operator in Java. Since many programmers, especially freshers are familiar with % operators, this nice little trick does put them into thinking mode, which is what the interviewer wants. This question is on a similar level of checking if a number is a palindrome or not if you have practiced such questions, it would be easy to find a solution.

Java Program to print Prime numbers in Java - Example Tutorial and Code

How to print Prime numbers in Java or how to check if a number is prime or not is classical Java programming questions, mostly taught in Java programming courses. A number is called a prime number if it's not divisible by any number other than 1 or itself and you can use this logic to check whether a number is prime or not. This program is slightly difficult than printing even or odd numbers which is relatively easier than Java exercises. This Simple Java program prints prime numbers starting from 1 to 100 or any specified number. It also has a method that checks if a number is prime or not.

Java Program to Find Sum of Digits in a Number using Recursion? Example

Recently this question to ask was one of my readers, which inspired me to write this tutorial. There was a usual check to solve this problem using both recursion and iteration. To be frank, calculating the sum of digits of an integral number is not difficult, but I have still seen quite a few programmers fumbles, even after providing hints in terms of division and modulus operator. The key point here is to know how to use division and modulus operators in Java. This kind of exercise including reversing a number, where you need to find digits from a number, use division operator to remove right, and use modulus operator or % to get rightmost digits.

Difference in @RunWith vs @ExtendWith Annotations in JUnit - Java

Hello guys, if you have written any unit test in Java then you must have come across JUnit and Mockito. JUnit is the most popular and widely-used testing framework in the Java ecosystem, enabling developers to write and execute unit tests for their Java applications. While writing tests, JUnit provides various annotations to facilitate different functionalities. Two of these annotations, @RunWith and @ExtendWith, play a crucial role in customizing the test execution process. In this article, we will explore the key differences between these two annotations and illustrate their usage through a Java program. This one is also one of the popular JUnit Interview question which I have also included in my earlier article about 20 Most asked JUnit Interview Questions for Java developers, if you haven't read it yet then you can also read it to learn more about JUnit and unit testing in Java. 

Java Program to Reverse an Integer Number - Example tutorial

How to reverse a number in Java without using any API or write a simple Java program to reverse a number is common programming questions asked on fresher level software engineer interviews. Reversing a number is also popular homework question on many Java programming courses in schools, colleges, and training institutes. I personally feel java program to reverse number is good programming exercise for someone who is just started learning to program in Java or any other programming language because of its simplicity and a little bit of trickiness which shows how to use operators for programming purposes rather than arithmetic purpose.

3 ways to Find First Non Repeated Character in a String - Java Programming Problem Example

Write a Java program to find the first non-repeated character in a String is a common question on coding tests. Since String is a popular topic in various programming interviews, It's better to prepare well with some well-known questions like reversing String using recursion, or checking if a String is a palindrome or not. This question is also in the same league. Before jumping into the solution, let's first understand this question. You need to write a function, which will accept a String and return first non-repeated character, for example in the world "hello", except 'l' all are non-repeated, but 'h' is the first non-repeated character.

How to Count Number of Words in String ? Java Coding Exercise Example

The string is very popular among the Interviewer, and you are bound to see some questions on any programming interview, Java Interviews are no exception. Questions based on Java fundamentals like why String is Immutable in Java to questions based on coding skills e.g. reverse String using recursion in Java, String has always troubled candidates. In this article, we will see a similar question, how to count the number of words in Java String. Before jumping to the solution, just read below to make sure what a word means here. It's a sequence of one or more non-space characters.

How to Reverse String in Java Using Iteration and Recursion - Example

How to reverse String in Java is a popular core java interview question and asked on all levels from junior to senior java programming jobs. since Java has rich API most java programmer answer this question by using StringBuffer reverse() method which easily reverses a String in Java and its right way if you are programming in Java but the most interview doesn't stop there and they ask the interviewee to reverse String in Java without using StringBuffer or they will ask you to write an iterative reverse function which reverses string in Java.

Top 20 String Algorithm Questions from Coding Interviews

In this article, we are going to see the top 20 String based coding interview question and their solution to help programmers better prepare for interviews. The string is one of the most important data structures and available in almost every programming language like Java, C, C++, Python, Perl, and Ruby. Though there implement differ the essence remains the same like String is NULL terminated character array in C but String is an object in Java, again backed by character array. The string is also available on weekly typed languages like Python and Perl.  This is why you will always find some String based coding questions on programming interviews.

How to check if two String are Anagram in Java - Program Example

Write a Java program to check if two String is an anagram of each other, is another good coding question asked at fresher level Java Interviews. This question is on a similar level of finding the middle element of LinkedList in one pass and swapping two numbers without using the temp variable. By the way, two String is called anagram, if they contain the same characters but on different order e.g. army and mary, stop and pots, etc. Anagrams are actually a mix-up of characters in String. If you are familiar with String API, i.e. java.lang.String then you can easily solve this problem.

How to delete a directory with files in Java - Example

Deleting an empty directory is easy in Java, just use the delete() method of java.io.File class, but deleting a directory with files is unfortunately not easy. You just can't delete a folder if it contains files or sub folders. Calling delete() method on a File instance representing a non-empty directory will just return false without removing the directory. In order to delete this folder, you need to delete all files and subdirectories inside this folder. This may seem cumbersome, but unfortunately, there is no method that can delete a directory with files in Java, not even on Java 7 Files and Paths class.

How to resolve java.lang.ClassNotFoundException in Java? Example

What is ClassNotFoundException in Java
ClassNotFoundException is one of Java nightmare every Java developer face in there day to day life. java.lang.NoClassDefFoundError and java.lang.ClassNotFoundException are two errors  which occurs by and now and chew up of your precious time while finding and fixing root cause. From the name java.lang.ClassNotFoundException looks quite simple but underlying cause of it is always different and which classifies it as an environmental issue. In this java tutorial, we will see what is ClassNotFoundException in java, what is real cause of it, and how to fix it along with some more frequent and infamous examples of java.lang.ClassNotFoundException in Java or J2EE, Don’t mistake this exception with NoClassDefFoundError in Java which is also due to incorrect classpath in Java. 

9 Maven Concepts and Tools Every Java Developers Should Know

The Apache Maven is an essential tool for Java developers. It makes their life easy by allowing them to create a Java project faster by using a standard directory structure. It also helps them to download project dependency automatically. Not only that, but Maven also downloads transitive dependencies that relieve Java developers from the big headache of keep check of different versions of dependent libraries. For example, if your application is dependent on the Spring framework, but Spring is dependent on Log4j then you also need to download the correct version of Log4j JAR files for the Spring MVC framework, Maven does this automatically for you.

Where and How to download Spring Framework JAR file (Spring 5.0 or Spring 4.0) without Maven, Gradle

One of the easiest and oldest ways to run a Java program which depends on an external library or framework is to download dependency JAR files, put them on the classpath and then run the program by creating a Main class with the main() method. This is simple but not as easy as you think, there are many challenges down the road e.g. you need to find the right version of JAR files and their dependencies e.g. Spring might have a dependency on other third-party libraries like Log4j. So, when the build tool like Maven and Gradle comes, everybody stopped downloading the JAR file manually.

Tibco Tutorial : Http Interface for Tibco RV Issues Troubleshooting and TIBCO RV Alternatives

This is another post of my tibco tutorial series , if you want to read more about tibco rv or tibco ems please read there. in this post I am sharing you great tool to solve tibco rv related problems and a great interface to analyze your Tibco RVD activities. until i know this I mostly used netstat command to figure out which topics are subscribed by my tibco RVD but after since I know about this I had helped me a lot.

FIX protocol Tutorial: Futures and Options

Hello guys, its been a long time since I shared any FIX protocol tutorial, I think almost 12 years but today is the day. If you are using FIX Protocol to create derivative trading systems in Java and want to learn about Futures, Options and how they are supported in FIX Protocol then you have come to the right place. In the past, I have shared FIX Protocol tutorials as well as many FIX Protocol questions and in this article, I will talk about Futures, Options and how FXI Protocol support their trading. The FIX Protocol, widely used in financial markets, plays a crucial role in electronic trading, including futures and options. Futures and options are derivatives that enable traders and investors to speculate on the price movement of various financial instruments. This tutorial will provide a comprehensive overview of futures and options trading and how the FIX Protocol supports these activities.

What is Atomic Operation and Variable in Java? AtomicInteger Counter Example

Hello guys, if you're wondering what is atomic operator in Java and how Atomic variable works then you have come to the right place. Earlier, I have shared difference between atomic, volatile and synchronized in Java and in this article, I will explain What is Atomic operator in Java, What are atomic variables and how to use them. In Java, the reading and writing of 32-bit or smaller quantities are guaranteed to be atomic. By atomic we mean each action takes place in one step and cannot be interrupted. Thus, when we have multithreaded applications, the read and write operations are thread-safe and need not be made synchronized.

Role based Access control using Spring Security and MVC, Mapping LDAP Groups to Authorities for Authorization

Authentication and Authorization is an integral part of any Java enterprise or web application. Since most of the company uses LDAP Active directory for authentication, authorization, and Role-based access control (RBAC), it's good to know How to implement Role-based access control using Spring MVC and Spring Security. This is the second part of my articles on using Spring Security for authentication and authorization in Spring MVC based Java application. In the last part, we have learned about doing LDAP authentication against Windows active directory, and in this Spring Security tutorial, we will learn How to map LDAP groups to authorities for implementing Role-based access control or authorization.

Top 12 Locking, Synchronization and Multithreading Interview Questions for 5 to 7 Years Experienced Java Programmers [2023]

Hello guys, if you are an experienced Java developer say 3 to 5 years or 5 to 7 years experience and  preparing for Java Interviews then you very well know that Locking, Synchronization, ConcurrentHashMap, volatile and atomic, compare and swap (CAS), Executor Service, Stream API, and Multithreading in general are quite important and as an experienced Java developer you should be ready for them. In the past, I have shared 50+ Java Multithreading questions, 12 concurrency questions, 21 HashMap questions, and 10 ConcurrentHashMap questions and in this article, I am going to share 10 of my favorite question on Locking, Synchronization and Inter thread communication in Java. If you have been doing Java Interviews then its highly likely that you have already seen this problem but if you haven't you should definitely prepare them.

What is happens-before in Java Concurrency? An example

A couple of days ago, one of my readers messaged me on LinkedIn about a Java interview question he has recently faced - what is the happens-before relationship in Java concurrency? What is the benefit of it, and how exactly it works? He kind of has some ideas about that its related to the Java Memory Model and provides some sort of visibility guaranteed but couldn't explain with conviction to his interviewer, particularly with a code example and was a bit disappointed. He then asked me if I can write an article about it. I said you should have read the Java concurrency in Practice book or join the Java Concurrency in Practice Bundle course by Heinz Kabutz before the interview, that would have helped, but nonetheless, I liked the idea to just provide a quick overview of what is the happens-before relationship between threads in Java.

Top 5 Free Java Multithreading Courses to Learn in 2023 - Best of Lot

Hello guys, if you want to learn multithreading and concurrency in Java and looking for best free resources like online courses and tutorials then you have come to the right place. Earlier, I have shared best free Java Courses and best free Spring Framework courses and today, I will share best free online courses to learn Multithreading and Concurrency in Java. Multithreading is one of the important skill for Java programmers as companies are always in hunt of Java programmer who are good at multithreading and concurrency but at the same time its very hard topic to master. Acquiring solid multithreading and concurrency skill require a lot of hard worked and years of experience but you can speed up by joining best Java multithreading courses and learning from expert.