IT, Scala, Java, Web, Ubuntu

Category: Fundamental (Page 3 of 3)

Re: Java Challenge: Dropping Balloons in Java

About a few days ago, I read this post, Java Challenge: Dropping Balloons in Java having an interesting question.

It’s a question to find an algorithm that gives the highest floor where you can drop a water balloon without breaking it at a 100 story building. You have only two balloons. I had actually seen this problem before and solved it already. It was not balloons but eggs. The solution is simple. Since if you miss two chances then have no balloons available, you need to try from the lowest safe level which means throw it from the first floor then if it does not pop, pick it up and go up to the level 2 and repeat it until the balloon pops. When you get the level from which the balloon bursts after dropping it, one level below this level is the highest safe level. (e.g. If the balloon first breaks on the 20th floor, the highest safe floor is the 19th).

It’s easy isn’t it? Yet the post above has one more condition to make it a little bit more difficult which is that you can try only 15 times. This means if I try the solution I’ve just described, I can test up to only the 15th floor. If the breaking point is the 40th floor or even the 16th floor, I’ll never know it. When I first saw it, I tried to solve it for about five minutes yet couldn’t solve it. That time, I was too busy to think about it further thus couldn’t be bothered to do it. So I saved the URI of the post and forgot about it. Last night, no actually today morning, at approximately 6 am, I went to bed (yeah, am I a vampire or what? :)). Although I didn’t even try to think about the problem yet all of sudden I came up with the answer of that question. It was just like the ‘Eureka‘ story of Archimedes. Just like he figured out how to assess the purity of a golden crown when he stepped into a bath. OK, stop flattering myself πŸ™ I figured it out as the question was easy to solve.

Well, an important point here is that I in fact often find solutions to some difficult problems when I am not struggling to solve it yet am doing something irrelevant to the problem such as working out at the gym, washing dishes after a meal, lying on the bed to sleep or even when I’m dreaming. πŸ™‚ So if you can’t solve some problem then why don’t you try something else rather than thinking about the solution all the time. Your brain may need to relax or when you use the other parts of the brain by doing something else, it might figure it out.

Anyway, here is what my brain told me that time.

Since I have only two balloons, if I choose a wrong floor twice, I lose all the chances. Then what if I check every second level as I have two balloons. So first, if I drop it from the second level and it breaks then I still have one more balloon so that I can try it from the first level and I can find if the highest safe level is the first or there is no safe level. If the balloon is fine after dropping it from the second level then I can pick it up and try it from the fourth level and so on. However, with this solution I can check at most until the 30th floor which is better than checking up to the 15th one but still not enough to check the 100 stories. This means that it is not sufficient to check ever second level in order to find the floor and I should focus on not only the two balloons but also the 15 times of attempts. From the first solution above, I know that I can definitely have 15 times if I try from the lowest safe floor and move up. It means if the number of floors between the already known highest safe level and the breaking point level found is the same as or fewer than the number of attempts I still have, I can find the very first level where the balloon bursts after dropping it (In order to reduce the number of attempts, of course, I have to make it the same as the number of attempts still left). Therefore I can tell the highest safe level that is one level below the breaking point level found. This should be easier to understand with an example.

Let’s assume that the breaking point is the 17th floor.

At first, I have 15 tries to drop one balloon from the 15th floor.
The balloon’s still OK and I now have 14 times of attempts. Since the number of attempts I still have is 14 time, I can’t try it from the 30th floor which is 15 + 15 yet I have to try it from the 29th floor (15 + 14). Because the breaking point is the 17th floor, the balloon breaks on the 29th floor. Now I have 13 times of attempts and 1 balloon left. From the first try, I know the 14th floor is fine so I try from the next floor that is floor 15 to see if floor 14 is the highest safe floor. It turns out the 15th one is fine and now I have 12 times left. 16th -> OK / 11 times left, 17th -> it breaks! / 10 times left
So I can tell the 16th floor is the highest safe floor and I didn’t even use all the 15 times of attempts.

The post that has the question specifies the floor starts from 0 so let’s make the first floor is floor 0 and see have it works. I made a table which shows the number of attempts required to find the highest safe floor.

====================================================
 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15   tries
====================================================
 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14	- 1
15 16 17 18 19 20 21 22 23 24 25 26 27 28		- 2
29 30 31 32 33 34 35 36 37 38 39 40 41			- 3
42 43 44 45 46 47 48 49 50 51 52 53				- 4
54 55 56 57 58 59 60 61 62 63 64				- 5
65 66 67 68 69 70 71 72 73 74					- 6
75 76 77 78 79 80 81 82 83						- 7
84 85 86 87 88 89 90 91							- 8
92 93 94 95 96 97 98							- 9
99												- 10

