Alessandro Lacava

on Designing and Developing Software. In love with Functional Programming.

Java Split() of String | Multiple Whitespace Characters

The split method of the String class is very useful when you want to tokenize a string. Its power lies in the fact that it accepts a string, as a parameter, which can be a regular expression. However you must be careful when you want to split a string using the whitespace character as a delimiter. Consider the following snippet of code:

1
2
3
4
5
String str = "Testing split using two  whitespace characters";
String[] tokens = str.split("\\s");
for(String token : tokens) {
  System.out.println("-" + token + "-");
}

What’s the output produced by the previous code? If you think it is the following one you’re wrong:

  • -Testing-
  • -split-
  • -using-
  • -two-
  • -whitespace-
  • -characters-

The actual output is instead this one:

  • -Testing-
  • -split-
  • -using-
  • -two-
  • -whitespace-
  • -characters-

Where in the hell did that empty string come out from? It comes out from the two whitespace characters that are between the word two and whitespace of the str string. If this is what you want OK. However, most of the time, you will want to discard that empty string from your resulting string array. You can obtain this result by using the \s+ regex in place of \s. Basically, the previuos code becomes:

1
2
3
4
5
String str = "Testing split using two  whitespace characters";
String[] tokens = str.split("\\s+");
for(String token : tokens) {
  System.out.println("-" + token + "-");
}

OutOfMemoryError In Eclipse | Java Virtual Machine (JVM)

OutOfMemoryError in Eclipse | Java Virtual Machine (JVM)

It might happen that while running a Java application within the Eclipse environment you get an OutOfMemoryError due to the maximum amount of memory dedicated to the heap. You can fix it by increasing the minimum (-Xms parameter) and maximum (-Xmx parameter) heap size. You can do it in two different ways:

  1. By editing your eclipse.ini file you find under your Eclipse installation directory. Within that file you should find two lines similar to the following ones:

-Xms40m -Xmx512m

You might want to change them depending on your hardware. For example I changed them as follows:

-Xms256m -Xmx1024m

It means: “start with an initial heap size of 256 MB and grow to a maximum of 1024MB, that is 1GB.”

  1. By editing the Run Configurations in Eclipse. To do this, right-click on the class you want to run (the one with the main method) and then select:

Run As -> Run Configurations

Click on the Arguments tab. In the VM Arguments box type:

-Xms256m -Xmx1024m

or whatever you prefer, depending on the hardware you possess.

IMPORTANT NOTE: It is strongly recommended that you make a backup copy of your eclipse.ini file before editing it in any way.

Performance Differences Between IN and EXISTS

Very often I hear people wondering if it’s better the EXISTS or the IN operator. Let’s start by saying that the two operators behave differently when NULL values are involved (see this post).

If you are sure that the field used to join the tables does not include NULL values then the IN operator “generally” performs better than EXISTS. I say “generally” because this is not always the case. For example consider the following query:

1
2
3
SELECT *
FROM table_a a
WHERE a.cd_field NOT IN (SELECT b.cd_field FROM table_b b);

The previous query is processed more or less as follows:

1
2
3
SELECT *
FROM table_a a, (SELECT DISTINCT cd_field FROM table_b) b
WHERE a.cd_field != b.cd_field;

The subquery is evaluated and the duplicates removed from the result. Then it is joined to the outer table. Now lets analyze the same query using NOT EXISTS:

1
2
3
SELECT *
FROM table_a a
WHERE NOT EXISTS (SELECT 1 FROM table_b b WHERE a.cd_field = b.cd_field);

The previous query is processed more or less as follows:

