Java is the mostly praised programming language that created by James Gosling from Sun Microsystems in 1991. This programming language is publicly available in 1995 with its version Java 1.0. As time passes more version of Java introduced that creates problem for developers to write code. While writing SQL through JDBC or jOOQ, there are some Java developers committing mistakes that affect their development task.
SQL is a declarative language and developers can easily express a query in SQL. This language has neither relation with object-oriented nor with imperative thinking. Moreover, it is not as simple to express as we are thinking so. Java developers have to think in terms of set theory along with re-think their programming paradigm.
Here, we are going to figure out some common mistakes of Java developers and also provide solution for same. Check out and find out what mistake you are doing in your development task.
Table of contents:
Null pointers- The Big Issue Problem of blank exception handlers == rather than .equals for comparison Overriding While Mistyping the name of a method Assignment of Comparison ( = rather than == ) Errors of CapitalizationNull pointers- The Big Issue
One of the most common errors that Java Programmers are creating is Null pointers. It is must to search null pointer before your users as compilers are not search this for you. A NullPointerException will be thrown whilst an effort to admittance an object is created, and the reference to that object is null. There are many causes of null pointers, however, you either haven’t initialized an object, or you haven’t verified the return value of a function.
Different functions you will see that indicate an error condition, but you have to check return values to know what is happening. Normal testing may not pick up as the cause is an error condition that simply means your users will search problem for you. It is must to check the API function before making use of the object reference! Check Out below given codes and see whether you are facing problem or not.
{
// Accept up to 3 parameters
String[] list = new String[3];
int index = 0;
while ( (index < args.length) && ( index < 3 ) )
{
list[index++] = args[index];
}
// Check all the parameters
for (int i = 0; i < list.length; i++)
{
if (list[i].equals “-help”)
{
// ………
}
else
if (list[i].equals “-cp”)
{
// ………
}
// else …..
}
}
A common mistake is showed by this code. Under this situation, you have to enter more than 3 parameters to run the code in the finest ways. One can have NullPointerException at runtime, if no parameters are inserted. Sometimes your variables will be initialized or sometime they are not.
Hire on-demand dedicated developers of desired skill & experience.
Problem of blank exception handlers
For developers, it is very appealing to write blank exception handlers by ignoring errors. It is not possible for developers to search out any errors when you are running into problems and haven’t written any error messages. Even, you will get advantages through the simplest exception handler.
Try this example:
Place a try { .. }
catch Exception around your code for catching ANY type of exception and print the message. Developers don’t require writing a custom handler for every exception or don’t leave it blank and won’t know what is going on.
{
try {
// Your code goes here..
}
catch (Exception e)
{
System.out.println (“Err – ” + e );
}
}
== rather than .equals for comparison
While we are using the == operator, it simply means that we are comparing two object references to check whether they are pointing to same object or not. Developers can’t able to compare two strings for equality by using the == operator. Rather than, we can use the .equals method that inherited by all classes from java.lang.Object. See below codes to compare two strings:
// Bad way
if ( (abc + def) == “abcdef” )
{
……
}
// Good way
if ( (abc + def).equals(“abcdef”) )
{
…..
}
Overriding While Mistyping the name of a method
Through overriding, programmers can easily replace a method’s implementation through new code. Overriding is one of the excellent and useful features used by most OO programmers to get custom functionality. Mistype the method name, if you want to fall into with overriding. Moreover, if you are misplacing the name, you can able to with override. You can able to create latest method, but with the same parameter and return type.
// This should be WindowClosed
public void WindowClose(WindowEvent e) {
// Exit when user closes window
System.exit(0);
}
});
Compilers won’t lift up this one and problem becomes more frustrating. One of the best ways to add a println statement, to use good trace debugger (like Visual J++ or Borland JBuilder), to record a message in a log file, or and step through line by line.
Assignment of Comparison ( = rather than == )
It is a simple error to create by developer while writing SQL code. Before this, if you have used other languages like Pascal, you’ll come to know that the pitiable of language’s designers. While writing anything in Pascal, we are using the := operator for assignment, and leave = for comparison. It seems like a throwback to C/C++, from which Java depicts its roots.
If you are not able to spot this one while checking the code, your compiler will do this for you. Moreover, you will find an error message “Can’t convert xxx to boolean”, where xxx indicates a Java type that you’re assigning rather than comparing.
Errors of Capitalization
Capitalization errors are one of the most frequent errors created by developers. Being a simple, it is very difficult to investigate an uncapitalized variable or method and still not mark the problem. There is no silver bullet for searching this error, so it is advisable to easily train yourself by creating less of them. if you want to learn simple tricks, follow below given advises.
Hire dedicated team for your next web development project.
1. all methods and member variables use capitalization where a new word begins e.g. – getDoubleValue()
2. all methods and member variables in the Java API begin with lowercase letters
Developers have to create a conscious effort to get it right, if they want to use this pattern for all of your member variables and classes. This result into reduce in number of mistakes.