Kevin's Blog

IT, Scala, Java, Web, Ubuntu

Tag: Code

Quizzes for Primary Schoolchildren

Yesterday (the 5th of May) was the children’s day, which is an official holiday, in South Korea, (by the way, the day before yesterday was Star Wars Day :)) and I found that Xeraph, who is the CEO of the IT security company named Nchovy, as well as the creator & core programmer of the OSGi based opensource security platform, Kraken, posted some quiz for kids. :) His blog also has other quizzes for the primary schoolchildren.

These quizzes remind me of my childhood as these kinds of quizzes are what I used to solve when I was a primary schoolchild. It was approximately 22 years ago. That time, I learned Basic (Not Visual Basic (VB), VB didn’t exist back then), Fortran, COBOL and C. When I was learning Basic, those kinds of problems were what I solved. After I graduated from the primary school, I had not touched any computer programming languages at all for 13 years. I started it again as I came to Sydney and entered the university to study IT. Then I realised that I could remember nothing about what I learned before. Nothing at all. That’s why my ‘about me‘ page doesn’t contain any of Basic, Fortran and COBOL since I’ve completely forgot about these (it does have Visual Basic though as it’s my first programming language that I learned at the diploma school, UTS:INSEARCH). So my point is, doesn’t matter whether it’s a programming language or a human language, if you don’t practice or keep studying constantly, you will lose your skills and fluency of the language.

