background preloader

Jvm

Facebook Twitter

JsonParser (JSR 353 (JSON Processing) API 1.0-SNAPSHOT API) JDHT: Java Distributed Hash Table or Java DKS Hash Table. JGroups - The JGroups Project. Javolution. Why NULL is Bad? A simple example of NULL usage in Java: What is wrong with this method?

Why NULL is Bad?

It may return NULL instead of an object — that’s what is wrong. NULL is a terrible practice in an object-oriented paradigm and should be avoided at all costs. There have been a number of opinions about this published already, including Null References, The Billion Dollar Mistake presentation by Tony Hoare and the entire Object Thinking book by David West. Java Method Logging with AOP and Annotations. Sometimes, I want to log (through slf4j and log4j) every execution of a method, seeing what arguments it receives, what it returns and how much time every execution takes.

Java Method Logging with AOP and Annotations

This is how I’m doing it, with help of AspectJ, jcabi-aspects and Java 6 annotations: This is what I see in log4j output: Nice, isn’t it? Now, let’s see how it works. Annotation with Runtime Retention. Date/Time Formatting/Parsing, Java 8 Style. Since nearly the beginning of Java, Java developers have worked with dates and times via the java.util.Date class (since JDK 1.0) and then the java.util.Calendar class (since JDK 1.1).

Date/Time Formatting/Parsing, Java 8 Style

During this time, hundreds of thousands (or maybe millions) of Java developers have formatted and parsed Java dates and times using java.text.DateFormat and java.text.SimpleDateFormat. Given how frequently this has been done over the years, it’s no surprise that there are numerous online examples of and tutorials on parsing and formatting dates and times with these classes. The classic Java Tutorials cover these java.util and java.text classes in the Formatting lesson (Dates and Times). Exceptions Hierarchy and Custom Exceptions in Java. Exceptions are a vital part of every modern programming language, as it is a way to handle error cases and unexpected behavior, without necessarily terminating the whole application.

Exceptions Hierarchy and Custom Exceptions in Java

Except for that, by handling the exceptions in the correct way, we can get meaningful feedback regarding what went wrong, why and where. As you can understand, it is very important for every Java programmer to have a good grasp of how Exceptions work. In this post, we are going to take a look at the exception hierarchy, and explain the different kinds of exceptions that exist, the connections between each other and the different ways to approach them. When the Java 8 Streams API is not Enough. Java 8 was – as always – a release of compromises and backwards-compatibility.

When the Java 8 Streams API is not Enough

A release where the JSR-335 expert group might not have agreed upon scope or feasibility of certain features with some of the audience. See some concrete explanations by Brian Goetz about why … But today we’re going to focus on the Streams API’s “short-comings”, or as Brian Goetz would probably put it: things out of scope given the design goals. Scala and Java 8 type inference in higher order functions sample. One of the concepts mentioned in the Functional Programming in Scala is about the type inference in higher order functions in Scala and how it fails in certain situations and a workaround for the same.

Scala and Java 8 type inference in higher order functions sample

So consider a sample higher order function, purely for demonstration: Ideally, passing in a list of say integers, you would expect the predicate function to not require an explicit type: Type inference does not work in this specific instance however, the fix is to specify the type explicitly: Or a better fix is to use currying, then the type inference works! I was curious whether Java 8 type inference has this issue and tried a similar sample with Java 8 Lambda expression, the following is an equivalent filter function: Getters/Setters. Evil. Period. There is an old debate, started in 2003 by Allen Holub in this Why getter and setter methods are evil famous article, about whether getters/setters is an anti-pattern and should be avoided or if it is something we inevitably need in object-oriented programming.

Getters/Setters. Evil. Period.

I’ll try to add my two cents to this discussion. The gist of the following text is this: getters and setters is a terrible practice and those who use it can’t be excused. Again, to avoid any misunderstanding, I’m not saying that get/set should be avoided when possible. No. I’m saying that you should never have them near your code.

This is the Final Discussion! Pun intended… Let’s discuss Java final.

This is the Final Discussion!

