Allocation elimination: when the Java keyword “new” doesn’t create an object

Which of these two methods for calculating the sum of the first N integers do you think runs the fastest? The answer may surprise you.

Method 1:

public Long sumPrimitive() {
    long total = 0;
    for (int i = 0; i < LOOP_SIZE; i++) {
        total += i;
    }
    return total;
}

Method 2:

public Long sumMutableWrapper() {
    long total = 0;
    for (int i = 0; i < LOOP_SIZE; i++) {
        MutableWrapper mutableWrapper = new MutableWrapper(total);
        mutableWrapper.add(i);
        total = mutableWrapper.value;
    }
    return total;
}

private static class MutableWrapper {
    private long value;

    public MutableWrapper(long value) {
        this.value = value;
    }

    public void add(long other) {
        value += other;
    }
}

Continue reading “Allocation elimination: when the Java keyword “new” doesn’t create an object”

The cost of object creation in Java, including garbage collection

NOTE: The research and views presented below are mine and do not necessarily reflect those of my employer.

I sometimes see advice that object creation in Java is slow and should be avoided. The common counter argument is that it’s not slow, and even if it was slow it should be avoided only after causing a proven performance issue. I always liked the counter argument, to put it to the test I did experiments on how much time a modern JVM takes for object creation and subsequent garbage collection.

Continue reading “The cost of object creation in Java, including garbage collection”

How to Avoid Static Helper Classes

Static helper classes are classes which contain only static methods, often having names that end in Helper or Utils. The existence of such a class usually indicates a sub-optimal design. In this post I’ll talk about why these classes are a poor design choice in an object oriented language such as Java and how to refactor to a better design.

How to avoid static helpers…

The Best Advanced Java Books

I’ve spent some time trying to find technical books that are good next level reading after classics like Effective Java and Design Patterns (Gang of Four). These books are must reads for any engineer that is serious about improving their skills, but where should a Java developer look to after these? Below are a few more advanced books that I’ve found especially useful for Java software construction. Note: purchasing items through these links supports this website.

See recommended advanced Java books…

Simplify Code by Encapsulating Collections

A quick survey: Below are two methods that return school district ratings given a list of houses. Which method do you find more to the point?

Method A
public List<Rating> getDistrictRatings(List<House> houses, Price maxPrice) {
  Set<SchoolDistrict> districts = houses.stream()
      .filter(house -> house.price().isLessThan(maxPrice))
      .map(house -> house.getSchoolDistrict())
      .collect(Collectors.toSet());

  return ratingService.rateDistricts(districts);
}
Method B
public List<Rating> getDistrictRatings(Houses houses, Price maxPrice) {
  Set<SchoolDistrict>=> districts = houses.below(maxPrice)
      .getSchoolDistricts();
  return ratingService.rateDistricts(districts);
}

How to achieve the cleaner method…

How to avoid Object types in Java using the Adapter Pattern

Using the Object type for a method parameter or instance variable in Java application code is a choice that will later haunt you. The temptation to use Object strikes when common functionality needs to be extracted from otherwise unrelated classes and it is impossible for these classes to share a common interface. Seeing this problem can indicate that a larger refactoring is in order, however for now we’ll focus on this specific issue.

How to avoid using Object…

Most Useful IntelliJ Refactorings for Cleaner Code

IntelliJ’s refactorings are a quick way to improve your Java code. In this post I detail the refactoring actions I find most commonly useful. Even small refactorings can greatly improve readability and make larger refactorings easier and more obvious. Many of these actions have multiple options that are worth exploring. For a more in depth discussion of refactoring I recommend Martin Fowler’s Refactoring: Improving the Design of Existing Code [affiliate link].

See refactorings…