Thursday, October 31, 2024

How to convert JSON String to Java HashMap? Example

Suppose you have a JSON String, may be a web-service response and you want to create a Map out of it, where key should be the fields and value should be values of those fields e.g. if given JSON String is following :

{

"zip": "90210",

"city": "Beverly Hills"

"state": "CA"

"timeZone": "P"

}


then your HashMap should contain 4 mappings where keys are zip, city, state and timeZone, all of String type and corresponding values. 

Thursday, October 24, 2024

What is Timer and TimerTask in Java – Tutorial Example

Timer in Java is a utility class that is used to schedule tasks for both one time and repeated execution. Timer is similar to the alarm facility many people use in mobile phones. Just like you can have one time alarm or repeated alarm, You can use java.util.Timer to schedule a time task or repeated task. In fact, we can implement a Reminder utility using Timer in Java and that's what we are going to see in this example of Timer in Java. Two classes java.util.Timer and java.util.TimerTask is used to schedule jobs in Java and forms Timer API. 

4 Reasons and Benefits of Using Multithreading in Java? Why Threads?

In one word, we use Threads to make Java applications faster by doing multiple things at the same time. In technical terms, Thread helps you to achieve parallelism in Java programs. Since CPU is very fast and nowadays it even contains multiple cores, just one thread is not able to take advantage of all the cores, which means your costly hardware will remain idle for most of the time. By using multiple threads, you can take full advantage of multiple cores by serving more clients and serving them faster. Since, in today's fast-paced world, response time matters a lot and that's why you have multi-core CPUs, but if your application doesn't make full use of all resources then there is no point adding them, multi-threading is one way to exploiting huge computing power of CPU in Java application.

4 examples to convert Date to LocalDate in Java 8?

One of the challenge you will face while using new Java 8 Date and time API, JSR-310 in existing Java application is the conversion between java.util.Date and java.time.LocalDate and other classes. Even though library has everything you would expect, it doesn't have a direct conversion method between old Date and new LocalDate. There is a reason for it, eventhough java.util.Date says its a date its not, becuase its just a millisecond value since midnight at the start of 1970 GMT. It's equivalent to Instant class in new Java Date API and that's why you have a direct conversion method between Date and Instant in Java 8. Anyway, its not hard to convert a java.util.Date to java.time.LocalDate in Java 8, in fact there are multiple ways which we will explore in this tutorial. 

Wednesday, October 23, 2024

Difference between ExecutorService.submit() and Executor.execute() methods in Java?

What is the difference between Executor.submit() and Executor.execute() method in Java? This is one of the good multi-threading questions for experienced Java programmers, mostly asked in Investment Banks like Barclays, Deutsche Bank, or Citibank. A main difference between the submit() and execute() method is that ExecuterService.submit()can return the result of computation because it has a return type of Future, but the execute() method cannot return anything because return type is void. The core interface in Java 1.5's Executor framework is the Executor interface which defines the execute(Runnable task) method, whose primary purpose is to separate the task from its execution.

Difference between Process and Thread in Java - Example

One of the common questions from programming interviews is, what is the difference between a Thread and a Process? Well, the main difference between them is that a Process is a program that is executing some code and a thread is an independent path of execution in the process. A process can have more than one thread for doing independent tasks e.g. a thread for reading data from disk, a thread for processing that data, and another thread for sending that data over the network. This technique to improve throughput and better utilize CPU power is also known as multi-threading.

How to use Exchanger for Inter thread communication in Java? Example Tutorial

Hello guys, if you are working in a concurrent Java application then you might have heard about the Exchanger class of java.util.concurrent package. The Exchanger in Java is another concurrency or synchronization utility introduced in Java 1.5 along with CountDownLatch, CyclicBarrier, and Semaphores. As the name suggests, the Exchanger allows two Threads to meet and exchange data at the rendezvous or meeting point. This means you can use Exchanger to share objects between threads and for inter-thread communication. The java.util.Exchanger is a parametric class, which defines and holds the type of object to be exchanged. It has an overloaded method called the exchange(), which is used to exchange objects between threads.

What happens when you call Thread.run() instead of Thread.start() in Java? Trick Interview Question

Hello guys, writing multi-threaded and concurrent programs is not easy, not even in Java.  Even senior developers, including myself, make mistakes while writing concurrent Java applications. This is also one of the trickiest areas of Java programming language, where misconceptions outnumber concepts. Considering the amount of misconception an average Java programmer has about multi-threading and concurrency, I thought to start a new series about common multi-threading mistakes done by Java programmers; what is a better way to learn from common real word mistakes.

How to Implement Thread in Java with Example

