Visitor Design Patterns In Java Examples Tutorial

Hello guys, if you want to learn Visitor design pattern in Java then you have come to the right place. Earlier, I have covered many design patterns like Decorator, Strategy, State, Composite, Adapter, Command, Template, Factory, Observer and even few Microservice patterns like SAGA and Database per service and in this article, I will talk about Visitor Design Pattern and how to implement in Java. You will learn things like what is Visitor design pattern, what problem it solves, what are pros and cons of Visitor design pattern, when to use Visitor pattern as well as any alternatives of Visitor Pattern in Java. I will also show you a real world example of Visitor design pattern, but, before we get to the 5 best examples that will teach you all about design patterns in Java, let me tell you a little bit more about what it really is.

Difference between 32-bit vs 64-bit JVM in Java?

Hello Java Programmers, if you want to learn Java virtual Machine in-depth and wondering what is the difference between a 32-bit and 64-bit JVM and which one should you use and why? then you have come to the right place. Earlier, I have shared the best JVM books and online JVM courses and in this article, I am going to talk about 32-bit vs 64-bit JVM and their pros and cons. This is also a common Java interview question for beginners and intermediate Java programmers. I have tried to answer this question to the point that's why this article is a short but informative one. You will find out what they are, how they are different, how much heap size, and the pros and cons of each of them. 

10 Examples Of Mockito + JUnit in Java for Unit Testing

Hello guys, if you are writing unit test in Java then you know how difficult it is to write especially if you are testing a class which is dependent upon other class like HttpClient and you cannot connect to actual server. At those time, a mocking library like Mockito comes to rescue. Given the increased focus on unit testing and code coverage, I have find myself using Mockito more and more along with JUnit in last a couple of years but I haven't written many articles on Mockito yet but that is chaging now. In this article, I am going to share 10 essential Mockito examples which I belive every Java programmer should know. But, before we get to the 10 best examples that will teach you everything there is to know about Mockito in Java, let me tell you a little bit more about what it really is.

How to convert Java 8 Stream to Array and ArrayList in Java? Example Tutorial

It's relatively easy to convert a Stream to an array in Java 8 by using the toArray() method of java.util.Stream class. By using this method you can convert any type of Stream to a corresponding array like a Stream of Strings can be converted into an array of String, or a Stream of integers can be converted into an array of Integers. The Stream.toArray() method is also overloaded, the one which doesn't take any parameter returns an Object[] which might not be very useful, particularly if you want to convert Stream of T to an array of T.

Array length vs ArrayList Size in Java [Example]

One of the confusing parts in learning Java for a beginner to understand how to find the length of array and ArrayList in Java? The main reason for the confusion is an inconsistent way of calculating the length between two. Calling size() method on arrays and length, or even length() on ArrayList is a common programming error made by beginners. The main reason for the confusion is the special handling of an array in Java.  Java native arrays have built-in length attribute but no size() method while the Java library containers, known as Collection classes like ArrayList<>, Vector<>, etc,  all have a size() method. 

How to create an ArrayList from Array in Java? Arrays.asList() Example Tutorial

One of the common problems faced by junior and less experienced Java developers is converting an array to ArrayList e.g. they are getting an array from somewhere in their code and then want to create an ArrayList out of that so that they can add more elements and use other library methods which operate with ArrayList or List. The simplest way to convert an array to ArrayList is by using the Arrays.asList() method, which acts as a bridge between Collection classes and array data structure. This method returns a List that contains elements from an array. 

Is it Possible to add static or private methods in Java interface?

Can you add a static or private method in an interface in Java? or is it possible to add a private or static method in Java interface? or can you add a non-abstract method on an interface in Java? are a couple of popular Java interview questions which often pop up during telephonic interviews. Well, prior to Java 8, it wasn't possible to add non-abstract methods in Java but nowadays you can add non-abstract static, default, and private methods in the Java interface. The static and default methods were supported as part of interface evolution in Java 8 and you can add private methods on an interface from Java 9 onwards.

10 Examples Of Scanner Class In Java

Hello guys, if you want to learn about Scanner class in Java, one of the most popular class to read input from command prompt or console then you have come to the right place. In this article, I will explain how to read input from command prompt, what is Scanner class and how you can use Scanner class to read various data types like String, int, long, boolean directly from console. You must have heard about famous nextInt(), nextDouble() methods which can read integer and double values from command prompt or console. But, before we get to the 10 best examples that will teach you everything you need to know about scanner class in Java, let me tell you a little bit more about what it really is.

What is Diamond operator in Java? Example Tutorial

Hello guys, if you are reading Java code and come across closed angle bracket with a diamond like shape and wondering what they are and what they do then you have come to the right place. They are known as Diamond operator in Java and I will explain you what they are and where should you use them in this article. The Diamond operator is a relatively new operator in Java which was first introduced in JDK 7  to improve type inference and reduce boilerplate Java coding. It is denoted with a closed angle bracket that resembles the shape of a diamond (<>) and that's why it's called the Diamond operator. If used correctly it can reduce typing and boilerplate coding in Java and result in much cleaner and readable code especially when you use Generics.  

