Java Lobby Announcements

Subscribe to Java Lobby Announcements feed
Recent posts in Java on DZone.com
Updated: 10 hours 9 min ago

Useful Scala Compiler Options: Part 2

Mon, 2016-08-29 15:31

Scala offers a number of advanced language features that are disabled by default. If you try to use any of them, the compiler will generate a warning informing you of their usage. There are a number of reasons that you might want to enable features (or keep them disabled). In this post, we will explore what each language feature is and under which circumstances you might want to enable them. (For a discussion of basic Scala Compiler options that we find useful at Threat Stack, see Part 1 of this series.)

Language Feature Warnings

To start with, you should always enable the -feature flag. This flag tells scalac to provide information about misused language features. It can be useful for discovering what advanced language features are being used, but have not been enabled. For example, if we have a simple definition with a higher kind:

Categories: Java

Using @Named vs. @ManagedBean

Mon, 2016-08-29 12:31

I was looking through some JSF controllers and it was a mix of @Named and @ManagedBean annotations. These annotations provide similar dependency injections, but there are important differences.

Containers
  • @ManagedBean – javax.faces.bean.ManagedBean – managed by JSF container
  • @Named – javax.faces.bean – CDI bean managed by application server

This means @Named beans are visible to the whole JEE container, while @ManagedBean are visible only to the JSF container. The visibility issue is covered in this table:

Categories: Java

Best Practices for Code Documentation in Java

Mon, 2016-08-29 10:31

Code documentation is a necessary evil that every developer has to deal with. Almost every programming language has different ways of approaching code documentation, and today, we will discuss the best practices of code documentation in Java. With Java, anyone can easily create enterprise-level applications. It is object-oriented in nature and is suited for developing any modern web application. Not to mention it can also be used in creating web apps, games, desktop applications, mobile applications, etc.

There are many ways of approaching code documentation in Java, but they do have a caveat. Not every approach can be equated to good practice, but that doesn’t mean you cannot work with anything besides best practices. Feel free to change the paradigm if your project needs it.

Categories: Java

Spring Data, MongoDB, and Java 8

Mon, 2016-08-29 08:30

While saving an object containing the new Java 8 java.time.LocalDateTime, the following error is thrown:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.time.LocalDateTime] to type [java.util.Date]

We are testing:

Categories: Java

Memory Leaks and Java Code

Mon, 2016-08-29 07:31

Java implicitly reclaims memory by GC (a daemon thread). GC periodically checks if there is any object which is unreachable or, to be precise, has no reference pointing to that object. If so, GC reclaims the newly-available memory.

Now the question is should we worry about memory leaks or how Java handles it?

Categories: Java

Why I Started Learning Emacs in 2016

Sun, 2016-08-28 12:31

The main role of a good editor is to not get into your way while working, and maybe even help a bit from time to time. If you are a programmer, chances are you are spending a large amount of time using one — and that’s why it’s so important to choose a good editor. Learning a new editor is always a big investment — significant time passes before you tame your chosen tool and adjust it to your needs.

I’m a long-time IntelliJ Idea user, and it’s great for Scala/Java development, probably the best in market. But then, in some ways it feels a bit heavy. Even for the tiniest experiment you have to create a “project”, with its own window, meta-data directory etc. You go through a short wizard, and then it has to be imported and indexed, and re-imported, re-indexed, …. Maybe it doesn’t sound like much, but it’s definitely not a “lightweight” experience.

Categories: Java

Optional Parameters Handling Strategy in Java

Sun, 2016-08-28 10:31

Often we face a situation where we need to design an object that expects a lot of parameters from the client. Some parameters are required and some are optional. In this article, we will focus on various strategies by which we can design such objects, as well as their pros and cons.

Strategy 1. Telescopic Constructors

To design such objects, we can use a chain of overloading constructors. First, a minimal constructor is taking only the required parameters, then delegate calls another constructor, which takes an optional parameter.

Categories: Java

Understand Java Callable and Future

Sun, 2016-08-28 08:31

In Java multithreading programs, we extensively use Java Callable and Future. I believe all of you have the basic understanding of threads. In brief, The thread is a separate path of execution, so if you have to do a repetitive task, you can break the work into multiple chunks (tasks) and assign them to threads. Multiple Threads will execute tasks in parallel to get the result quickly.

In Java 5, java.util.concurrent was introduced. The callable interface was introduced in concurrency package, which is similar to the Runnable interface, but it can return any object, and is able to throw an Exception.

Categories: Java

Convert Map to List in Java 8

Sat, 2016-08-27 12:31

A Java 8 example to convert a Map to a List

ConvertMapToList.javapackage com.mkyong.example; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class ConvertMapToList { public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(10, "apple"); map.put(20, "orange"); map.put(30, "banana"); map.put(40, "watermelon"); map.put(50, "dragonfruit"); System.out.println("\n1. Export Map Key to List..."); List<Integer> result = map.entrySet().stream() .map(x -> x.getKey()) .collect(Collectors.toList()); result.forEach(System.out::println); System.out.println("\n2. Export Map Value to List..."); List<String> result2 = map.entrySet().stream() .map(x -> x.getValue()) .collect(Collectors.toList()); result2.forEach(System.out::println); } }

Output

Categories: Java

Groovy Gotcha With the Every() Collection Method

Sat, 2016-08-27 10:31

In the Groovy programming language, every object that implements the Iterable interface (such as List and Map objects) comes with the every() method.  The every() method takes a closure as an argument and is supposed to evaluate whether every item in the collection meets the condition set forth in the closure:

assert [ 3, 4 ].every{ element -> element > 2 } //true assert [ fingerCount: 10, toeCount: 10 ].every{ key, value -> value == 10 } //true

Given that, you might reasonably expect that the every() method would return false when executed against an empty collection. But it doesn't:

Categories: Java

The Most Popular Programming Languages of 2016

Sat, 2016-08-27 08:31

Our annual look at the relative popularity of programming languages reveals that while Java still rules, other options are catching up for significant groups of developers. Just as important, more and more developers are taking a polyglot approach, leveraging multiple languages according to which one best solves a particular problem.

Choosing a Language Ain’t Easy

Software developers know that choosing the best programming language to use — for your project or for your career — can be critically important but often challenging. The criteria can be confusing and the options difficult to determine. Even the questions you need to ask aren’t always clear:

Categories: Java

PowerShell on Linux? No, Thank You [comic]

Sat, 2016-08-27 07:01
Categories: Java

Using ANTLR From the Parse Tree to the Abstract Syntax Tree

Fri, 2016-08-26 15:46

In this post we are going to see how to process and transform the information obtained from the parser. The ANTLR parser recognizes the elements present in the source code and build a parse tree. From the parse tree we will obtain the Abstract Syntax Tree which we will use to perform validation and produce compiled code.

Note that the terminology can vary: many would call the tree obtained from ANTLR an Abstract Syntax Tree. I prefer to mark the difference from this two steps. To me the parse tree is the information as meaningful to the parser, the abstract syntax tree is the information reorganized to better support the next steps.

Categories: Java

3R Principles of Programming

Fri, 2016-08-26 15:31
Introduction

Programming is an Art. When we program, often we think that programs are a set of instructions fed to a computer for solving business use cases. Though it is true, programs also serve more than that in the real world.

Let us start with a code snippet that runs without any problems.

Categories: Java

The Java Synchronizers

Fri, 2016-08-26 12:31

Threads communication happens primarily by sharing access to fields and objects. Although extremely efficient, this form of communication is prone to errors such as thread interference and memory consistency. Synchronization is a tool that helps to prevent such errors.

However, synchronization does not come for free and can introduce latency when accessing a lock or object that is currently being held by another thread. The waiting thread cannot use that object until the other thread releases the lock on the object. This condition is known as thread contention. It may also lead to deadlocks and livelocks.

Categories: Java

Custom Annotation in Java for SQL Injection Safe Parameters

Fri, 2016-08-26 10:31

Hi Java Developers, In the today’s world of ORM, we are mostly using JPA based implementations for our persistence.

We also use JPA over Hibernate, Spring Data, or Spring JDBC templates quite often.

Categories: Java

Custom Annotation in Java for SQL Injection Safe Parameters

Fri, 2016-08-26 10:31

Hi Java Developers, In the today’s world of ORM, we are mostly using JPA based implementations for our persistence.

We also use JPA over Hibernate, Spring Data, or Spring JDBC templates quite often.

Categories: Java

Introducing Redisson Live Objects (Object Hash Mapping)

Fri, 2016-08-26 08:31
What is Redisson?

Redisson is a Redis Java library that provides distributed Java objects and services including Set, Multimap, SortedSet, Map, List, Queue, BlockingQueue, Deque, BlockingDeque, Semaphore, Lock, AtomicLong, CountDownLatch, Publish / Subscribe, Bloom filter, Remote service, Spring cache, Executor service, Live Object service and most recently added Scheduled executor service on top of the Redis server.

What is a Live Object?

A Live Object can be understood as an enhanced version of standard Java object, of which an instance reference can be shared not only between threads in a single JVM, but can also be shared between different JVMs across different machines. Wikipedia discribes it as:

Categories: Java

Introducing Redisson Live Objects (Object Hash Mapping)

Fri, 2016-08-26 08:31
What is Redisson?

Redisson is a Redis Java library that provides distributed Java objects and services including Set, Multimap, SortedSet, Map, List, Queue, BlockingQueue, Deque, BlockingDeque, Semaphore, Lock, AtomicLong, CountDownLatch, Publish / Subscribe, Bloom filter, Remote service, Spring cache, Executor service, Live Object service and most recently added Scheduled executor service on top of the Redis server.

What is a Live Object?

A Live Object can be understood as an enhanced version of standard Java object, of which an instance reference can be shared not only between threads in a single JVM, but can also be shared between different JVMs across different machines. Wikipedia discribes it as:

Categories: Java

The Evolution and Future of IDEs

Thu, 2016-08-25 15:31

Being a developer today is perhaps more exhilarating than at any other time in history. We have incredible choice when it comes to languages and frameworks, and tools like GitHub and StackOverflow have connected millions to make sharing code and expertise simple and fast. These conveniences have allowed us all to spend more of our time being creative and honing our craft, rather than fighting with source code repos and ancient languages. But in this age of global sharing and constant collaboration, one of our most important development tools, the IDE, has remained stubbornly individual and private. Why?

IDEs are very personal — perhaps to a fault. Before developers can start work on a project, we have to set up our IDE’s workspace, from configuring the project to marrying that workspace to a runtime that we download and configure separately. If the runtime is using a desktop or laptop, we may have to contend with resource limitations and can’t easily share their runtime with others (in fact the steps we took to make it work might not even work for someone else). If the runtime is shared on a Virtual Machine (VM), then it’s likely a large one that’s hard to keep synchronized with the ever-evolving codebase and dependencies. Today, when most developers contribute to many projects (often simultaneously), this setup tax becomes prohibitive — and seems to go against the sharing and collaboration that’s swept the rest of the development tooling world.

Categories: Java

Pages