So if the highest safe floor is 73. The first balloon breaks on the floor 74 after 6 times of attempts. After that try from the floor 65 (attempts: 7) through 66 (8), 67 (9), 68 (10), 69 (11), 70 (12), 71 (13), 72 (14) to 73 (15) and the second balloon hasn’t broken so I can tell the highest safe floor is the floor 73.

====================================================
 1  2  3  4  5  6  7  8[ 9]10 11 12 13 14 15
====================================================
65 66 67 68 69 70 71 72[73]74					-[6]

attempts: 6 times to get the floor 74 + 9 times to check until the floor 73 = 15 times.

Once I found the solution writing code is a piece of cake.
I roughly wrote the main part of the code like this.

private static final int MAX_ATTEMPTS = 15;

private int theFloor = (int) (Math.random() * 100);
private int numberOfBalloons = 2;
private int numberOfAttempts = 0;

// ...


public void findHighestSafeFloor()
{
	int previousFloorIndex = -1;
	int floorIndex = MAX_ATTEMPTS - 1;
	int highestSafeFloor = 0;

	while (MAX_ATTEMPTS > numberOfAttempts)
	{
		numberOfAttempts++;
		if (theFloor <= floorIndex)
		{
			numberOfBalloons--;
			highestSafeFloor = findHighestSafeFloor(previousFloorIndex + 1, floorIndex);
			break;
		}
		previousFloorIndex = floorIndex;
		floorIndex += (MAX_ATTEMPTS - numberOfAttempts);
		if (99 < floorIndex)
		{
			floorIndex = 99;
		}
	}
	
	/* print the result. */
}

private int findHighestSafeFloor(final int start, final int end)
{
	for (int i = start; i < end; i++)
	{
		numberOfAttempts++;
		if (theFloor == i)
		{
			numberOfBalloons--;
			return (i - 1);
		}
	}
	return (end - 1);
}

I am sure that the findHighestSafeFloor() method above works but I decided to make it recursion. Why? No special reasons. I just like recursion πŸ™‚ as I haven’t used it for approximately four years since I last programmed in a functional programming language like Haskell in my math class thus miss it. πŸ˜€

Although the code can be very simple as the following code with recursion, it has to increase the number of attempts and to decrease the number of balloons left, so it would be a bit more verbose as the second code.

private int findHighestSafeFloor(final int start, final int end)
{
	return (start < end ? (theFloor == start ? (start - 1) : findHighestSafeFloor((start + 1), end)) : (end - 1));
}
private int findHighestSafeFloor(final int start, final int end)
{
	if (start < end)
	{
		numberOfAttempts++;
		if (theFloor == start)
		{
			numberOfBalloons--;
			return (start - 1);
		}
		else
		{
			return findHighestSafeFloor((start + 1), end);
		}
	}
	else
	{
		return (end - 1);
	}
}

Now it’s time to code the complete programme and test it. Well, since it’s a very small programme, I didn’t do object-oriented programming. πŸ™‚

package com.lckymn.kevin.findingfloor;

public final class HighestSafeFloorFinder
{
	private static final int TOP_LEVEL = 99;
	private static final int MAX_ATTEMPTS = 15;

	private int theBreakingPointFloor;
	private int numberOfBalloons;
	private int numberOfAttemptsMade;

	public void init(final int theBreakingPointFloor)
	{
		this.theBreakingPointFloor = theBreakingPointFloor;
		System.out.println("The breaking point: " + theBreakingPointFloor);
		numberOfBalloons = 2;
		numberOfAttemptsMade = 0;
	}

	public void findHighestSafeFloor()
	{
		int previousFloorIndex = -1;
		int floorIndex = MAX_ATTEMPTS - 1;
		int highestSafeFloor = 0;

		while (MAX_ATTEMPTS > numberOfAttemptsMade)
		{
			dropBalloon();
			if (theBreakingPointFloor <= floorIndex)
			{
				breakBalloon();
				highestSafeFloor = findHighestSafeFloor(previousFloorIndex + 1, floorIndex);
				break;
			}
			previousFloorIndex = floorIndex;
			floorIndex += (MAX_ATTEMPTS - numberOfAttemptsMade);
			if (TOP_LEVEL < floorIndex)
			{
				floorIndex = TOP_LEVEL;
			}
		}

		if (0 > highestSafeFloor)
		{
			System.out.println("There is no floor that you can drop a balloon without breaking it. Even the first floor (floor 0) is not safe.");
		}
		else
		{
			System.out.println("The highest safe floor: " + highestSafeFloor);
		}

		System.out.println("The number of attempts made: " + numberOfAttemptsMade);
	}