How to implement Thread in Java
In my opinion, Thread is one of the most important features of the Java programming language which helped it to become the most popular programming language. I remember, when I first started learning Java in one of the programming classes in India how important Thread was a portrait and how much emphasis is given on a clear understanding of multi-threading. It’s still popular and one of most sought after skills in Java programmer because writing concurrent and multi-threaded applications in Java is challenging, despite Java providing excellent support at language level using synchronized and volatile keyword.

Udemy vs Educative Review? Which is better Website for Beginners?

Hello guys, if you are wondering the difference between Udemy and Educative and whether you should take Udemy courses or join the Educative platform to learn new tech skills, you have come to the right place. Earlier, I have shared a comparison between Pluralsight and Udemy as well as CodeCademy vs Pluralsight in this article, I will compare Udemy with Educative, a new text-based, interactive learning platform. While I have often said that Udemy is my favorite platform because you can get high-quality and comprehensive courses from top instructors for just $9.9, which is phenomenal. 

Can You Learn Coding and Web Development in your 40s and 50s?

Yesterday, I was speaking to one of my friends who were not from a programming background but working as a teacher in a college of Engineering and Technology in the countryside of India. He just turned 40, and he is also the father of two wonderful kids. He has been teaching Maths to B.Tech students in a private engineer college for the last 15 years. He is earning Ok, but he always wanted to explore the world, and he now realizes that a Programming job can provide him that opportunity, so he contacted me to find out whether it's too late for a Programming career.

Monday, October 21, 2024

Review - Is Grokking the Machine Learning Interview on Educative Worth it in 2025?

Hello friends, we are here again today for another exciting topic to discuss. But, today we are not gonna discuss something which is related to Java or any other language or spring boot. Today we are gonna discuss something which is immensely practical and has the potential to land you very high-paying data science jobs.  Today we are gonna review a course that focuses on Machine Learning! Machine Learning is very important when we are considering data science interviews! So what's the wait? Let's start! On Educative.io, there is a great course called Grokking the Machine Learning Interview. It couldn't have come at a better moment, with machine learning expected to be a $3.6 billion business by 2025.

Top 10 Eclipse Plugins for Java and Spring Boot Developers

There is no doubt that Eclipse is one of the most popular IDEs for Java development, and what makes Eclipse great is nothing but Plugins. There are hundreds of Eclipse plugins for different kinds of tasks and integration with other essential tools like there are plugins to download code from Github, SVN, CVS, etc. There are planning to use Maven or Gradle from Eclipse. There are specific plugins for Java development, Python Development, creating Spring-based Java applications and for creating Android apps from Eclipse itself. I have seen productivity improvement by just installing the right kind of plugins. 

Top 10 Java 11 Certification Mock Tests and Practice Tests [4 Free OCPJP 11 Sample Questions]

The latest Java certification is now out, the Java 11 Certification. Ever since Java 9 and Java10 were out, programmers were speculating that what will happen to Java certification now? Will, there is, be a new Java 9 certification or Java 10 certification? Will there be a new Java certification every 6 months, following the Java SE release cycle? Well, Thankfully, nothing of that sort happened; a new Java certification every six months would have been a crazy idea, not just for students and programmers but also to all the parties involved, including Oracle itself.  Now the big question is what has been changed in Java 11 certification, and should you go for OCAJP 8 or OCAJP 11 certification?

Sunday, October 20, 2024

Top 5 Websites to Learn SQL Online for FREE - Best of Lot

SQL is one of the most important skills for any programmer be it a Java, C++, Python, JavaScript, or Ruby developer. Almost 95% of the Java applications use a relational database in their back-end and almost all web applications use the database. In recent years, one of the most common ways to learn any programming skill is online, at your comfort of the office or home and SQL is no different. Learning SQL online has another advantage of a quick head start because you don't need to install a database and create tables to write some SELECT queries. 

Grokking The Java Interview - My First Book After 10 Years as Java Blogger

Hello guys, I am very excited to announce the release of my first book after 10 years of writing Java articles and Java interview questions While I have been blogging for the last 10 years, I have never really sold anything, didn’t have any book, course, or any digital product. Finally, now I have my first book and it's going to help Java developer cracking interview — Grokking the Java interview. This was my long desire to have my content in a structured and organized way to provide more value and this book does that. It provides a structured way to prepare for Java interviews and learn essential core Java concepts.

5 Projects You can do to learn iOS App Development in 2025

Hello guys, if you are learning iOS App development and looking for some project ideas to better learn the concepts and apply your existing knowledge then you have come to the right place. Earlier, I have shared the best iOS and Swift courses, and today, I am going to talk about 5 cool projects you can do to learn iOS app development and master several key concepts like UI, Core ML, Speech to ext, and other APIs. Since it's very common for people to get stuck while doing projects, I have also included guide courses, both free and paid which you can join to learn how to do these projects step by step. 

