April 24, 2001

Preparing a Log File With the Least File Operations

In a big project involving several classes, logging error messages can be required. There are several ways to do this, but a simple approach is to open the log file in append mode, add the messages to it and then close the file. However, writing to a file is a

Another Way to Fix a Single Quote in SQL Statements

A generic approach to making sure a single quote (‘) does not cause a SQL statement to fail is to replace the single quote with two single quotes, as in the following example: REPLACE(SQL_Statement,

Making a Read-only Collection

The Collections class provides static APIs to generate read-only orunmodifiable collections, lists, sets, or maps from specified entities. For instance, if you are sure that the ArrayList you’ve made should not be modified, you can generate a read-only list from it using the following: ArrayList list = new ArrayList(); list.add(“first”);

Using the instanceof Keyword in Java

When you are typecasting and using instanceof keyword in Java, there are several things you should keep in mind. Consider the following example program: class Point { int x, y; }class Element { int atomicNumber; }class Test { public static void main(String[] args) { Point p = new Point(); Element

How to Choose the Right Data Structure

Following are some tips for matching the most commonly used data structures with particular needs.1. When to use a Hashtable?A hashtable, or similar data structures, are good candidates if the stored data is to be accessed in the form of key-value pairs. For instance, if you were fetching the name

Using MACRO Definitions

By using MACRO definitions, you can easily write lines of code that are only included in Debug versions and not in Release versions. For instance, if you create a function ‘MyLog(x,y)’ that writes debugging information to a file you can define a macro such as: #ifdef DEBUG#define LOG(a,b) MyLog(a,b);#else#define LOG(a,b)#endif

The Difference Between Pointers and Arrays

Some programmers believe that int a[] and int * a are equivalent.Also some C++ books to say that pointers and arrays are almost same. This belief usually leads to hard to find bugs in the program. Suppose we have a function: void f(int *p); We can call it either passing

No more posts to show