	private int findHighestSafeFloor(final int start, final int end)
	{
		if (start < end)
		{
			dropBalloon();
			if (theBreakingPointFloor == start)
			{
				breakBalloon();
				return (start - 1);
			}
			else
			{
				return findHighestSafeFloor((start + 1), end);
			}
		}
		else
		{
			return (end - 1);
		}
	}

	private void dropBalloon()
	{
		if (MAX_ATTEMPTS == numberOfAttemptsMade)
		{
			throw new IllegalStateException("You've already tried 15 times.");
		}
		numberOfAttemptsMade++;
	}

	private void breakBalloon()
	{
		if (0 == numberOfBalloons)
		{
			throw new IllegalStateException("You've already broken all the balloons available.");
		}
		numberOfBalloons--;
	}

	public static void main(String[] args)
	{
		HighestSafeFloorFinder highestSafeFloorFinder = new HighestSafeFloorFinder();
		for (int i = 0; i < 100; i++)
		{
			System.out.println("[Test Number: " + i + "]");
			highestSafeFloorFinder.init(i);
			highestSafeFloorFinder.findHighestSafeFloor();
			System.out.println("=======================================");
		}
		System.exit(0);
	}
}

Well, the code looks a bit ugly but in my defence, I’d just like to see the result and am busy so had no stomach for making it pretty. πŸ˜€ (Oh! such a poor excuse / but really busy :()

The dropBalloon() and the breakBalloon() are added to increase the number of attempts made and to decrease the number of balloons left respectively. The dropBalloon() method also checks if the number of attempts already made is 15 and it tries to make another one than throws an exception telling that all the chances have been used. Similarly the breakBalloon() method checks if all the available balloons have broken if so, it also throws an exception saying there are no available balloons left. Thus I can see if the programme can find the highest safe floor with 15 times or fewer times of attempts and only two water balloons. If it cannot, it immediately throws an exception and stop the programme so I can see my programme fails to prove my algorithm.

The init() method is to initialise all the variables so that I can test it many times. It checks all the floors from the first one (floor 0) to the last one (floor 99) so if the programme works fine without any exception thrown then it might prove my answer is correct.

Let’s check out the result!

[Test Number: 0]
The breaking point: 0
There is no floor that you can drop a balloon without breaking it. Even the first floor (floor 0) is not safe.
The number of attempts made: 2
=======================================
[Test Number: 1]
The breaking point: 1
The highest safe floor: 0
The number of attempts made: 3
=======================================
[Test Number: 2]
The breaking point: 2
The highest safe floor: 1
The number of attempts made: 4
=======================================
[Test Number: 3]
The breaking point: 3
The highest safe floor: 2
The number of attempts made: 5
=======================================
[Test Number: 4]
The breaking point: 4
The highest safe floor: 3
The number of attempts made: 6
=======================================
[Test Number: 5]
The breaking point: 5
The highest safe floor: 4
The number of attempts made: 7
=======================================
[Test Number: 6]
The breaking point: 6
The highest safe floor: 5
The number of attempts made: 8
=======================================
[Test Number: 7]
The breaking point: 7
The highest safe floor: 6
The number of attempts made: 9
=======================================
[Test Number: 8]
The breaking point: 8
The highest safe floor: 7
The number of attempts made: 10
=======================================
[Test Number: 9]
The breaking point: 9
The highest safe floor: 8
The number of attempts made: 11
=======================================
[Test Number: 10]
The breaking point: 10
The highest safe floor: 9
The number of attempts made: 12
=======================================
[Test Number: 11]
The breaking point: 11
The highest safe floor: 10
The number of attempts made: 13
=======================================
[Test Number: 12]
The breaking point: 12
The highest safe floor: 11
The number of attempts made: 14
=======================================
[Test Number: 13]
The breaking point: 13
The highest safe floor: 12
The number of attempts made: 15
=======================================
[Test Number: 14]
The breaking point: 14
The highest safe floor: 13
The number of attempts made: 15
=======================================
[Test Number: 15]
The breaking point: 15
The highest safe floor: 14
The number of attempts made: 3
=======================================
[Test Number: 16]
The breaking point: 16
The highest safe floor: 15
The number of attempts made: 4
=======================================
[Test Number: 17]
The breaking point: 17
The highest safe floor: 16
The number of attempts made: 5
=======================================
[Test Number: 18]
The breaking point: 18
The highest safe floor: 17
The number of attempts made: 6
=======================================
[Test Number: 19]
The breaking point: 19
The highest safe floor: 18
The number of attempts made: 7
=======================================
[Test Number: 20]
The breaking point: 20
The highest safe floor: 19
The number of attempts made: 8
=======================================
[Test Number: 21]
The breaking point: 21
The highest safe floor: 20
The number of attempts made: 9
=======================================
[Test Number: 22]
The breaking point: 22
The highest safe floor: 21
The number of attempts made: 10
=======================================
[Test Number: 23]
The breaking point: 23
The highest safe floor: 22
The number of attempts made: 11
=======================================
[Test Number: 24]
The breaking point: 24
The highest safe floor: 23
The number of attempts made: 12
=======================================
[Test Number: 25]
The breaking point: 25
The highest safe floor: 24
The number of attempts made: 13
=======================================
[Test Number: 26]
The breaking point: 26
The highest safe floor: 25
The number of attempts made: 14
=======================================
[Test Number: 27]
The breaking point: 27
The highest safe floor: 26
The number of attempts made: 15
=======================================
[Test Number: 28]
The breaking point: 28
The highest safe floor: 27
The number of attempts made: 15
=======================================
[Test Number: 29]
The breaking point: 29
The highest safe floor: 28
The number of attempts made: 4
=======================================
[Test Number: 30]
The breaking point: 30
The highest safe floor: 29
The number of attempts made: 5
=======================================
[Test Number: 31]
The breaking point: 31
The highest safe floor: 30
The number of attempts made: 6
=======================================
[Test Number: 32]
The breaking point: 32
The highest safe floor: 31
The number of attempts made: 7
=======================================
[Test Number: 33]
The breaking point: 33
The highest safe floor: 32
The number of attempts made: 8
=======================================
[Test Number: 34]
The breaking point: 34
The highest safe floor: 33
The number of attempts made: 9
=======================================
[Test Number: 35]
The breaking point: 35
The highest safe floor: 34
The number of attempts made: 10
=======================================
[Test Number: 36]
The breaking point: 36
The highest safe floor: 35
The number of attempts made: 11
=======================================
[Test Number: 37]
The breaking point: 37
The highest safe floor: 36
The number of attempts made: 12
=======================================
[Test Number: 38]
The breaking point: 38
The highest safe floor: 37
The number of attempts made: 13
=======================================
[Test Number: 39]
The breaking point: 39
The highest safe floor: 38
The number of attempts made: 14
=======================================
[Test Number: 40]
The breaking point: 40
The highest safe floor: 39
The number of attempts made: 15
=======================================
[Test Number: 41]
The breaking point: 41
The highest safe floor: 40
The number of attempts made: 15
=======================================
[Test Number: 42]
The breaking point: 42
The highest safe floor: 41
The number of attempts made: 5
=======================================
[Test Number: 43]
The breaking point: 43
The highest safe floor: 42
The number of attempts made: 6
=======================================
[Test Number: 44]
The breaking point: 44
The highest safe floor: 43
The number of attempts made: 7
=======================================
[Test Number: 45]
The breaking point: 45
The highest safe floor: 44
The number of attempts made: 8
=======================================
[Test Number: 46]
The breaking point: 46
The highest safe floor: 45
The number of attempts made: 9
=======================================
[Test Number: 47]
The breaking point: 47
The highest safe floor: 46
The number of attempts made: 10
=======================================
[Test Number: 48]
The breaking point: 48
The highest safe floor: 47
The number of attempts made: 11
=======================================
[Test Number: 49]
The breaking point: 49
The highest safe floor: 48
The number of attempts made: 12
=======================================
[Test Number: 50]
The breaking point: 50
The highest safe floor: 49
The number of attempts made: 13
=======================================
[Test Number: 51]
The breaking point: 51
The highest safe floor: 50
The number of attempts made: 14
=======================================
[Test Number: 52]
The breaking point: 52
The highest safe floor: 51
The number of attempts made: 15
=======================================
[Test Number: 53]
The breaking point: 53
The highest safe floor: 52
The number of attempts made: 15
=======================================
[Test Number: 54]
The breaking point: 54
The highest safe floor: 53
The number of attempts made: 6
=======================================
[Test Number: 55]
The breaking point: 55
The highest safe floor: 54
The number of attempts made: 7
=======================================
[Test Number: 56]
The breaking point: 56
The highest safe floor: 55
The number of attempts made: 8
=======================================
[Test Number: 57]
The breaking point: 57
The highest safe floor: 56
The number of attempts made: 9
=======================================
[Test Number: 58]
The breaking point: 58
The highest safe floor: 57
The number of attempts made: 10
=======================================
[Test Number: 59]
The breaking point: 59
The highest safe floor: 58
The number of attempts made: 11
=======================================
[Test Number: 60]
The breaking point: 60
The highest safe floor: 59
The number of attempts made: 12
=======================================
[Test Number: 61]
The breaking point: 61
The highest safe floor: 60
The number of attempts made: 13
=======================================
[Test Number: 62]
The breaking point: 62
The highest safe floor: 61
The number of attempts made: 14
=======================================
[Test Number: 63]
The breaking point: 63
The highest safe floor: 62
The number of attempts made: 15
=======================================
[Test Number: 64]
The breaking point: 64
The highest safe floor: 63
The number of attempts made: 15
=======================================
[Test Number: 65]
The breaking point: 65
The highest safe floor: 64
The number of attempts made: 7
=======================================
[Test Number: 66]
The breaking point: 66
The highest safe floor: 65
The number of attempts made: 8
=======================================
[Test Number: 67]
The breaking point: 67
The highest safe floor: 66
The number of attempts made: 9
=======================================
[Test Number: 68]
The breaking point: 68
The highest safe floor: 67
The number of attempts made: 10
=======================================
[Test Number: 69]
The breaking point: 69
The highest safe floor: 68
The number of attempts made: 11
=======================================
[Test Number: 70]
The breaking point: 70
The highest safe floor: 69
The number of attempts made: 12
=======================================
[Test Number: 71]
The breaking point: 71
The highest safe floor: 70
The number of attempts made: 13
=======================================
[Test Number: 72]
The breaking point: 72
The highest safe floor: 71
The number of attempts made: 14
=======================================
[Test Number: 73]
The breaking point: 73
The highest safe floor: 72
The number of attempts made: 15
=======================================
[Test Number: 74]
The breaking point: 74
The highest safe floor: 73
The number of attempts made: 15
=======================================
[Test Number: 75]
The breaking point: 75
The highest safe floor: 74
The number of attempts made: 8
=======================================
[Test Number: 76]
The breaking point: 76
The highest safe floor: 75
The number of attempts made: 9
=======================================
[Test Number: 77]
The breaking point: 77
The highest safe floor: 76
The number of attempts made: 10
=======================================
[Test Number: 78]
The breaking point: 78
The highest safe floor: 77
The number of attempts made: 11
=======================================
[Test Number: 79]
The breaking point: 79
The highest safe floor: 78
The number of attempts made: 12
=======================================
[Test Number: 80]
The breaking point: 80
The highest safe floor: 79
The number of attempts made: 13
=======================================
[Test Number: 81]
The breaking point: 81
The highest safe floor: 80
The number of attempts made: 14
=======================================
[Test Number: 82]
The breaking point: 82
The highest safe floor: 81
The number of attempts made: 15
=======================================
[Test Number: 83]
The breaking point: 83
The highest safe floor: 82
The number of attempts made: 15
=======================================
[Test Number: 84]
The breaking point: 84
The highest safe floor: 83
The number of attempts made: 9
=======================================
[Test Number: 85]
The breaking point: 85
The highest safe floor: 84
The number of attempts made: 10
=======================================
[Test Number: 86]
The breaking point: 86
The highest safe floor: 85
The number of attempts made: 11
=======================================
[Test Number: 87]
The breaking point: 87
The highest safe floor: 86
The number of attempts made: 12
=======================================
[Test Number: 88]
The breaking point: 88
The highest safe floor: 87
The number of attempts made: 13
=======================================
[Test Number: 89]
The breaking point: 89
The highest safe floor: 88
The number of attempts made: 14
=======================================
[Test Number: 90]
The breaking point: 90
The highest safe floor: 89
The number of attempts made: 15
=======================================
[Test Number: 91]
The breaking point: 91
The highest safe floor: 90
The number of attempts made: 15
=======================================
[Test Number: 92]
The breaking point: 92
The highest safe floor: 91
The number of attempts made: 10
=======================================
[Test Number: 93]
The breaking point: 93
The highest safe floor: 92
The number of attempts made: 11
=======================================
[Test Number: 94]
The breaking point: 94
The highest safe floor: 93
The number of attempts made: 12
=======================================
[Test Number: 95]
The breaking point: 95
The highest safe floor: 94
The number of attempts made: 13
=======================================
[Test Number: 96]
The breaking point: 96
The highest safe floor: 95
The number of attempts made: 14
=======================================
[Test Number: 97]
The breaking point: 97
The highest safe floor: 96
The number of attempts made: 15
=======================================
[Test Number: 98]
The breaking point: 98
The highest safe floor: 97
The number of attempts made: 15
=======================================
[Test Number: 99]
The breaking point: 99
The highest safe floor: 98
The number of attempts made: 10
=======================================

It works! πŸ˜€

Test for Empty String

If you programme in Java (or other languages), the first thing to do when validating a String value might be checking if the String variable has the reference pointing to any String object or not (in other words, if it is null or not) or if the String object to which the reference in the String variable points contains an empty String (“”) value. In both cases, there is nothing to validate. Or you might purposely expect it and if so, want to do something else. One way or another, the test happens frequently. In Java, like other languages, there are, of course, many ways to do it. However, there can be a better way than other ways in terms of performance. The reason why I’m writing it is that we seem to often forget that kind of basic principles while we are focusing on higher level design issues. It is maintainability that I think the most important in software development but the better way to test for an empty String that I am about to point out here does not reduce maintainability yet increases performance.

To be honest, I had not thought of any other ways until the second grade when I was a university student. At that time, I coded like the following whenever I tried to test for an empty String.

if (text == null || text.equals(""))

(By the way, I now programme in this way when I check if an Object type variable is null.

if (null == object)

This is to avoid some problem caused by a possible typo mistake that is (object = null) which means assigning null to the variable so it refers to nowhere. Yet if I use (null == object), that mistake would be (null = object). This is assigning whatever in the object variable to null which does not make any sense and will cause a compile time error thus I can easily notice it. So I follow this way even though I did not make this kind of mistake before. I will also never do it as long as I keep that style. :D)

Move back to the original topic, when I became a third year student which was the final year of my course, I was attracted to Object-Orientation and Java. Probably at the same time, I also enjoyed checking out the source code of JDK. I thought “it doesn’t seem to be a good idea to use the equals method for empty String test” after I read the source of the method in Java String class.

Here is the source code of the equals() method of String class in JDK 1.6.0_13.

    /**
     * Compares this string to the specified object.  The result is {@code
     * true} if and only if the argument is not {@code null} and is a {@code
     * String} object that represents the same sequence of characters as this
     * object.
     *
     * @param  anObject
     *         The object to compare this {@code String} against
     *
     * @return  {@code true} if the given object represents a {@code String}
     *          equivalent to this string, {@code false} otherwise
     *
     * @see  #compareTo(String)
     * @see  #equalsIgnoreCase(String)
     */
    public boolean equals(Object anObject) {
	if (this == anObject) {
	    return true;
	}
	if (anObject instanceof String) {
	    String anotherString = (String)anObject;
	    int n = count;
	    if (n == anotherString.count) {
		char v1[] = value;
		char v2[] = anotherString.value;
		int i = offset;
		int j = anotherString.offset;
		while (n-- != 0) {
		    if (v1[i++] != v2[j++])
			return false;
		}
		return true;
	    }
	}
	return false;
    }

For just simple empty String test, the code does not seem to be right, as you see, it requires several lines of code to be processed for that simple test. Since String is immutable its length value is always a constant int value and it is therefore comparing the length of the String with zero should be faster than using the equals() method.

So in order to see how many lines of code in the equals() method are processed to test for an empty String, I made simple test code. I copied code partially from Java’s String class and made TestString class which has the same equals() method as what String class has then added some code to display how many lines are processed.

package com.lckymn.test.emptystring;

import java.util.Arrays;

/**
 * This is TestString class the code of which is copied from java.lang.String class to test the equals() method of the String class. It just
 * has the part of String code which is enough to see how many lines of code are processed for empty String test.
 * 
 * @author Lee, SeongHyun (Kevin)
 * @version 0.01 (2009-05-05)
 */
public final class TestString
{
	/** The value is used for character storage. */
	private final char value[];

	/** The offset is the first index of the storage that is used. */
	private final int offset;

	/** The count is the number of characters in the String. */
	private final int count;

	public TestString()
	{
		this.offset = 0;
		this.count = 0;
		this.value = new char[0];
	}

	public TestString(char value[])
	{
		int size = value.length;
		this.offset = 0;
		this.count = size;
		this.value = Arrays.copyOf(value, size);
	}

	public TestString(int offset, int count, char value[])
	{
		this.value = value;
		this.offset = offset;
		this.count = count;
	}

	public boolean equals(Object anObject)
	{
		int tempCount = 0;
		System.out.println(++tempCount + ": A");
		if (this == anObject)
		{
			System.out.println(++tempCount + ": B");
			return true;
		}
		System.out.println(++tempCount + ": C");
		if (anObject instanceof TestString)
		{
			System.out.println(++tempCount + ": D");
			TestString anotherString = (TestString) anObject;
			System.out.println(++tempCount + ": E");
			int n = count;
			System.out.println(++tempCount + ": F");
			if (n == anotherString.count)
			{
				System.out.println(++tempCount + ": G");
				char v1[] = value;
				System.out.println(++tempCount + ": H");
				char v2[] = anotherString.value;
				System.out.println(++tempCount + ": I");
				int i = offset;
				System.out.println(++tempCount + ": J");
				int j = anotherString.offset;
				System.out.println(++tempCount + ": K");
				while (n-- != 0)
				{
					System.out.println(++tempCount + ": L");
					if (v1[i++] != v2[j++])
					{
						System.out.println(++tempCount + ": M");
						return false;
					}
				}
				System.out.println(++tempCount + ": N");
				return true;
			}
		}
		System.out.println(++tempCount + ": O");
		return false;
	}

}

The following is the test code using the TestString.

package com.lckymn.test.emptystring;

/**
 * This class is to see how many lines of code in the equals() method of String class are processed to test for an empty String.
 * 
 * @author Lee, SeongHyun (Kevin)
 * @version 0.01 (2009-05-05)
 * 
 */
public final class EmptyTest
{
	private static final String EMPTY = "It's empty.\n";
	private static final String NOT_EMPTY = "It's not empty.\n";

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{

		TestString emptyOne = new TestString();
		TestString anotherEmptyOne = new TestString();
		TestString testString = new TestString(new char[] { 'H', 'e', 'l', 'l', 'o' });

		System.out.println("TestString emptyOne = new TestString();\n" + "TestString anotherEmptyOne = new TestString();\n"
				+ "TestString testString = new TestString(new char[] { 'H', 'e', 'l', 'l', 'o' });\n");

		System.out.println("emptyOne.equals(emptyOne): ");
		if (emptyOne.equals(emptyOne))
		{
			System.out.println(EMPTY);
		}
		else
		{
			System.out.println(NOT_EMPTY);
		}

		System.out.println("emptyOne.equals(anotherEmptyOne): ");
		if (emptyOne.equals(anotherEmptyOne))
		{
			System.out.println(EMPTY);
		}
		else
		{
			System.out.println(NOT_EMPTY);
		}

		System.out.println("emptyOne.equals(testString)");
		if (emptyOne.equals(testString))
		{
			System.out.println(EMPTY);
		}
		else
		{
			System.out.println(NOT_EMPTY);
		}

		System.exit(0);
	}

}

The result of this code is

TestString emptyOne = new TestString();
TestString anotherEmptyOne = new TestString();
TestString testString = new TestString(new char[] { 'H', 'e', 'l', 'l', 'o' });

emptyOne.equals(emptyOne): 
1: A
2: B
It's empty.

emptyOne.equals(anotherEmptyOne): 
1: A
2: C
3: D
4: E
5: F
6: G
7: H
8: I
9: J
10: K
11: N
It's empty.

emptyOne.equals(testString)
1: A
2: C
3: D
4: E
5: F
6: O
It's not empty.

The first test is comparing two empty TestString objects which are in fact an identical object. To recognise it is an empty String, two lines of the code are executed.

emptyOne.equals(emptyOne): 
1: A
2: B
It's empty.

The second test is comparing two empty TestString objects each of which has a reference to a different TestString object meaning emptyOne.equals(anotherEmptyOne) is true but emptyOne == anotherEmptyOne is false. It requires to process eleven lines of the code to know anotherEmptyOne object is an empty TestString object.

emptyOne.equals(anotherEmptyOne): 
1: A
2: C
3: D
4: E
5: F
6: G
7: H
8: I
9: J
10: K
11: N
It's empty.

The final one is testing if testString TestString object is an empty TestString object. Since testString has “Hello”, it is not empty. To find it, six lines of the code are implemented.

emptyOne.equals(testString)
1: A
2: C
3: D
4: E
5: F
6: O
It's not empty.

Based on the test above, it is quite obvious that equals() method cannot perform better than comparing zero(0) with the length of String object as the former one requires two to eleven (average: (2+11+6)/3 = 10) lines of code to test for an empty String whereas a length comparison requires only one or two lines of code. So I finally tested if it is truly faster to check String’s length to test for an empty String.

The following is the test code.

package com.lckymn.test.emptystring;

/**
 * This class is to benchmark the performance of the equals() method in String class and comparing the length of the String object in order
 * to test if the String object contains an empty String.
 * 
 * @author Lee, SeongHyun (Kevin)
 * @version 0.01 (2009-05-05)
 */
public final class EmptyPerformanceTest
{
	public static final int HOW_MANY_TIMES = 1000000000;

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		String emptyOne = "";
		String anotherEmptyOne = new String("");
		String notEmptyOne = "Hello";

		System.out.println("String emptyOne = \"\";\n" + "String anotherEmptyOne = new String(\"\");\n"
				+ "String notEmptyOne = \"Hello\";\n");

		long result = 0;

		result = System.currentTimeMillis();
		for (int i = 0; i < HOW_MANY_TIMES; i++)
		{
			if ("".equals(emptyOne))
				;
		}
		result = System.currentTimeMillis() - result;
		System.out.println("\"\".equals(emptyOne): " + result);

		result = System.currentTimeMillis();
		for (int i = 0; i < HOW_MANY_TIMES; i++)
		{
			if ("".equals(anotherEmptyOne))
				;
		}
		result = System.currentTimeMillis() - result;
		System.out.println("\"\".equals(anotherEmptyOne): " + result);

		result = System.currentTimeMillis();
		for (int i = 0; i < HOW_MANY_TIMES; i++)
		{
			if ("".equals(notEmptyOne))
				;
		}
		result = System.currentTimeMillis() - result;
		System.out.println("\"\".equals(notEmptyOne): " + result);

		System.out.println("");

		result = System.currentTimeMillis();
		for (int i = 0; i < HOW_MANY_TIMES; i++)
		{
			if (0 == emptyOne.length())
				;
		}
		result = System.currentTimeMillis() - result;
		System.out.println("0 == emptyOne.length(): " + result);

		result = System.currentTimeMillis();
		for (int i = 0; i < HOW_MANY_TIMES; i++)
		{
			if (0 == anotherEmptyOne.length())
				;
		}
		result = System.currentTimeMillis() - result;
		System.out.println("0 == anotherEmptyOne.length(): " + result);

		result = System.currentTimeMillis();
		for (int i = 0; i < HOW_MANY_TIMES; i++)
		{
			if (0 == notEmptyOne.length())
				;
		}
		result = System.currentTimeMillis() - result;
		System.out.println("0 == notEmptyOne.length(): " + result);

		System.exit(0);
	}
}

And… seeing is believing… so here is the result.

String emptyOne = "";
String anotherEmptyOne = new String("");
String notEmptyOne = "Hello";

"".equals(emptyOne): 1470
"".equals(anotherEmptyOne): 6951
"".equals(notEmptyOne): 5601

0 == emptyOne.length(): 5
0 == anotherEmptyOne.length(): 5
0 == notEmptyOne.length(): 5

Using equals() method to test both empty String objects which are also an identical object took 1470 milliseconds while comparing 0 with the length of the emptyOne object took 5 milliseconds.
If the target object is not the same empty object, it is worse. The test shows it took 6951 milliseconds. Again, the length comparison took just 5 milliseconds. Considering that the test conducted one billion times, taking just 5 milliseconds is really fast.
Finally, as seen in the test, it took 5601 milliseconds to know the given String is not empty. The length checking is still 5 milliseconds.

Although each test is repeated 1,000,000,000 times, the difference is, in my opinion, not really acceptable as we may have numerous other places to lose the performance of the software thus it might be a good idea to prevent predictable ones from happening.

So better do this when testing for an empty String. πŸ™‚

if (null == text || 0 == text.length())
{
	// it is empty.
}

Or if you are using JDK 1.6 (Java 6), you can also use String’s isEmpty() method which does exactly the same as checking if its length is 0.

if (null == text || text.isEmpty())
{
	// it is empty.
}
Newer posts »

© 2026 Kevin's Blog

Theme by Anders Noren — Up ↑