Saturday, October 19, 2024

Coursera's Google IT Automation with Python Certification Review - Is it worth it in 2025?

Hello guys, if you are joining Google IT Automation with Python Professional Certification on Coursera and want to find out whether it's worth your time and money or not then you have come to the right place. Earlier, I have shared the best Coursera Certifications and a review of other popular Google courses on IT support and in this article, we'll review Google's most popular Python certification course in Coursera. Google's IT automation with Python is a professional certificate and more than 575,000 people have already joined this course it's from Google which is a great sign but you will find out more about this course in this review. 

Udemy's The Data Science Course: Complete Data Science Bootcamp 2025 Review - Is it worth it?

Hello guys, if you want to learn Data Science and looking for the best courses or want to join The Data Science Course 2025: Complete Data Science Bootcamp course on Udemy but are not sure whether this course is right for you then you have come to the right place. Earlier, I have shared the best Data Science courses and in this article, I am going to review this popular Udemy course to learn Data Science in 2025. This is one of the most popular Udemy courses when it comes to learning Data Science and with more than 560K students it's right there with the Data Science A-Z course by Kirill Eremenko, my favorite Udemy instructor for Data Science and Machine Learning. 

Friday, October 18, 2024

Top 25 Skills Java Developers Should Learn in 2025 [UPDATED]

Hello guys, recently I was doing some research on what skills Java developers should learn in 2025 to become a better developer and give their career a boost and I found some interesting skills like Kafka and  Elastic Search, apart from obvious ones like Spring Boot and Microservices. One of my favorite places for doing such research is examining Job listings on LinkedIn from different companies like Investment banks, Service-based companies, startups, and mid-tier companies where Java is used extensively. When I saw those skills, I can immediately tell that those are really useful and worth sharing with you guys, as I have often seen those skills playing important roles while hiring Java developers.

6 Tips to write Production Quality Code

We often hear the term "production quality code", some times in an interview where the interviewee asked to write production-quality code for a particular function, sometimes discussion between developers and programmers and sometimes during code review. I know most of you familiar with the term but for those who wonder what is production quality code in simple terms its code which can bear the test of time in a production environment. We all develop code in development, then test in QA and finally, it gets released in production.

Top 10 Multithreading and Concurrency Best Practices for Experienced Java Developers

Writing concurrent code is hard and testing correctness with concurrency is even harder. Though Java programming language provides lots of synchronization and concurrency support from language to API level, it eventually comes to an individual's diligence and expertise to write bug-free Java concurrency code. These Java concurrency and multi-threading best practices are a collection of some well-known tips, which help you to write better concurrency code in Java. Some of you may be familiar with these tips but it's often worth revising them time and time again. 

Wednesday, October 16, 2024

How to find Square root of integer in Java without using any Library? Interview Question

Problem : Given an integer i, find square root of it, without using Math.sqrt() method. Your method should return square root if given number is perfect square otherwise return floor(sqrt(x));

For example, 


Input i = 16

Output : 4


Input i = 17

Output : 4


This is very interesting problem and asked both freshers and experienced programmers. I have seen this question on many companies including ANZ, Barclays, and Citibank.

How to print Floyd's Triangle in Java? Example Tutorial

There are lots of programming exercises in Java, which involve printing a particular pattern in the console, one of them is printing the Floyd triangle in the console. In the Floyd triangle, there are n integers in the nth row and a total of (n(n+1))/2 integers in n rows. This is one of the most simple patterns to print but helpful in learning how to create other more complex patterns. The key to develop patterns is using nested loops and methods like System.out.print() and println() appropriately. Actually, pattern-based programming tasks are originally designed to master loops in programming.

How to Check if two Rectangles Overlap in Java? Collision Detection Solution and Example

Can you write a Java program to check if two rectangles are overlapping with each other or not? is one of the frequently asked coding questions on tech giants like Facebook, Amazon, Microsoft, and others. This is also a kind of problem where it's easy to find opposite or negative conditions like when rectangles are not overlapping and then inverting the result to prove that rectangles are colliding with each other. I first heard about this problem from one of my friends who were in the Android game development space. He was asked to write an algorithm to find if given rectangles are intersecting or not.

Tuesday, October 15, 2024

Linux Command to Find how Long a process is Running in UNIX - Example

