Feed aggregator

Using the Composite Pattern to Turn the Lights on

Javalobby Syndicated Feed - 1 hour 48 min ago

Design patterns are probably one of the most popular topics in the Java blogosphere. Yet many times, the examples used to illustrate the different design patterns are not very engaging. To remedy that, this post explains the Composite pattern in a fun way.

Since there are already many descriptions of this pattern out there, we will not repeat it again. The only thing to remember is that the Composite pattern is used to apply a recursive action on a tree structure. 

Categories: Java

The Developer's Guide to Collections: Sets

Javalobby Syndicated Feed - 4 hours 48 min ago

The set is one of the most mathematical abstractions in the Java Collections Framework (JCF) and plays an integral role in numerous use cases, including status or state flags, unique collections, and ordered groups of unique elements. In order to support these various goals, the Set interface and its implementation classes include semantics that closely mirror the concepts and characteristics of set theory developed by Georg Cantor.

In this continuation of The Developer's Guide to Collections series, we explore the Set interface and its associated implementations, paying special attention to the mathematical underpinnings that the interface mimics. We also take a look at the hierarchy that turns the Set interface into workable and usable implementation classes. Lastly, we compare the advantages and disadvantages of each of the implementation classes and under what conditions each is best suited. In order to start this deep-dive, we must start with the most simple, yet fundamental aspect of sets in the JCF: Set theory.

Categories: Java

Configuring HTTPS for Use With Servlets [Snippet]

Javalobby Syndicated Feed - Mon, 19-Feb-18 14:01

Configuring your Java EE application to communicate over HTTPS requires a few lines of XML in the web.xml file.

The web.xml file is located in the WEB-INF directory of your project and is usually created automatically when your IDE generates a Java EE web application. If it is not, you can create it yourself.

Categories: Java

IntelliJ IDEA 2018.1 EAP: Inline External Annotations and Java Compiler Enhancements

Javalobby Syndicated Feed - Mon, 19-Feb-18 10:01

A new IntelliJ IDEA 2018.1 EAP build is here now and it’s ready for your assessment! Download the latest build from our website, or update via the Toolbox App.

As you may already know, IntelliJ IDEA provides an ability to annotate your code via annotations.xml files, which are stored outside of your source code. These external annotations are especially useful when direct annotation of the source code is not possible (library classes). IntelliJ IDEA shows the icon in the gutter near the externally annotated code. With the upcoming IntelliJ IDEA 2018.1, this functionality has been extended, and now the IDE shows these external annotations inline in your code.

Categories: Java

Getting Started With Java 9 Modules/Project Jigsaw [Video]

Javalobby Syndicated Feed - Mon, 19-Feb-18 04:01

Much has been written about the much anticipated Project Jigsaw/Java 9 modules already.

But what if you want a simple, practical introduction of how to get started with your first self-made Java 9 modules?

Categories: Java

Java 10 and the Emerging Problem of Release Fatigue

Javalobby Syndicated Feed - Mon, 19-Feb-18 01:01

Last year, our most popular blog post by far was The Legacy Developer’s Guide to Java 9. With Java 10 around the corner, it was natural for us to consider writing up a “Legacy Developer’s Guide to Java 10.” However, in researching the topic, it became clear that Java 10 just isn’t that exciting, and that there’s really no reason for legacy developers to worry about how they can take advantage of it or even whether they need to think about it.

The problem with Java 10 reflects broader issues relating to software release cadences in Java and beyond. I would suggest that this is a consequence of the accelerated Java release cadence that Oracle announced last year. It used to be that new Java releases came out every couple of years – enough time to implement a checklist of significant new features. The release managers had to juggle a number of factors, including providing enough time to get enough features completed and into the release, and not taking so much time that people got frustrated waiting for the new release. Inevitably, some overly ambitious features would be postponed to a later release or discarded.

Categories: Java

Hibernate/GORM: Solving the N+1 Problem

Javalobby Syndicated Feed - Sun, 18-Feb-18 22:01

Many developers who work with Hibernate or any other ORM framework eventually run into the so-called N+1 problem.

Our team faced it when we were working on a project using Grails. For its ORM, Grails uses a GORM "under the hood" that contains the same old Hibernate. In case you haven't encountered this problem yet, let’s give you the gist of it. Let's say we have the following perfectly typical scheme: "News - Comment(s)".

Categories: Java

CDI (Part 4): Lazy Initialization

Javalobby Syndicated Feed - Sat, 17-Feb-18 23:01


This is the Part 4 of the CDI Series in Java that contains:

Categories: Java

The Developer's Guide to Collections: Lists

Javalobby Syndicated Feed - Fri, 16-Feb-18 22:01

Of all the collection types in Java, the list is arguably the most popular due to its ordered characteristics, allowing developers to insert and retrieve elements at a specified index and to sort elements according to specified criteria. In this article, we continue The Developer's Guide to Collections series with an examination of the list collection, focusing on the conceptual idea of a list in Java, the List interface and how its overridden methods differ from those of the Collection interface, supporting interfaces, and the concrete list types (and their respective advantages and disadvantages).

The reader should note that this article uses the common mathematical convention for indices, where is some arbitrary index, n is the size of the indexed entity, square brackets are used to denote inclusive ranges, and parentheses are used to denote exclusive ranges. Also note that while this article uses notation and illustrations that are normally associated with linked lists (such as previous and next relationships), not all lists are implemented as linked lists; some lists may internally use arrays while others are free to use whichever internal implementation is most suited for the task at hand. 

Categories: Java

Spring MVC Exception Handling and Monitoring With Rollbar

Javalobby Syndicated Feed - Fri, 16-Feb-18 14:30

The Spring Famework is the most popular framework for Java according to hotframeworks.com. It provides a model view controller (MVC) architecture and readily available components to develop flexible and loosely coupled web applications.

If you are new to Rollbar, it helps you monitor errors in real-world applications. It provides you with a live error feed from the application, including complete stack traces and request data from the browser to debug errors quickly. It lets you easily understand user experience by tracking who is affected by each error. Learn more about our Java error monitoring product features.

Categories: Java

Variable Shadowing and Hiding in Java

Javalobby Syndicated Feed - Fri, 16-Feb-18 14:01

Java allows us to declare a variable whenever we need it. We can categorize all our variables into three categories, which have different-different scopes:

  1. Instance variables: defined inside a class and have object-level scope.
  2. Class variables: defined inside a class with the static keyword. They have class-level scope common to all objects of the same class.
  3. Local variables: defined inside a method or in any conditional block. They have block-level scope and are only accessible in the block where they are defined.


Categories: Java

A Practical Explanation for Initialization Modifiers in Kotlin

Javalobby Syndicated Feed - Fri, 16-Feb-18 10:01

Recently, I held a Kotlin Workshop for a client. The workshop's central idea was to spend a couple of hours showing the language itself, going individually through some of its capabilities, and, in the second half, developing a full app able to connect to an API, fetch data, storing it, and displaying it in a RecyclerView.

I found this workshop concept to be apt for an audience new or just starting with Kotlin. Most of the initial concepts are explored or at least touched on, and you have an app with an initial architecture and approach that can, later on, be extended and reused in other professional projects.

Categories: Java

Scala: Lazy Evaluation [Presentation]

Javalobby Syndicated Feed - Fri, 16-Feb-18 04:01

As per Wikipedia, “Lazy Evaluation is an evaluation strategy which delays the evaluation of an expression until its value is needed.” And today most of the modern programming languages support Lazy Evaluation. In contrast with strict or eager evaluation, which computes values as soon as possible, lazy evaluation can certainly deliver a few benefits, such as:

  • Lazy evaluation can help to resolve circular dependencies

Categories: Java

3 Things Every Java Developer Should Stop Doing

Javalobby Syndicated Feed - Fri, 16-Feb-18 01:01

From returning null values to overusing getters and setters, there are idioms that we as Java developers are accustom to making, even when unwarranted. While they may be appropriate in some occasions, they are usually forces of habit or fallbacks that we make to get the system working. In this article, we will go through three things that are common among Java developers, both novice and advanced, and explore how they can get us into trouble. It should be noted that these are not hard-and-fast rules that should always be adhered to, regardless of the circumstances. At times, there may be a good reason to use these patterns to solve a problem, but on the whole, they should be used a lot less than they are now. To start us off, we will begin with one of the most prolific, but double-edged keywords in Java: Null.

1. Returning Null

