One thing many people forget is that string concatenation is a polynomial time process. (O(n^2)).
Because every time it extends some characters, a new string is created and the original string and the extending characters are copied over.
In some programs, people try to write large arrays of data in to file, and to make it simple, they use a String variable to contain all the data, then write to a file. But the problem is, you have to loop through the array and adding each element to the string, this will take significantly more time than just using a BufferedWriter and just keep writing to it.
Just found this website that got so many interesting and fun java techniques. I think any new java learner, even some professionals will find this site very useful.
Suppose you have a table, tab or "," delimited. Look like this:
If you read each line and do
The result will be a 2 element String for 2nd line. Because java will ignore the repeated ",".
The solution to this is to do
Java language is safe but weird. A lot of the times it doesn't what you think it does.
For example, if you do
File f= new File( path) and then do f.delete().
It won't be deleted if it's directory. It doesn't even throw exception. So you have to recursively delete all files and folders inside before you delete it.