Hello guys, today, I am going to share one of the useful tips to find out how long a particular process is running in Linux. This is very useful while you are troubleshooting an issue and want to know whether your process or service is restarted fine on a daily or weekly basis. So you checked your process is running in Linux operating system and it's running fine, by using ps command. But now you want to know, from how long process is running, what is the start date of that process etc. Unfortunately, PS command in Linux or any UNIX based operating system doesn't provide that information.

Difference between FileInputStream and FileReader in Java | InputStream vs Reader Example

Before going to explain specific difference between FileInputStream and FileReader in Java, I would like to state fundamental difference between an InputStream and a Reader in Java, and when to use InputStream and when to go for Reader. Actually, Both InputStream and Reader are abstractions to read data from source, which can be either file or socket, but main difference between them is, InputStream is used to read binary data, while Reader is used to read text data, precisely Unicode characters.

Monday, October 14, 2024

4 Ways to Convert String to long in Java? Example Tutorial

How to convert a string to long in Java is one of those frequently asked questions by a beginner who has started learning Java programming language and is not aware of how to convert from one data type to another. Converting String to long is similar to converting String to Integer in Java, in-fact if you know how to convert String to Integer then you can convert String to Long by following the same procedure. Though you need to remember few things while dealing with long and String first of all long is the primitive type which is wrapped by the Long wrapper class and String is an Object in Java backed by character array.

How to Count Negative Numbers in a Sorted Matrix? [Solved] (Amazon Coding Question)

Hello guys, earlier I shared a list of Google Coding Interview questions, and today, I am going to share with you an interesting coding problem which was asked on Amazon, how to count total negative numbers in a given matrix where rows and columns are sorted in increasing order. Again, I found this coding problem while surfing on the internet, It wasn't actually asked to me or my reader, so I can't vouch that it's actually an Amazon Interview question. Though, I really expect it to be because it's an interesting problem and the optimal solution is not so easy but with the internet, you never know.

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.

Thursday, October 10, 2024

How to check if array is a binary heap in Java? [Solved]

Problem : You have given an array of integer, check if the given array represents a binary max-heap data structure. In max heap every node is greater than all of its descendants. 

For example :

Input: array[] = {100, 25, 20, 17, 22, 12} 

Output: true

The given array represents below tree

100

/ \

25 20

/ \ /

17 22 12 

This binary tree follows max-heap property as every node is greater than all of its descendant. 

Saturday, October 5, 2024

10 Code Review Checklist and Best practices in Java

What to review while doing Code review
Code Review and Unit testing are some of the best development practices I always recommend, strive for, and enforce as much as possible. Even just by doing code review and Junit test case always offer positive results it can be improved a lot by constantly learning from our mistakes, others mistakes and by observing how others are doing it. I always try to get my code review done by someone with more technical and domain experience so that I can capture any domain-specific scenarios which have been missed during think through the process.

How to find the first element in Stream in Java 8? findFirst() Example

In Java 8, you can use the Stream.findFirst() method to get the first element of Stream in Java. This is a terminal operation and is often used after applying several intermediate operations e.g. filter, mapping, flattening, etc. For example, if you have a List of String and you want to find the first String whose length is greater than 10, you can use the findFirst() method along with stream() and filter() to get that String. The stream() method gets the Stream from a List, which then allows you to apply several useful methods defined in the java.util.Stream class like filter(), map(), flatMap() etc.

Friday, October 4, 2024

10 Examples of Optional in Java 8

Null is bad, it can crash your program. Even its creator called it a billion-dollar mistake hence you should always try to avoid using nulls whenever you can. For example, you should not return a null String when you can return an empty String, similarly never return null collections when you can return an empty collection. I have shared many such tips in my earlier article, 10 tips to avoid NullPointerException and my reader liked that a lot. But, I wrote that article a couple of years ago when Java 8 was not around and there was no Optional, a new way to avoid NullPointerException in Java, but, things have changed now.

Why use SLF4J over Log4J for logging in Java? Example

Every Java programmers know that logging is critical for any Java application, especially server-side application, and many of them are already familiar with various logging libraries e.g. java.util.logging, Apache log4j, logback, but if you don't know about SLF4J, Simple logging facade for Java,  then it's time to learn and use SLF4J in your project. In this Java article, we will learn why using SLF4J is better than using log4j or java.util.logging. It’s been a long time, since I wrote 10 logging tips for Java programmer,I don’t remember anything I have writing about logging.

Thursday, October 3, 2024

How to replace NULL with Empty String in SQL Server? ISNULL() vs COALESCE() Examples

We often need to replace NULL values with empty String or blank in SQL e.g. while concatenating String. In SQL Server, when you concatenate a NULL String with another non-null String the result is NULL, which means you lose the information you already have. To prevent this, you can replace NULL with empty String while concatenating. There are two ways to replace NULL with blank values in SQL Server, function ISNULL(), and COALESCE(). Both functions replace the value you provide when the argument is NULL like ISNULL(column, '') will return empty String if the column value is NULL.

