javacodegeeks.com

History and Motivations Behind Java’s Maligned Serialization

Issues related to Java’s serialization mechanism are well-advertised. The entire last chapter of Effective Java 1st Edition (Chapter 10) and of Effective Java 2nd Edition (Chapter 11) are dedicated to the subject of serialization in Java. The final chapter of Effective Java 3rd Edition (Chapter 12) is still devoted to serialization, but includes a new item (Item 85) that goes even further emphasize two assertions related to Java serialization:“The best way to avoid serialization exploits is to never deserialize...

javacodegeeks.com

Callback hell and Reactive patterns

One of the ways that I have better understood the usefulness of aReactive Streams based approach is how it simplifies a Non-blocking IO call.This post will be a quick walkthrough of the kind of code involved in making a synchronous remote call, then show how layering in Non-blocking IO though highly efficient in the use of resources(especially threads) introduces complications referred to as a callback hell and how a reactive streams based approach simplifies the programming model.Target ServiceSince I will be writing a client call, my target...

javacodegeeks.com

Reflection is the most important Java API

The other day I was wondering – which is the most important Java API. Which of the SE and EE APIs is the one that makes most of the Java ecosystem possible and that could not have just been recreated as a 3rd party library.And as you’ve probably guessed by the title – I think it’s the Reflection API. Yes, it’s inevitably part of every project, directly or indirectly. But that’s true for many more APIs, notably the Collection API. But what’s important about the Reflection API is that it enabled most of the popular tools and...

javacodegeeks.com

Using Java 12 in Eclipse

1. Install JDK 12Link: https://www.oracle.com/technetwork/java/javase/downloads/jdk12-downloads-5295953.html2. Install Eclipse 4.11Link: https://download.eclipse.org/eclipse/downloads/drops4/R-4.11-2019030705003. Install Eclipse Java 12 SupportStart Eclipse and go to Help > Install New Software. Add Update Site: https://download.eclipse.org/eclipse/updates/4.11-P-builds. Install Eclipse Java 12 support for 2019-03 development stream from the list of available software4. Add Java 12 JRE to EclipseGo to Window > Preferences, navigate to...

javacodegeeks.com

Reflection selector expression

Java::Geci is a code generator that runs during unit test time. If the generated code fits the actual version of the source code then the test does not fail. If there is a need for any modification then the tests modify the source code and fail. For example, there is a new field that needs a setter and getter then the accessor generator will generate the new setter and getter and then it fails. If there is no new field then the generated code is just the one that is already there, no reason to touch the source code: the test that started the...

javacodegeeks.com

Java 12: Switch Expressions

In Java 12, the switch statement has been enhanced so that it can be used as an expression. It is now also possible to switch on multiple constants in a single case, resulting in code that is more concise and readable. These enhancements are a preview language feature, which means that they must be explicitly enabled in the Java compiler and runtime using the --enable-preview flag.Consider the following switch statement:0102030405060708091011121314151617int result = -1;switch (input) {  case 0:  case...

javacodegeeks.com

Generating setters and getters using Java::Geci

In the article , we created very simple hello-world generators to introduce the framework and how to generate generators generally. In this article, we will look at the accessor generator, which is defined in the core module of Java::Geci and which is a commercial grade and not a demo-only generator. Even though the generator is commercial grade, using the services of the framework it has simple code so that it can be represented in an article.What does an accessor generatorAccessors are setters and getters. When a class has many fields and we...

javacodegeeks.com

Explicit No-Arguments Constructor Versus Default Constructor

Most developers new to Java quickly learn that a “default constructor” is implicitly created (by javac) for their Java classes when they don’t specify at least one explicit constructor. Section 8.8.9 of the Java Language Specification succinctly states, “If a class contains no constructor declarations, then a default constructor is implicitly declared.” That section further describes characteristics of the implicitly created default constructor including it having no parameters, having no throws clause, and...

javacodegeeks.com

Box old objects to be autoclosable

Since Java 7 we can use try-with-resources and have any object automatically closed that implements the Autocloseable interface. If the resource is Autocloseable. Some of the classes need some wrap-up but are not Autocloseable. These are mainly old classes in some legacy framework that still get in our way to make us trip up. Nobody is using Struts any more, but still, there are enough old frameworks that are there lurking in the dark and with which we have to live. I recently had that experience and I was so motivated that I created a simple...

javacodegeeks.com

Java Queue Interface

Introduction:A Queue is a FIFO (First In First Out) abstract data type (ADT). In other words, the elements are removed in the order in which they were inserted.The java.util.Queue is an interface in Java and extends from java.util.Collection. Some of the commonly used Queue implementation classes include a LinkedList, an ArrayDeque and a PriorityQueue.Types of Queues:There are two major categories of the queue:1. Blocking queues: These are the bounded queues having a fixed capacity. That means we must provide the capacity of the queue at the...

javacodegeeks.com

Java ArrayDeque

Introduction:ArrayDeque in Java is a class that implements a Deque interface. It’s an array-based implementation of a double-ended queue. As the name suggests, a double-ended queue is a queue that allows us to add or remove items from both front and rear ends.Before we dive in, let’s quickly look at a few noteworthy points on an ArrayDeque:An ArrayDeque has no capacity constraints; the size of the array grows dynamically as per needsWe can’t add null values to an ArrayDequeIt’s not a thread-safe implementationSince Deque is...

javacodegeeks.com

PriorityBlockingQueue In Java

Introduction:A PriorityBlockingQueue in Java implements the BlockingQueue interface and supports the features of a PriorityQueue. So, what’s a BlockingQueue?Following holds true for any implementation of a BlockingQueue:While attempting to retrieve an element, a thread waits if the queue is emptyIn case of a bounded BlockingQueue implementation, the thread waits till it gets space to insert a new elementA PriorityBlockingQueue is an unbounded concurrent blocking queue present in java.util.concurrent package. It doesn’t allow null...

javacodegeeks.com

Lazy assignment in Java

Programmers are inherently lazy and similis simili gaudet also like when the programs are lazy. Have you ever heard lazy loading? Or lazy singleton? (I personally prefer the single malt version though.) If you are programming in Scala or Kotlin, which is also a JVM language you can even evaluate expressions in a lazy way.If you are programming in Scala you can write1lazy val z = "Hello"and the expression will only be evaluated when z is accessed the first time. If you program in Kotlin you can write something like1val z: String by lazy {...

javacodegeeks.com

LinkedHashMap In Java

Introduction:LinkedHashMap is a Map implementation which maintains the insertion order of its elements. It extends the HashMap class: public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V> Just like a HashMap, it allows storing one null key and many null values.Instantiating LinkedHashMap:We can use one of the following constructors to create a LinkedHashMap: LinkedHashMap() //default capacity 16 with load factor of 0.75 LinkedHashMap(Map<? extends K,? extends V> map) LinkedHashMap(int...