My Articles:
The builder pattern, as the name implies, is an alternative way to construct complex objects. This pattern should be used when we want to build different immutable objects using the same object-building process. 1. The GoF Builder Pattern Before starting the discussion, I want to make it clear that the …
Java 8 introduced ‘java.util.Base64‘ utility class that provides built-in support for performing base64 encoding and decoding operations as specified in RFC 4648 and RFC 2045. Let us explore Base64 encode and decode processes with various usecases with examples. 1. What is Base64? Base64 is a binary-to-text encoding scheme that converts …
In this Java tutorial, we will learn to read a text file line-by-line methods such as Streams or FileReader. We will also learn to iterate through lines and filter the file content based on some conditions. 1. Files.lines() – Stream through Lines of a File in Java 8 The Files.lines() …
Java 8 has brought many awesome features for java developers. Some of them I have already described in Comparator changes, Streams examples, Internal vs. external iterations, predicates, functional interfaces, default methods, lambda expressions and date and time API changes. All above changes were related to lambda expressions, which is the …
Since Java 8, we can use String.join() method to concatenate strings with a specified delimiter. For advanced usages, use StringJoiner class.
Sreenath Ravva, one of the readers of HowToDoInJava, has appeared in an interview in Oracle for a position in the Oracle Enterprise Manager product. The below-listed questions were shared by him so that I can share them with all of you, in hope that it will benefit few of us …
Learn to create a Comparator instance with lambda expressions, method references and chaining multiple comparators for complex comparisons.
Java Streams can be defined as a sequence of elements from a source that supports aggregate operations on them. The source refers to a collection or array.
Learn the difference between internal iteration and external iteration in Java with examples. 1. External Iteration Till Java 7, the collections framework relied on the concept of external iteration where the Collection provides a means to enumerate its elements by implementing the Iterator interface. The clients use the iterator to …
Introduced in Java 8, a functional interface is simply an interface that has exactly one abstract method. Learn more about functional interfaces in this tutorial.
To improve the performance and make the eclipse faster, there are certain ways that you can utilize and see noticeable differences.
Java Predicates are boolean-valued statements that may be true or false depending on the test argument. Predicates are used to filter Streams.
In previous post, we learned about Lambda expressions and functional interfaces. Now, let’s move on the discussion and talk about another related feature i.e. default methods. Well, this is truly revolutionary for java developers. Till java 7, we have learned a lot of things about interfaces and all those things …
Lambda expressions are known to many of us who have worked on advanced languages like Scala. The term “lambda” has its origin in Lambda calculus that uses the Greek letter lambda (λ) to denote a function abstraction. Lambda expressions were introduced to Java as part of Java 8 release. 1. What are …
To solve the version mismatch issue, you can use the concept of a “bill of materials” (BOM) dependency. A BOM dependency keeps track of version numbers and ensures that all dependencies (both direct and transitive) are at the same version. 1. Why do we need the BOM file? If you …
Spring Method Security with Protect-Pointcut is a mechanism to apply security at the method level based on AOP-style expressions (pointcuts). This allows the Spring security configuration to be segregated from the application code.
Life is about little things that happen while planning for big things. Let us discuss those little things having a major impact on our future.
If you have worked on Spring framework projects, then you must have seen spring context configuration files (e.g. applicationContext.xml) where in header you specify the schema references (.xsd) for various spring modules. In schema references, we mention the xml namespace and as well as schema version number. Well specifying schema …
AbstractRoutingDataSource is a very useful feature if we have a design which permits multiple databases based on certain criteria which may change for each user request. An example can be when we can use a particular database when the user belongs to a certain locale and switch to another locale …
Java New input/output (NIO) was introduced with JDK 1.4. Picking up where Standard IO leaves, NIO provides high-speed, block-oriented IO to Java library. By defining classes to hold data, and by processing that data in blocks, NIO takes advantage of low-level optimizations in a way that the java.io package could …
If you are trying to setup your project/ or adding dependency for hibernate 4 then you might face this error in your server logs. Stack-trace will be looking like this: Solution: If you have anything like below declaraions in your hibernate configuration for “sessionFactory” then remove it. OR Once you …
If you are trying to setup your project/ or adding dependency for hibernate 4 then you might face this error in your server logs. Stack-trace will be looking like this: Solution: Reason is that you have incompatible jar’s versions in your application classpath. To resolve this issue, make sure you …
1. Reason If we are trying to setup your project/ or adding a dependency for hibernate then we might face this error in your server logs. Stack-trace will be looking like this: 2. Solution The reason is that we have incompatible jar versions in our application classpath. To resolve this …
Learn the difference between checked vs unchecked exceptions in Java, with simple explanations and examples. Learn Java exception handling best practices.
Learn to create a splash window using the Java SWT toolkit for Windows applications that need a delay during application loading and initialization.
Java labeled loops help in the case of nested loops when we want to break or continue a specific loop out of those multiple nested loops.
Learn to handle the exceptions thrown from the instance or static initializer blocks in Java by declaring them in constructors.
This post is next update in sequence of discussions regarding little known features of java. Please subscribe through email to get updated when next discussion goes live. And do not forget to express your views in comments section. Java is a safe programming language and prevents programmer from doing a …
Java SerialVersionUID example. Learn to create generate serialVersionUID for classes to be serialized. Learn to change serialversionuid correctly.
Learn to convert InputStream to String using Google guava IO’s ByteSource and CharStreams, BufferedReader, Scanner, and IOUtils classes.
Today the line between “app server” and “web server” is fuzzy. But people continue to use the terms differently, primarily for emphasis.
In software engineering, the decorator design pattern is used to add additional features or behaviors to a particular instance of a class without modifying the other instances of the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be …
Design patterns are used to solve the problems which occur in a pattern, we all know that, right? Also we know that behavioral design patterns are design patterns that identify common communication patterns between objects. One of such behavioral patterns is visitor pattern, which we are going to learn about …
Learn to store and fetch large objects such as images, videos and other documents. Learn to mapping objects to LOB, CLOB and other types
We know that what global variables or constants are, these are fields which are accessible application wide. In java, it is done usually done using defining “public static” fields. Here by adding final keyword, we can change the global variable to global constant. So easy, right? But what about javascript? …
The immutable strings help in achieving the required data safety and performance in a Java application with help of underlying string pool.
So far we have learned about securing spring application using login form based security, custom user details security and many more such security related concepts. In this post, I am giving an example of scenario where use is already authenticated via any third party application or tool e.g. site minder …
Learn to use the EntityManager’s getReference() and find() usages and differences, their behavior when the primary identifies does not exist.
Learn the difference between natural id and primary id, @NaturalId annotation and various APIs to load persistent and proxy entities.
JAX-RS Resteasy has automatic GZIP decompression support. If the client framework or a JAX-RS service receives a message body with a Content-Encoding of “gzip”, it will automatically decompress it. The client framework automatically sets the Accept-Encoding header to be “gzip, deflate”. So you do not have to set this header …
JAX-RS 2.0 has brought lots of improvement over previous version. One of major improvement is client API which was completely missing in JAX-RS 1.0. While it was easy to write a portable JAX-RS service, each JAX-RS implementation defined their own proprietary API. JAX-RS 2.0 fills in this gap with a …
If you are using RESTEasy 3.x.y which has been support for JAX-RS 2.0, and you are trying to write RESTEasy client then you can face this exception. Full stack trace might look like this: Exception in thread “main” java.lang.RuntimeException: java.lang.ClassNotFoundException: org.glassfish.jersey.client.JerseyClientBuilder at javax.ws.rs.client.ClientBuilder.newBuilder(ClientBuilder.java:103) at test.jaxrs2.Demo_JAXRS_2_Example.main(Demo_JAXRS_2_Example.java:14) Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.client.JerseyClientBuilder at …
We know that JAXB(Java Architecture for XML Binding) allows Java developers to map Java classes to XML representations. JAXB provides two main features: the ability to marshal Java objects into XML and the inverse, i.e. to unmarshal XML back into Java objects. JAXB mostly is used while implementing webservices or …
This exception occurs when you are using JAXB to marshal a Java object (collection type) to xml format. The stack trace looks like this in case of Lists. OR in the case of Maps. Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 3 counts of IllegalAnnotationExceptions java.util.Map is an interface, and JAXB can't …
This exception occurs when you are using JAXB to marshal a Java object (collection type) to xml format. The stack trace looks like this: 1. Reason The above exception occurs because JAXB always expects a @XmlRootElement annotation on the entity, it gets to marshal. This is mandatory and can not …
Java examples to marshal and unmarshal the collection of objects of type List and Set implementations such as ArrayList or HashSet into XML.
Learn to use RESTEasy ContainerRequestFilter to create security filter which is able to to perform authentication and authorization on RESTEasy based web applications.
Java compiler rearranges the statements we write in a Java program for performance improvements. Learn to generate the byte code for a class file to see how they have been rearranged.
Learn Java Secure Hashing algorithms in-depth. Java examples of MD5, SHA256, SHA512, PBKDF2, BCrypt, SCrypt algorithms with salt to create secure passwords.
Learn the basics of CountDownLatch and how to use it in real-life applications, its methods and use it to control the flow of application.