Java and Technology weblog
Java8 isn’t scheduled for for release until March 2014, but early release versions have been available for a while.
Some of the most interesting new features are:
- Functional interfaces
- Default methods
- Java Time
I started writing a blog post on what’s new in the upcoming Java8 release, and thought I would start by doing a quick look back at what Java7 brought to us.
Java7 was released back in July 2011, and was described as “more evolutionary than revolutionary”.
“There are some significant improvements, but no really earth-shattering or ground-breaking kinds of features.” – Oracle chief Java architect Mark Reinhold
It didn’t contain the much hyped lambda expressions for example. Still, it did bring a lot of other neat additions to the Java language. You can see the more at http://docs.oracle.com/javase/7/docs/, or read on for my summary.
I recently started trying out the early access version of Java8. When I tried using it with maven in IntelliJ however, I got the following error when I ran the maven install command:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project xyz:
Fatal error compiling: invalid target release: 1.8\
Although I saw some suggestions on stackoverflow here about hardcoded JAVA_HOME paths, the solution was just changing the maven specific version of Java that IntelliJ uses. This can be done via:
File > Settings > Project Settings > Maven > Runner
and selecting the 1.8 version of the JRE.
I have been working on some Java code recently that required both a stack and a queue. The choices to use aren’t immediately obvious. There is a Queue interface, but no clear concrete implementation to use. There is also a Stack class, but the javadocs point out that other classes “should be used in preference to this class”. So, what implementation do you use for stacks and queues in Java?
Problem 3 in Project Euler is as follows:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
Problem 2 in Project Euler is as follows:
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
ThoughtWorks Technology Radar is out and definitely worth a read. Although I follow many of the ThoughtWorks folks, like Martin Fowler, Neal Ford and Jez Humble, this is the first time I have read their tech radar, but it won’t be the last.
I’ve found the builder design pattern occasionally useful in code, but frequently useful in tests. This article is a quick summary of the pattern in general, followed by look at a working example of using it in tests. See the code in github.
Immutable classes are inherently thread safe, and can only ever be in a single state. A class can be made immutable by:
- all fields being final and private
- no mutator (setter) methods
- class can’t be extended (e.g. make final) to avoid subclasses making things mutable
- provide exclusive access to any mutable components (e.g. getters provide defensive copies of collections)
See Item 15:Minimize mutability in Effective Java for more details.