1
2
3
4
5
FOR x IN ( SELECT * FROM table_a ) LOOP
  IF ( NOT EXISTS ( SELECT 1 FROM table_b b WHERE x.cd_field = b.cd_field ) THEN
    --output the record
  END IF
END LOOP

This means that using NOT EXISTS always results in a full scan of the outer table whereas the query which uses NOT IN can exploit an index on table_a.cd_field. In general NOT IN performs better when the result of the subquery is small and there’s an index on the field used by the join. On the other hand, NOT EXISTS is to prefer when the subquery has many rows, the outer query is relatively small and there’s an index on the field used to join the two tables. In this latter case the cost of removing duplicates (DISTINCT) could be greater than the cost of the full scan of the outer table.

Of course this is a very general guideline, you should always conduct some test using indexes and so on to figure out which method to use.

NOT IN vs. NOT EXISTS When NULL Values Are Involved

NOT IN does not behave as you would expect when NULL values are involved. Suppose you have the following tables:

CD_FIELD DS_FIELD
1 ALPHA
2 BETA

TABLE_A

CD_FIELD DS_FIELD
1 GAMMA
NULL DELTA

TABLE_B

Now try the following query:

1
2
3
SELECT *
FROM table_a a
WHERE a.cd_field NOT IN (SELECT b.cd_field FROM table_b b);

Does the previous query return any row? The answer is NO it doesn’t, even if there is a value (2) in table_a.cd_field which is not present in table_b.cd_field.

I think this is because NOT IN internally uses the inequality operator. You should never use the equality or inequality operators with NULL, but IS NULL and IS NOT NULL, respectively. For this reason, in SQL, NULL is not equal to NULL. If you don’t believe me then try the following query:

1
2
3
SELECT 'test' c
FROM dual
WHERE NULL = NULL;

It returns 0 rows.

So how can you work this out? Enter the EXISTS (and NOT EXISTS) operator.

Try the following query:

1
2
3
SELECT *
FROM table_a a
WHERE NOT EXISTS (SELECT 1 FROM table_b b WHERE a.cd_field = b.cd_field);

The previous query returns 1 row, that is what you would expect since there is a row in table_a with cd_field = 2 which is not present in table_b.cd_field.

From now on beware of using NOT IN and IN when NULL values are involved.

Funny Long Jokes and Aphorisms About Programming (and Programmers of Course)

Here are some funny jokes and aphorisms about computer programmers and programming in general:


A man is smoking a cigarette and blowing smoke rings into the air. His girlfriend becomes irritated with the smoke and says, “Can’t you see the warning on the cigarette pack? Smoking is hazardous to your health!”To which the man replies, “I am a programmer. We don’t worry about warnings; we only worry about errors.”


A programmer is walking along a beach and finds a lamp. He rubs the lamp, and a genie appears. “I am the most powerful genie in the world. I can grant you any wish, but only one wish.”The programmer pulls out a map, points to it and says, “I’d want peace in the Middle East.”The genie responds, “Gee, I don’t know. Those people have been fighting for millennia. I can do just about anything, but this is likely beyond my limits.”The programmer then says, “Well, I am a programmer, and my programs have lots of users. Please make all my users satisfied with my software and let them ask for sensible changes.”

At which point the genie responds, “Um, let me see that map again.”


Two strings walk into a bar and sit down.The bartender says, “So what will it be?”The first string says, “I think I’ll have a beer quag fulk boorg jdkCjfdLk jk3s d#f67howe%^U r89nvy~~owmc63Dz x.xvcu”

“Please excuse my friend,” the second string says, “He isn’t null-terminated.”


Why computers are like men:1. In order to get their attention, you have to turn them on. 2. They have a lot of data, but are still clueless. 3. They are supposed to help you solve problems, but half the time they are the problem. 4. As soon as you commit to one, you realize that if you had waited a little longer, you could have had a better model.Why computers are like women:

  1. No one but the Creator understands their internal logic.
  2. The native language they use to communicate with other computers is incomprehensible to everyone else.
  3. Even your smallest mistakes are stored in long-term memory for later retrieval.
  4. As soon as you make a commitment to one, you find yourself spending half your paycheck on accessories for it.

A programmer finds himself in front of a committee that decides whether he should go to Heaven or Hell. The committee tells the programmer he has a say in the matter and asks him if he wants to see either Heaven or Hell before stating his preference.“Sure,” the programmer replies. “I have a pretty good idea what Heaven is like, so let’s see Hell.” So an angel takes the programmer to a sunny beach, full of beautiful women in skimpy bikinis playing volleyball, listening to music and having a great time. “Wow!” he exclaims, “Hell looks great! I’ll take Hell!”Instantly the programmer finds himself in red-hot lava with demons tearing at his flesh. “Where’s the beach? The music? The women?” he screams frantically to the angel.“That was the demo,” the angel replies as she vanishes.


Software Development Cycles:1. Programmer produces code he believes is bug-free. 2. Product is tested. 20 bugs are found. 3. Programmer fixes 10 of the bugs and explains to the testing department that the other 10 aren’t really bugs. 4. Testing department finds that five of the fixes didn’t work and discovers 15 new bugs. 5. Repeat three times steps 3 and 4. 6. Due to marketing pressure and an extremely premature product announcement based on overly-optimistic programming schedule, the product is released. 7. Users find 137 new bugs. 8. Original programmer, having cashed his royalty check, is nowhere to be found. 9. Newly-assembled programming team fixes almost all of the 137 bugs, but introduce 456 new ones. 10. Original programmer sends underpaid testing department a postcard from Fiji. Entire testing department quits. 11. Company is bought in a hostile takeover by competitor using profits from their latest release, which had 783 bugs. 12. New CEO is brought in by board of directors. He hires a programmer to redo program from scratch. 13. Programmer produces code he believes is bug-free…


Funny Short Jokes and Aphorisms About Programming (and Programmers of Course)

Here are some funny jokes and aphorisms about computer programming and programmers in general:


There are only 10 types of people in the world: Those who understand binary, and those who don’t.


It’s always a long day, 86,400 won’t fit into a short.


Why do programmers always mix up Halloween and Christmas? Because Oct 31 equals Dec 25.


“Knock, knock.” “Who’s there?” very long pause… “Java.”


Programming is like sex: One mistake and you have to support it for the rest of your life.


How can you tell when a programmer has had sex? When he’s washing the pepper spray out of his eyes.


Eight bytes walk into a bar. The bartender asks, “Can I get you anything?” “Yeah,” reply the bytes. “Make us a double.”


  1. How did the programmer die in the shower?
  2. He read the shampoo bottle instructions: Lather. Rinse. Repeat.

How many programmers does it take to change a light bulb? None - It’s a hardware problem


A programmer walks to the butcher shop and buys a kilo of meat. An hour later he comes back upset that the butcher shortchanged him by 24 grams.


Programming is 10% science, 20% ingenuity, and 70% getting the ingenuity to work with the science.


There are three kinds of lies: lies, damned lies, and benchmarks.


All programmers are playwrights, and all computers are lousy actors.


Have you heard about the new Cray super computer? It’s so fast, it executes an infinite loop in 6 seconds.


The generation of random numbers is too important to be left to chance.


“I just saw my life flash before my eyes and all I could see was a close tag…”


The computer is mightier than the pen, the sword, and usually, the programmer.


Debugging: Removing the needles from the haystack.


Two bytes meet. The first byte asks, “Are you ill?” The second byte replies, “No, just feeling a bit off.”


From the Random Shack Data Processing Dictionary:Endless Loop: n., see Loop, Endless. Loop, Endless: n., see Endless Loop.


One hundred little bugs in the code One hundred little bugs. Fix a bug, link the fix in, One hundred little bugs in the code.


A computer science student is studying under a tree and another pulls up on a flashy new bike. The first student asks, “Where’d you get that?”The student on the bike replies, “While I was studying outside, a beautiful girl pulled up on her bike. She took off all her clothes and said, ‘You can have anything you want’.”The first student responds, “Good choice! Her clothes probably wouldn’t have fit you.”


Programmer 1: We have a problem Programmer 2: Let’s use RegEx! Programmer 1: Now we have two problems


A SQL query goes to a restaurant, walks up to 2 tables and says “Can I join you”?


How to Disable All the Elements of a Form Using JavaScript

Here’s a little JavaScript function that disables all form elements:

1
2
3
4
5
6
function disableElements(formName) {
  var fm = document.forms[formName];
  for(var i = 0; i < fm.elements.length; ++i) {
    fm.elements[i].disabled = true;
  }
}

As you may have noticed the only parameter you need to pass is the form name.

How to Get the Number of Columns In a ResultSet In Java

In Java it is possible to retrieve the number of columns of a ResultSet dinamically, thanks to the ResultSetMetaData class. Here’s an example:

1
2
3
4
5
6
7
8
// Here you get the conn object. E.g.:
// Connection conn = DriverManager.getConnection(...);

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM your_table");
ResultSetMetaData rsmd = rs.getMetaData();
int numCols = rsmd.getColumnCount();
System.out.println("Number of columns in your_table: " + numCols);

The previous code retrieves and displays the number of columns of your_table.

Capturing Groups Using Regular Expressions (RegEx) In JavaScript

There are whole books about regular expressions so this post shouldn’t be intended as an exhaustive resource on the subject. It just shows how to extract a substring from a string using regular expressions in JavaScript so it must be considered just a tip not a tutorial on RegEx. Look at the following example:

1
2
3
4
5
6
7
8
9
10
var str = "http://www.alessandrolacava.com/?code=ALE69";
var regex = /code=(w+)&?/;
var results = regex.exec(str);
if(!results){
  alert("no match");
}
else{
  // first group
  alert(results[1]);
}

The previous code extracts the string that follows the code= part of str. That string is captured in the first group. of the RegEx, that’s why I use results[1] to display it. When you utilise groups–through the use of parenthesis ()–you can refer to them using indices, starting from 1. Indeed, at the index 0 you find the whole match. In the previous example, results[0] is equal to code=ALE69

How to Compute the Timestamp In JavaScript

Many sources use the term timestamp to refer specifically to Unix time, the number of seconds since 00:00:00 UTC on January 1, 1970. In JavaScript you can use the built-in object Date to compute this timestamp. Here follows an example:

1
2
var ts = Date.UTC('2007', '09', '28') / 1000;
alert(ts);

The previous code displays an alert with the number of seconds between 00:00:00 UTC on January 1, 1970 and 00:00:00 UTC on October 28, 2007.

How it works:

UTC is a static method of the Date object. The complete signature of this method is the following:

UTC(year, month, [day], [hours], [minutes], [seconds], [milliseconds])

Note that the parameters between square brackets are optional. The UTC method returns the number of milliseconds using UTC (Universal Time Coordinated). Hence the following code:

1
Date.UTC('2007', '09', '28') / 1000;

computes the number of seconds between 00:00:00 UTC on January 1, 1970 and 00:00:00 UTC on October 28, 2007. Note that the month parameter ranges from 0 (Jan) to 11 (Dec).