Feed aggregator

Lambda Expressions in Java 8

Javalobby Syndicated Feed - Tue, 02-Jan-18 13:00

Let's start with lambda expressions. What are they? And how do they work?

I Googled lots of posts and YouTube videos before now to understand lambda expressions, but I found it difficult to understand because I haven't used any functional language before. So I decided to write a blog post to help people like me.

Categories: Java

IntelliJ IDEA 2017.3: Support for Kotlin Multi-Platform Projects

Javalobby Syndicated Feed - Tue, 02-Jan-18 10:01

The Kotlin plugin bundled with IntelliJ IDEA 2017.3 has been updated to Kotlin 1.2. The key new feature of Kotlin 1.2 is its support for multi-platform projects – projects where the same code is compiled to target the JVM or JavaScript. Such projects consist of common modules, containing platform-independent code, and platform-specific modules. To support calling the platform-specific APIs from common code, the common code can specify expected declarations, and then platform-specific modules need to provide the actual declarations. The IDE now supports creating and running multi-platform projects, as well as code editing and navigation assistance for expected and actual declarations.

Template Multi-platform Project

First of all, now it is possible to create a template project to start exploring the new Multi-platform projects feature.

Categories: Java

Caching Method Results With JCache

Javalobby Syndicated Feed - Tue, 02-Jan-18 04:01

In JCache, there is a handy functionality that transparently caches the result of methods. You can annotate methods of managed beans with @CacheResult, and the result of the first call will be returned again without calling the actual method a second time.

import javax.cache.annotation.CacheResult;
// ...

public class Calculator {

    @CacheResult
    public String calculate() {
        // do some heavy lifting...
        LockSupport.parkNanos(2_000_000_000L);

        return "Hi Duke, it's " + Instant.now();
    }
}


Categories: Java

Creating Strings in JMeter

Javalobby Syndicated Feed - Tue, 02-Jan-18 01:01

Strings, the most commonly used programming tool, are the main carrier of textual information in programs. This information is passed in the form of a sequence of characters. In Apache JMeter, Strings are used to output messages to the JMeter console.

For example, when we send an HTTP request using JMeter, we get a reply in the form of a byte array. In order for this response to become readable, it is converted to strings. When an API is tested and the specific data is cut out from the API response, for example when using the "Regular Expression Extractor", the cut out data will be represented as strings. These two examples are a small part of how widely strings are used, both in programming languages and in JMeter.

Categories: Java

Working With hashcode() and equals()

Javalobby Syndicated Feed - Mon, 01-Jan-18 22:01

By default, the Java super class java.lang.Object provides two important methods for comparing objects: equals() and hashcode(). These methods become very useful when implementing interactions between several classes in large projects. In this article, we will talk about the relationship between these methods, their default implementations, and the circumstances that force developers to provide a custom implementation for each of them.

Method Definition and Default Implementation

  • equals(Object obj): a method provided by java.lang.Object that indicates whether some other object passed as an argument is "equal to" the current instance. The default implementation provided by the JDK is based on memory location — two objects are equal if and only if they are stored in the same memory address.

Categories: Java

Generating and Mocking Data With MockNeat

Javalobby Syndicated Feed - Mon, 01-Jan-18 10:01

MockNeat is a new Java 8+ library that can be used to provision apps with initial sets of data that are valid from a business standpoint.

Data is arbitrarily generated by matching a set of criteria that can be programmatically defined, allowing developers enough flexibility to obtain specific arbitrary results matching their business needs.

Categories: Java

Configuring JMS in IBM WAS Liberty

Javalobby Syndicated Feed - Mon, 01-Jan-18 03:01

Quite recently, we completed a project that needed simple JMS capabilities. A user would initiate an action on our website, get a prompt (e.g. USSD) on his/her mobile, respond to it, and feed this response to the web app. See the sketch below.

The WebSocket server and JMS engine are WAS Liberty servers, the auditing component is a WAS ND service, and the web application a JavaScript SPA.

Categories: Java

A Log Message Is Executable Code and Comment

Javalobby Syndicated Feed - Sun, 31-Dec-17 22:01

Although there are differences of opinion regarding how many comments one should add to one's code, I think it's safe to say most developers would agree that the comment in the following code snippet is superfluous:

// increment the total
total++;


Categories: Java

12 Java YouTube Channels You Should Follow in 2018

Javalobby Syndicated Feed - Sat, 30-Dec-17 23:01

It’s almost the end of 2017. If you’re like me, you might be thinking about what you want to change or improve in 2018.

Maybe you want to learn more about Java and improve your development skills.

Categories: Java

What Is Project Amber in Java?

Javalobby Syndicated Feed - Fri, 29-Dec-17 23:01

In this post, we’re going to delve into some details of the features being targeted in Project Amber, which was introduced in early 2017 by its lead and language architect Brian Goetz. This project aims to add some really cool beans to the Java programming language that improve the developer’s productivity when writing Java code.

As this is still work in progress, other features could be added to the project in the future. Even though these features may seem to have been addressed late in the Java timeline, it’s worth considering that the Java team has historically been rather cautious in introducing new features to evolve the language, as Goetz explains in a talk about the project.

Categories: Java

Partially Applied Functions in Scala

Javalobby Syndicated Feed - Fri, 29-Dec-17 14:01

Scala, like many other functional languages, allows developers to apply functions partially. What this means is that, when applying a function, a developer does not pass in all the arguments defined by the function. But provides only for some of them, leaving remaining parameters to be passed later.

Once you have provided the required initial parameters, what you get back is a new function whose parameter list only contains those parameters from the original function that were left blank.

