Skip to content

java

Using StringBuilder instead of usual concatenation. When is it appropriate?

You might know that using string concatenation in Java is not a good practice as it might affect performance. In this short article, I will try to describe when it is necessary to use StringBuilder and when we can afford using concatenation (+ sign).

Let’s start with a simple example:

package com.imsavva;

public class ConcatenationTestApp {

    public static void main(String[] args) {
        String str = "abc";
        str = str + "def";
        str += "12" + "34" + str;

        System.out.println(str);
    }
}

To see how it works under the hood, let’s compile and decompile it. I tried this using Java 6 and Java 8.

Compiling the code:

javac src/com/imsavva/ConcatenationTestApp.java

Decompiling the class:

javap -c src/com/imsavva/ConcatenationTestApp

Continue reading to see what happens next.

Read More →

UnhandledExceptionHandler in Java: how to catch uncaught exceptions

UncaughtExceptionHandler in Java

In this short tutorial, I’m going to describe how to handle uncaught exceptions in threads. This can be done by:

  • Using threadInstance.setUncaughtExceptionHandler() ─ for a specific thread
  • Overriding ThreadGroup’s uncaughtException() method ─ for a thread group
  • Using Thread.setDefaultUncaughtExceptionHandler() ─ for all threads

If you’re interested in how to implement this, press “read more”, I’ve prepared an example.

Read More →

Parsing a date string to ISO8601 with Joda Time

Recently, I faced a date conversion task: convert a string date “yyyy-MM-ddZ” (i.e. “1983-09-15+03:00”) to the ISO8601 standard “yyyy-MM-dd’T’HH:mm:ss.SSSZ” (i.e. “1983-09-15T03:00:00.000+03:00”).

I used Apache Joda time to convert date. First, add a Maven dependency in pom.xml

<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.9.4</version>
</dependency>

Read More →

Achievement unlocked. Sololearn Java certificate.

Sololearn Java course

Yesterday I finished a Java course with the Sololearn iPhone appilcation. Just for fun. I had some free time and decided, why not to study Java on my mobile phone, so that’s how I found this app. What can I say about it? It gives very little knowledge about Java.

There are 6 sections in this course: basic concepts, conditionals and loops, arrays, classes and objects, more on classes, and the last one, exceptions, lists, threads and files. Each section contain a small piece of theory and some ordinary practical tasks. I wonder, how have developers decided to combine such large themes as collections and threads (and exceptions and files)?

I don’t know what’s the target audience of this app. If you’re a beginner, you probably shouldn’t download it, because it’d be better to read some good Java books. The application can’t give you even 1/10 of needed knowledge. If you have some Java experience, you probably won’t discover anything new.

My mark: ❤️❤️❤️💙💙💙💙💙💙💙

How to create an ATG pipeline processor?

I’m currently studying Oracle ATG. Kinda big monstrous eCommerce platform. Today I had to create a commerce pipeline processor. So, I started googling and found this great tutorial by Oracle: Creating processors. Well, that’s not bad. But it’s a bit poor. For example, I’d like to know, what is the Object pParam? What is the PipelineResult pResult? Actually, there’s the third (unanswered) question: why all parameters starts with “p”?

Ok, I’ve found some answers to my questions. Let’s see.

Read More →

Observer pattern

Observer is a software design pattern, that is used when some objects create events and other objects should be notified when these events occur.

The main idea is that we have two types of instances: one that produces events and one that consumes them. Events producer must have three methods:

  1. addObserver(Observer observer)
  2. removeObserver(Observer observer)
  3. notifyObservers(…)

The first two methods are used to add and delete observers from a collection. The third method must iterate that collection and call notify(…) method of the Observer instance.

Java has its own Observer interface and Observable class, but sometimes we don’t want to extend from a class, or we want to adjust the API, you can create your own interfaces, as I’ve done in the following example:

Read More →

Strategy pattern

With this post I’m starting series of articles dedicated to GoF design patterns. GoF stands for Gang of Four – four authors that wrote a famous book about design patterns.

So the first pattern I want to describe is Strategy. It allows to choose the behaviour of instances at runtime by defining appropriate strategy.

Let’s see an example. Supposing we have a class Order, which has getTotal() method, that returns total cost of Items in this order:

 

public int getTotal() {
	int total = 0;
		
	for (Item item : items) {
		total += item.getPrice();
	}
		
	return total;
}

 

Very simple, isn’t it? But what if we want to change the price depending on some circumstances? That’s where we can use the Strategy pattern. Click read more to see full example.

Read More →

Programming competition and a small Java task

Last week we had a programming competition at work which consisted of some small tasks. One of them was to check, if we can make a given string palindrome by adding one letter in any place. Shame on me, I didn’t manage with this task in time. However I got the third place in this competition. 🏆😁

So the task:
Your app should check, if it’s possible to make a palindrome by adding a lowercase English letter to a given string in any position. If it is, the application should return an index which points to where to put a character, otherwise the app should return -1.
The given string contains only lowercase Latin characters.

What is a palindrome? Palindrome is a word or a number, which reads the same backward and forward. For example:

  • Was it a car or a cat I saw?
  • 1234321
  • Step on no pets

See the solution.

Read More →