10 Books Every Java Programmer Should Read - Best of Lot, Must Read

If you are a Java programmer and wondering what to read to improve your knowledge of Java and become a better Java developer, then you have come to the right place. In this article, I am going to share some of the best Java books ever written. In fact, this is my list of all-time great Java books. They have withstood the test of time and emerges as more and more successful years after years. It doesn't matter if you read them this year or reading them next year, you are always going to learn a lot, and that's why I call them all-time great books.

The OCAJP 1Z0-808 - Best Books and Mock Exams for Java 8 OCA Certification

From the last couple of months, I have been helping a few developers to prepare for Java 8 associate certification with exam code 1Z0-808, also known as OCAJP 8. From my experience, both for preparing for Sun certification and helping other developers to get Java certified, I can say that two keys to be successful in Oracle Java SE certification are choosing an excellent study guide/course and buying one or two top quality mock exam simulators. When I say success, it doesn't mean just passing the exam but scoring above 90% because passing score 65% is not very difficult to achieve and anybody can pass OCAJP 8 by just a couple of month of practice but to score 90% and above is not that easy, you need to prepare and prepare hard.

2 Books to Prepare Oracle Java Certification Exams - OCA and OCP Java SE 6,7, 8 OCPJP 8

There was a time when Sun Certification was very hot and programmers feel proud of being Sun Certified Java Professional(SCJP). After Oracle took over Sun in early 2010, it changed names of all the examination from SCJP to OCPJP or OCJP, and now OCA and OCP for Java SE 7 and Java SE 8. I see there is a lot of confusion among certification nowadays, but essentially they are of the same level at previous SCJP. Since Java gets new language features with every new release, subsequently a new certification title is introduced. I am not agreed with upgrading your Java certification with every release, it doesn't make much sense to me, but many programmers prefer to get certified and keep it updated.

Top 10 Free Java Programming Books, EBooks and PDF for Beginners and Experienced Programmers

Who doesn't like free stuff? Well, I do like and many Java programmers like me simply love free Java books, eBooks, and PDFs. Fortunately, the Internet is full of free books and eBooks but unfortunately, many of them are of not good quality. Most of the eBooks which are freely available either are sample chapters of popular Java programming books or they are simply old and out-of-date. In my last article, I have shared a lot of such books and the feedback was that most of them, even though they are good in the past, have simply not up to the mark. The search goes on and while browsing the net a couple of days back I hit the jackpot when I found these free Java books from O'Reilly.

Wednesday, October 2, 2024

5 Reasons to Use Composition over Inheritance in Java and OOP - Example

Favor composition over inheritance is one of the popular object-oriented design principles, which helps to create flexible and maintainable code in Java and other object-oriented languages. Many times I have seen people suggesting use composition instead of inheritance, in fact, my favorite books like Head First Design Patterns also advocate this design principle. The Head First books have their own way of explaining, why composition is better than inheritance, and though it's long it's quite interesting and informative. It was the first chapter of this book, which helped me a lot in understanding this key OOP concept. 

Difference between char, varchar, nchar and nvarchar data types in SQL Server? Example

What is the difference between char and varchar in SQL, followed by nchar and nvarchar, is one of the popular SQL interview questions, and surprisingly not every programmer knows this basic difference. If you go with the name, which you should, then you can figure out that char is a fixed-length data type while varchar should be a variable-length data type. Though all char, varchar, nchar, and nvarchar are used to store text or String data there are subtle differences between them. As I said char is fixed length, which means a variable or a column like Zipcode char(10) will take only 10 bytes to store data, including space.

Top 5 Books to Learn SQL and Database Design for Programmers and DBAs - Best of Lot

The database design and modeling are one of those topics which rarely get the attention they deserve, especially at the start of the project, but once things have gone out of hand, then everybody talks about them. Comments like - this database is designed poorly, the schema is not performing well, you cannot add a new column easily, etc. becomes very common. The most problem with database design is that it is mainly done by application developers like Java or C++ developer who knows SQL, but they are not the expert on how to design tables and schema. 

Tuesday, October 1, 2024

Observer design Pattern in Java with Real world code Example

Observer design pattern in Java is a fundamental core Java pattern where Observe watches for any change in state or property of Subject. For Example, Company updates all its shareholders for any decision they make here Company is Subject and Shareholders are Observers, any change in the policy of company and the Company notifies all its Shareholders or Observer. This was a simple real-world explanation of the Observer pattern.