Null has been the best friend and worst enemy of developers for decades and null in Java is no different. In high-performance applications, null can be a solid means of reducing the number of objects and signaling that a method does not have a value to return. In contrast to throwing an exception, which has to capture the entire stack trace when it is created, null serves as a quick and low-overhead way to signal clients that no value can be obtained.

Categories: Java

Java 8: Bastion of Long-Term Support

Javalobby Syndicated Feed - Thu, 15-Feb-18 22:01

Stephen Colebourne's post "Java 9 has six weeks to live" starts, "Java 9 is obsolete in just six weeks." Colebourne references the Mark Reinhold blog post "Moving Java Forward Faster" and writes, "The new Java release train means that there will be a new release of Java every six months. And when the next release comes out, the previous release is obsolete." Colebourne points out that those still on Java 8 can enjoy this "current LTS (long-term support) release until the next LTS release occurs (Java 11)." However, for those who have already moved to Java 9, different choices must be made and Colebourne outlines these choices at a high level. Colebourne outlines several types of dependencies that must also move forward every six months and concludes, "I think it's fair to say that it's a bold choice to use Java 9 or 10."

As a reminder, the aforementioned Reinhold blog post "Moving Java Forward Faster" outlines how the new proposed release train addresses "the tension between developers, who prefer rapid innovation, and enterprises, which prefer stability, and the fact that everyone prefers regular and predictable releases." The following are key points of this new release train approach:

Categories: Java

JDK 9: NotNullOrElse Methods Added to Objects Class

Javalobby Syndicated Feed - Thu, 15-Feb-18 14:01

JDK 9 added some new methods to the Objects class including two static methods highlighted in this post: requireNonNullElse(T,T) and requireNonNullElseGet(T obj,Supplier<? extends T> supplier). Both methods make it easier to verify that a given object is notnull and to provide an alternative if the provided variable turns out to be null. As such, these methods and the similar methods introduced to Objects in earlier JDK versions [requireNonNull(T), requireNonNull(T,String), and requireNonNull(T,Supplier<String>)] are most likely to be used to implement guard clauses in methods.

The three methods mentioned in the last paragraph that were added to Objects prior to JDK 9 did not allow a "default" value to be used when the object being tested was determined to be null. Instead, each of these three methods throws a NullPointerException when the variable passed to them is null. The two methods added to Objects in JDK 9 do allow a default to be specified that can be returned by the method rather than the method throwing a NullPointerException.

Categories: Java

Java Class Metadata: A User Guide [Presentation]

Javalobby Syndicated Feed - Thu, 15-Feb-18 14:01

Last week I presented a talk on the subject of Java Class Metadata at FOSDEM 2018 in the Free Java Room. In my presentation I explained:

  1. What Java Class Metadata is
  2. Why it helps to know about it
  3. What you might do to measure it and reduce the impact of the metadata’s footprint on your Java application

The “too long — didn’t read” summary is:

Categories: Java

This Week in Spring: Spring Cloud and Reactive

Javalobby Syndicated Feed - Thu, 15-Feb-18 10:01

Hi, Spring fans! Welcome to another installment of This Week in Spring! This week, I’ve been in Munich and Frankfurt, Germany, and Minneapolis, Minnesota, and it looks like I’ll be in New York City and San Francisco for the balance of the week.

We’ve got a lot to cover so without further ado so let’s get started.

Categories: Java

Java Quiz 11: Branching Statements

Javalobby Syndicated Feed - Thu, 15-Feb-18 04:01

Before we start with this week's quiz, here is the answer to Java Quiz 10: Demonstrating An Anonymous Class

  1. The statement System.out.print(ac.i); writes the value of the instance variable of the class AnonymousClass. So, it writes 5 to the standard output.

Categories: Java

Programming BS: Null

Javalobby Syndicated Feed - Thu, 15-Feb-18 01:01

I used to watch a TV show called "Penn & Teller Bullsh**!", where a couple of entertainers named Penn and Teller would discuss various issues. At the outset of each episode, Penn would briefly introduce the topic and then point his finger at the screen and say "<topic> is bullshit!", with extra emphasis on the last word.

Ever read the article about how NULL is the billion-dollar mistake? I understand the sentiment of null's inventor, and I both agree and disagree. I disagree because the problem isn't null itself, but rather the way we have supported it in languages — and due to that support, I agree we are better off without it.

Categories: Java

Thread Slivers eBook at Amazon

Syndicate content