Parallel Array Sorting in Java - Arrays.parallelSort() Example

There are multiple ways to sort array in Java. For example, you can use Array.sort() method to sort any primitive or object array in Java but Java 8 presents a new feature that is useful to sort array’s elements. It can implemented by using the package “java.util.Arrays” which represents several methods to sort the array. The biggest benefit is that parallel array sorting is that its faster than any other sorting method due to usage of multithreading conception. So multiple threads can break the array into parts and work simultaneously to sort it. 

Difference between Thread vs Process in Java? Example

Thread and Process are two closely related terms in multi-threading and the main difference between Thread and Process in Java is that Threads are part of the process. i.e. one process can spawn multiple Threads. If you run a Java program in UNIX based system e.g. Linux and if that program creates 10 Threads, it still one process and you can find that by using ps -ef | grep identifier command which is one of the most popular use of grep command in UNIX, Where identifier is UNIX the text which can be used as regular expression to find that Java process.

Difference between yield and wait method in Java? Answer

Yield vs wait in Java
The yield and wait methods in Java, though both are related to Threads,  are completely different to each other. The main difference between wait and yield in Java is that wait() is used for flow control and inter-thread communication while yield is used just to relinquish CPU to offer an opportunity to another thread for running. In this Java tutorial, we will what are differences between the wait and yield method in Java and when to use wait() and yield(). What is important for a Java programmer is not only to understand the difference between the wait() and yield() method but also to know the implications of using the yield method. 

10 points about wait(), notify() and notifyAll() in Java Thread?

If you ask me one concept in Java that is so obvious yet most misunderstood, I would say the wait(), notify(), and notifyAll() methods. They are quite obvious because they are one of the three methods of a total of 9 methods from java.lang.Object but if you ask when to use the wait(), notify() and notfiyAll() in Java, not many Java developers can answer with surety. The number will go down dramatically if you ask them to solve the producer-consumer problem using wait() and notify()

How to run Threads in an Order in Java - Thread.Join() Example

Hello Java programmers, if you need to execute multiple threads in a particular order, for example if you have three threads T1, T2 and T3 and we want to execute them in a sequence such that thread 2 starts only when first thread finishes it job and T3 starts after T2, but with multithreading in Java there is no guarantee. Threads are scheduled and allocated CPU by thread scheduler which you cannot control but you can impose such ordering by using Thread.join() method. When you start a thread its not guaranteed that which thread will start first and whether the thread started first will finish first, if your application's logic depends upon a sequence its better to do all those operation on single thread because if all code is confined to one thread it will execute in order they were written provided some JIT optimization.

10 Examples Of Lombok Libarary In Java

Hello guys, if you are working in Java then you may have heard about Lombok, one of the popular Java library which alleviate pain Java developer by removing boiler plate code and provides improve development experience. Yes, Lombok can remove a lot of code like the one you put on equals() and hashCode, getter and setter, toString and constructor and much more. This means you can just create a Java class with fields much like Record of Java SE 17 and you can use it like a fully functional Java class, I mean you can create object using constructor or Builder and you can get and set values using gettter and setter, only difference is they are not visible in code. But then you may be thinking how does it work? Where does it get those getter and setter? why not compiler raise any problem?

10 Examples Of Ternary Operator In Java

Hello guys, if you are wondering how to use ternary operator in Java then you have come to the right place. Ternary operator is a great operator and you can use ternary operator to replace a simple if-else statement. Since ternary operator allows you to write code in one line, its much more succinct and readable and that's what many Java developer love it, including me. In the last article, I shared 10 example of XOR bitwise operator in Java and in this article, I am going to share 10 example of ternary operator in Java so that you not only know what is ternary operator but also you can use it effectively to write better Java code. But, before we get to the 10 best examples that will teach you everything there is to know about ternary operators in Java, let me tell you a little bit more about what it all really is.

10 Examples of XOR Operator In Java

Hello guys, if you are wondering what does the XOR operator do in Java then you have come to the right place. XOR is a bitwise operator in Java, much like AND and OR and its very important because you can perform XOR operation using this operator. If you don't remember, XOR return true if the two values which you are comparing are different like one is true and other is false or vice-versa but it returns false or 0 if both operands are same like both are true or both are false. By using this technique you can solve many coding problems as you can check if the bits you are comparing is same or not. In this article, I will show you different example of XOR operator in Java after that you will understand this operator better. 

How to convert float, double, String, Map, List to Set, Integer, Date in Java - Example Tutorial

Hello guys, converting one data type to another, for example String to Integer or String too boolean is a common task in Java and every Java programmer should be familiar with how to convert common data types like String, Integer, Long, Float, Double, Date, List, Map, Set, to each other. In the past, I have shared several tutorial where I have shown you how to carry out such conversion in Java and this article is nothing but a collection of such tutorial so that you can learn all of that knowledge in one place. You can also bookmark this page as I will be adding new data type conversion tutorials into this list as and when I write them, and you can also suggest if you struggle to convert a particular type to another and I will try to cover them here.