Categories: Java

Switch Expressions Coming to Java?

Javalobby Syndicated Feed - Fri, 29-Dec-17 10:01

A JEP draft has been created with the title, "Switch Expressions for the Java Language." The current "Summary" states, "Extend the switch statement so that it can be used as either a statement or an expression, and improve how switch handles nulls. These will simplify everyday coding, as well as prepare the way for the use of pattern matching in switch." There are several promising characteristics of the proposed Java switch expression in its own right in addition to its enabling of the exciting Pattern Matching JEP (305).

The Java Tutorial defines a Java statement as a "complete unit of execution" that is "roughly equivalent to sentences in natural languages." It defines a Java expression as "a construct made up of variables, operators, and method invocations ... that evaluates to a single value." The current Java switch is a statement, but this draft JEP proposes that switch be supported as an expression as well.

Categories: Java

This Year in Spring: 2017

Javalobby Syndicated Feed - Fri, 29-Dec-17 04:01

Hi, Spring fans! Welcome to another, albeit very special, installment of This Week in Spring! This is the last installment before 2018 and so we’ll take the opportunity to review, as we always do in the last installment of the year, this very exciting year in Spring and its ecosystem.

Let me, on behalf of the Spring and larger Pivotal team, be the first to wish you and yours a heartfelt happy and safe New Year! Let’s first look at some of the major trends that defined 2017. We’ve got a lot to cover!

Categories: Java

Start a New MVC Spec Project With MongoDB

Javalobby Syndicated Feed - Fri, 29-Dec-17 04:01

The MVC, from Wikipedia, is a software architectural pattern that divides a given application into three interconnected parts. It is done to separate internal representations of information from the ways information is presented to, and accepted from, the user.

The MVC design pattern decouples these significant components, allowing for efficient code reuse and parallel development. MVC web frameworks can be categorized either as action-based or component-based. For decades, the Java Community was waiting for a specification that is action-based. Of course, the Java EE world already has Java Server Faces as a component-based. Finally, the action-based MVC is coming with Ozark.

Categories: Java

About Dependency Injection

Javalobby Syndicated Feed - Fri, 29-Dec-17 01:01

When I first heard about Dependency Injection at my first workplace, it sounded very fancy to me, and, somehow, a bit scary. And, in combination with Spring, it looked like black magic. But after reading a bit about it, I think this expression "dependency injection is a 25-dollar term for a 5-cent concept" describes it perfectly. I don’t want to say that it is a cheap pattern: It is not! It is extremely helpful and a must-have in software development. But it is simple, and its simplicity makes this pattern simply brilliant.

So What Is It?

Let’s see a simple example to understand better what this means. Let’s build a simple payment service for an online shop. We have a cart with a list of products, and we create a method that iterates all the products and uses a cash payment service to pay for them.

Categories: Java

Managing Randomness in Java

Javalobby Syndicated Feed - Thu, 28-Dec-17 04:01

If you already had to manage some degree of randomness on Java, chances are that you've become acquainted with the Math.random() methods. However, the previous method returns a double. Beyond very basic use-cases, another option has to be considered in the form of the java.util.Random class.

Random

An instance of this class is used to generate a stream of pseudorandom numbers.
Javadoc

This root class provides basic random-number-generation capabilities, nothing mind-blowing.

Categories: Java

Kotlin: Functional Exception Handling With Try

Javalobby Syndicated Feed - Thu, 28-Dec-17 01:01

Scala has a Try type to functionally handle exceptions. I wrapped my head around that concept by using the excellent Neophyte's guide to Scala by Daniel Westheide. This post will replicate that type using Kotlin.

Background

Consider a simple function that takes two Strings, converts them to integers, and then divides them (sample based on Scaladoc of Try):

Categories: Java

OOP Concepts for Beginners: What Is Polymorphism

Javalobby Syndicated Feed - Wed, 27-Dec-17 22:01

<!-- Google Tag Manager (noscript) --> <iframe src="https://www.googletagmanager.com/ns.html?id=GTM-PDSRGWC" height="0" width="0" style="display:none;visibility:hidden"></iframe>

The word polymorphism is used in various contexts and describes situations in which something occurs in several different forms. In computer science, it describes the concept that objects of different types can be accessed through the same interface. Each type can provide its own, independent implementation of this interface. It is one of the core concepts of object-oriented programming (OOP).

Categories: Java

Java Code Trick: Double Brace Initialization [Snippet]

Javalobby Syndicated Feed - Wed, 27-Dec-17 14:01

While coding for our current project, I had the following thought: "Is it possible somehow not to write the variable of the collection every time I want to add some elements to it?" So I searched if there is a way to do that and found out a solution that I assume is a code trick in Java. It is not very intuitive, but it was quite easy to use. Below, I will first share the standard initialization we do and then the code trick to show you how it improves the process.

Standard Initialization of a Collection in Java

Usually, we initialize a set this way:

Categories: Java

Remote Services Between Python and Java

Javalobby Syndicated Feed - Wed, 27-Dec-17 10:01

ECF's implementation of OSGi Remote Services allows multiple distribution providers, which are responsible for the actual RPC communication required by remote services. Here is a list of ECF distribution providers we've created.

Using Py4j and Google Protocol Buffers, we've recently enhanced an ECF distribution provider that allows the use of remote services (and Remote Service Admin) between OSGi and Python. Service impls can be in either Java or Python, and consumers can be either Java or Python. Protocol Buffers can be used to efficiently serialize arguments and return values.

Categories: Java

Thread Slivers eBook at Amazon

Syndicate content