Recently, our popular blog post “10 Subtle Best Practices when Coding Java” had a significant revival and a new set of comments as it was summarised and linked from JavaWorld. In particular, the JavaWorld editors challenged our opinion about the Java keyword “final“: More controversially, Eder takes on the question of whether it’s ever safe to make methods final by default:“If you’re in full control of all source code, there’s absolutely nothing wrong with making methods final by default, because:”“If you do need to override a method (do you really?) , you can still remove the final keyword”“You will never accidentally override any method anymore”

Lambdas and side effects. Overview Java 8 has added features such as lambdas and type inference.

lambdas and side effects

This makes the language less verbose and cleaner, however it comes with more side effects as you don’t have to be as explicit in what you are doing. The return type of a lambda matters. 3 Examples of Parsing HTML File in Java using Jsoup. HTML is the core of the web, all the pages you see on the internet are based on HTML, whether they are dynamically generated by JavaScript, JSP, PHP, ASP or any other web technology.

3 Examples of Parsing HTML File in Java using Jsoup

Your browser actually parse HTMLs and render it for you. But what do you do, if you need to parse an HTML document and find some elements, tags, attributes or check if a particular element exists or not, all that using a Java program. If you have been in Java programming for some years, I am sure you have done some XML parsing work using parsers like DOM and SAX.

From Java 7 Futures to Akka actors with Scala. Garbage Collection: increasing the throughput. The inspiration for this post came after stumbling upon “Pig in the Python” definition in the memory management glossary. Apparently, this term is used to explain the situation where GC repeatedly promotes large objects from generation to generation. The effect of doing so is supposedly similar to that of a python swallowing its prey in whole only to become immobilised during digestion. For the next 24 hours I just could not get the picture of choking pythons out of my head. As the psychiatrists say, the best way to let go of your fears is to speak about them. Property-based testing with Spock. Property based testing is an alternative approach to testing, complementing example based testing. The latter is what we’ve been doing all our lives: exercising production code against “examples” – inputs we think are representative.

Picking these examples is an art on its own: “ordinary” inputs, edge cases, malformed inputs, etc. But why are we limiting ourselves to just few examples? Why not test hundreds, millions… ALL inputs? There are at least two difficulties with that approach: Processing Java Annotations Using Reflection. In my previous article covering Java Annotations, I outlined a recent use case and provided you with some examples of custom annotations and how they might be used.

In this article, I’m going to take that a step further and give you a few examples of custom annotations and how you would process these custom annotations using the Java Reflection API. Once you have gone through this tutorial, you should come away with a better understanding of the simplicity and flexibility that custom annotations can provide. So let’s dig into the code! Custom Annotation Listings. How Nashorn Impacts API Evolution on a New Level. Following our previous article about how to use jOOQ with Java 8 and Nashorn, one of our users discovered a flaw in using the jOOQ API as discussed here on the user group. In essence, the flaw can be summarised like so: Java code JavaScript code. Java EE 7 with Angular JS – CRUD, REST, Validations – Part 2. This is the promised follow up to the Java EE 7 with Angular JS – Part 1. It took longer than I expect (to find the time to prepare the code and blog post), but it’s finally here!

The Application. Java RunnableFuture Example. Joining Strings in JDK 8. RE2/J: Linear-time regular-expression matching for Java. Spring Rest API with Swagger – Creating documentation. Custom Reason Phrase in HTTP status error message response with JAX-RS (Jersey) Beyond Thread Pools: Java Concurrency is Not as Bad as You Think. Using Google Guava Cache for local caching. Top 10 Most Common Java Performance Problems. Using Groovy for Bash (shell) Operations. Java.util.concurrent.Phaser Example. Java8 Sorting – Performance Pitfall. Disambiguating between instances with Google Guice.

Thread.tss?thread_id=80365&utm_content=buffer7791c&utm_medium=social&utm_source=twitter. The Optimum Method to Concatenate Strings in Java. Fork/Join Framework vs. Parallel Streams vs. ExecutorService: The Ultimate Fork/Join Benchmark. Fast Conservative Garbage Collection. Java 8 pitfall - Beware of Files.lines()