Anyway, I saw those quizzes and solved it for fun yet feel like I did better when I was a kid. :( Back then, I didn’t even use a computer to solve it but only pen and paper.
The followings are the quizzes and my answers, yet there can be much better answers of course, so why don’t you try by yourself before you check out mine.

This is the prompt utility class, used for the other programmes, to get input values. Notice that it doesn’t check whether the input value is number, integer more precisely, or not although it does check if the given integer is a positive one.

package com.lckymn.kevin.simplequiz;

import java.util.Scanner;

/**
 * @author Lee, SeongHyun (Kevin)
 * @version 0.0.1 (2010-05-05)
 */
public final class PromptUtil
{
    private PromptUtil()
    {
        throw new IllegalStateException(getClass().getName() + " cannot be instantiated.");
    }

    public static int askForPositiveInt(String message)
    {
        final Scanner scanner = new Scanner(System.in);
        System.out.print(message);
        int number = Integer.parseInt(scanner.nextLine());

        while (0 >= number)
        {
            System.out.println("Please enter a position integer.");
            System.out.print(message);
            number = Integer.parseInt(scanner.nextLine());
        }
        return number;
    }
}

 

Quiz 1. (taken from http://xeraph.com/5251248)

n for diamond? 3
  *
 ***
*****
 ***
  *

n for diamond? 5
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

n for diamond? 7
      *
     ***
    *****
   *******
  *********
 ***********
*************
 ***********
  *********
   *******
    *****
     ***
      *

n for diamond? 9
        *
       ***
      *****
     *******
    *********
   ***********
  *************
 ***************
*****************
 ***************
  *************
   ***********
    *********
     *******
      *****
       ***
        *

* Kevin’s Answer1

Show »

package com.lckymn.kevin.simplequiz;

import static com.lckymn.kevin.simplequiz.PromptUtil.*;

/**
 * @author Lee, SeongHyun (Kevin)
 * @version 0.0.1 (2010-05-05)
 */
public final class Diamond
{
    public static void main(String[] args)
    {
        int n = askForPositiveInt("n for diamond? ");
        int startPoint = --n;
        int endPoint = startPoint;
        for (int outer = 0, outerSize = n << 1; outer <= outerSize; outer++)
        {
            for (int inner = 0; inner <= endPoint; inner++)
            {
                System.out.print(inner >= startPoint ? "*" : " ");
            }
            if (outer < n)
            {
                startPoint--;
                endPoint++;
            }
            else
            {
                startPoint++;
                endPoint--;
            }
            System.out.print("\n");
        }
        System.exit(0);
    }

}

* Kevin’s Answer2

Show »

package com.lckymn.kevin.simplequiz;

import static com.lckymn.kevin.simplequiz.PromptUtil.*;

/**
 * @author Lee, SeongHyun (Kevin)
 * @version 0.0.1 (2010-05-05)
 */
public final class Diamond2
{
    public static void main(String[] args)
    {
        int n = askForPositiveInt("n for diamond? ");
        int startPoint = --n;
        int endPoint = startPoint;
        for (int outer = 0; outer < n; outer++)
        {
            for (int inner = 0; inner <= endPoint; inner++)
            {
                System.out.print(inner >= startPoint ? "*" : " ");
            }
            startPoint--;
            endPoint++;
            System.out.print("\n");
        }
        for (int outer = 0; outer <= n; outer++)
        {
            for (int inner = 0; inner <= endPoint; inner++)
            {
                System.out.print(inner >= startPoint ? "*" : " ");
            }
            startPoint++;
            endPoint--;
            System.out.print("\n");
        }
        System.exit(0);
    }

}

 

 

Quiz 2. (taken from http://xeraph.com/5251248)

n for outer box? 4
m for inner box? 2
****
*  *
*  *
****

n for outer box? 7
m for inner box? 3
*******
*******
**   **
**   **
**   **
*******
*******

n for outer box? 10
m for inner box? 4
**********
**********
**********
***    ***
***    ***
***    ***
***    ***
**********
**********
**********

n for outer box? 12
m for inner box? 6
************
************
************
***      ***
***      ***
***      ***
***      ***
***      ***
***      ***
************
************
************

* Kevin’s Answer

Show »

package com.lckymn.kevin.simplequiz;

import static com.lckymn.kevin.simplequiz.PromptUtil.*;

/**
 * @author Lee, SeongHyun (Kevin)
 * @version 0.0.1 (2010-05-05)
 */
public final class Box
{
    public static void main(String[] args)
    {
        int n = askForPositiveInt("n for outer box? ");
        int m = askForPositiveInt("m for inner box? ");

        while (n <= m || (0 != ((n - m) & 1)))
        {
            System.out.println("n must be greater than m and the result of (n - m) must be an even number.");
            n = askForPositiveInt("n for outer box? ");
            m = askForPositiveInt("m for inner box? ");
        }

        int halfOfDifference = n - m >>> 1;
        int startPoint = halfOfDifference - 1;
        int endPoint = n - halfOfDifference;
        for (int outer = 0; outer < n; outer++)
        {
            for (int inner = 0; inner < n; inner++)
            {
                System.out.print((outer > startPoint && outer < endPoint && inner > startPoint && inner < endPoint) ? " " : "*");
            }
            System.out.print("\n");
        }
        System.exit(0);
    }
}

 

 

Quiz 3. (taken from http://xeraph.com/5251248)

n for outer triangle? 3
m for inner triangle? 1
  *
 ***
** **

n for outer triangle? 5
m for inner triangle? 3
    *
   ***
  ** **
 **   **
**     **

n for outer triangle? 8
m for inner triangle? 4
       *
      ***
     *****
    *******
   **** ****
  ****   ****
 ****     ****
****       ****

n for outer triangle? 10
m for inner triangle? 5
         *
        ***
       *****
      *******
     *********
    ***** *****
   *****   *****
  *****     *****
 *****       *****
*****         *****

* Kevin’s Answer

Show »

package com.lckymn.kevin.simplequiz;

import static com.lckymn.kevin.simplequiz.PromptUtil.*;

/**
 * @author Lee, SeongHyun (Kevin)
 * @version 0.0.1 (2010-05-05)
 */
public final class Triangle
{
    public static void main(String[] args)
    {
        int n = askForPositiveInt("n for outer triangle? ");
        int m = askForPositiveInt("m for inner triangle? ");

        while (n <= m)
        {
            System.out.println("n must be greater than m.");
            n = askForPositiveInt("n for outer triangle? ");
            m = askForPositiveInt("m for inner triangle? ");
        }

        int startPoint = (n - 1);
        int endPoint = startPoint;

        final int innerHeightStartPoint = startPoint - (m - 1);
        int innerWidthStartPoint = startPoint;
        int innerWidthEndPoint = startPoint;
        for (int outer = 0; outer < n; outer++)
        {
            for (int inner = 0; inner <= endPoint; inner++)
            {
                System.out.print((outer >= innerHeightStartPoint && inner >= innerWidthStartPoint && inner <= innerWidthEndPoint) ? " "
                        : inner >= startPoint ? "*" : " ");
            }
            startPoint--;
            endPoint++;
            if (outer >= innerHeightStartPoint)
            {
                innerWidthStartPoint--;
                innerWidthEndPoint++;
            }
            System.out.print("\n");
        }
        System.exit(0);
    }
}

 

 

Quiz 4. (taken from http://xeraph.com/5261697)

matrix size n? 5
  1   2   3   4   5
 16  17  18  19   6
 15  24  25  20   7
 14  23  22  21   8
 13  12  11  10   9

matrix size n? 6
  1   2   3   4   5   6
 20  21  22  23  24   7
 19  32  33  34  25   8
 18  31  36  35  26   9
 17  30  29  28  27  10
 16  15  14  13  12  11

matrix size n? 7
  1   2   3   4   5   6   7
 24  25  26  27  28  29   8
 23  40  41  42  43  30   9
 22  39  48  49  44  31  10
 21  38  47  46  45  32  11
 20  37  36  35  34  33  12
 19  18  17  16  15  14  13

matrix size n? 8
  1   2   3   4   5   6   7   8
 28  29  30  31  32  33  34   9
 27  48  49  50  51  52  35  10
 26  47  60  61  62  53  36  11
 25  46  59  64  63  54  37  12
 24  45  58  57  56  55  38  13
 23  44  43  42  41  40  39  14
 22  21  20  19  18  17  16  15

* Kevin’s Answer1

Show »

package com.lckymn.kevin.simplequiz;

import static com.lckymn.kevin.simplequiz.PromptUtil.*;

/**
 * @author Lee, SeongHyun (Kevin)
 * @version 0.0.1 (2010-05-05)
 */
public final class Matrix
{
    public static void main(String[] args)
    {
        final int size = askForPositiveInt("matrix size n? ");
        final int sizeSquare = size * size;
        final int[][] matrix = new int[size][size];
        int number = 0;
        int row = 0;
        int column = 0;
        int addColumn = 1;
        int addRow = 0;
        int max = size;
        boolean isColumn = true;
        while (number < sizeSquare)
        {
            for (int filled = 0; filled < max; filled++)
            {
                matrix[row][column] = ++number;
                row += addRow;
                column += addColumn;
            }
            row += (isColumn ? (addColumn > 0 ? (addRow = 1) : (addRow = -1)) : -addRow);
            column += (isColumn ? -addColumn : (addRow > 0 ? (addColumn = -1) : (addColumn = 1)));
            if (isColumn)
            {
                max--;
                addColumn = 0;
            }
            else
            {
                addRow = 0;
            }
            isColumn = !isColumn;
        }
        final String format = "%" + (String.valueOf(sizeSquare).length()) + "d ";
        for (row = 0; row < size; row++)
        {
            for (column = 0; column < size; column++)
            {
                System.out.printf(format, Integer.valueOf(matrix[row][column]));
            }
            System.out.println();
        }
        System.exit(0);
    }

}

* Kevin’s Answer2

Show »

package com.lckymn.kevin.simplequiz;

import static com.lckymn.kevin.simplequiz.PromptUtil.*;

/**
 * @author Lee, SeongHyun (Kevin)
 * @version 0.0.1 (2010-05-05)
 */
public final class Matrix2
{
    enum Direction
    {
        UP, LEFT, DOWN, RIGHT;

        private static Direction current = UP;
        static
        {
            UP.next = RIGHT;
            DOWN.next = LEFT;
            LEFT.next = UP;
            RIGHT.next = DOWN;
        }
        private Direction next;

        static Direction next()
        {
            current = current.next;
            return current;
        }
    }

    public static void main(String[] args)
    {
        final int size = askForPositiveInt("matrix size n? ");
        int[][] matrix = new int[size][size];
        final int sizeSquare = size * size;
        int number = 0;
        int row = 0;
        int column = 0;
        int max = size;
        int filled = 0;
        int addColumn = 1;
        int addRow = 0;

        while (number < sizeSquare)
        {
            switch (Direction.next())
            {
                case RIGHT:
                case LEFT:
                    while (filled < max)
                    {
                        matrix[row][column] = ++number;
                        column += addColumn;
                        filled++;
                    }
                    row += addColumn;
                    column += -addColumn;
                    addRow = (addColumn > 0 ? 1 : -1);
                    addColumn = 0;
                    max--;
                    break;
                case DOWN:
                case UP:
                    while (filled < max)
                    {
                        matrix[row][column] = ++number;
                        row += addRow;
                        filled++;
                    }
                    row += -addRow;
                    column += -addRow;
                    addColumn = (addRow > 0 ? -1 : 1);
                    addRow = 0;
                    break;
                default:
                    throw new IllegalStateException("No one can get here!");
            }
            filled = 0;
        }
        final String format = "%" + (String.valueOf(sizeSquare).length()) + "d ";
        for (row = 0; row < size; row++)
        {
            for (column = 0; column < size; column++)
            {
                System.out.printf(format, Integer.valueOf(matrix[row][column]));
            }
            System.out.println();
        }
        System.exit(0);
    }
}

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.
}

© 2018 Kevin's Blog

Theme by Anders NorenUp ↑

%d bloggers like this: