Thursday, March 7, 2013

A suggestion on how to write the variable names in Java

Following Java variable naming conventions is a very important practice. First of all, if you are breaking the rules, then you will be punished with "Invalid Character" compilation error. Secondly, if you are not following the conventions, it will make your variable names confusing or unmeaningful or both to yourself and others who will use your code. We don't want our life difficult by even a micro-unit. You too.
For example, see the following two code snippets that show off the difference.

Thursday, February 21, 2013

What happens when we compile a program: "javac HelloWorld"?

When you go for compilation of a simple class, what happens? A .class file is generated for that class. Yes. But what the -verbose switch tells us is appealing. I mean by javac -verbose HelloWorld.java. Obviously you do not have anything to do with the information other than to learn for yourself the happenings, and also have some idea about the application's performance, if it's big. Let's go with a demo.

Wednesday, January 30, 2013

Removing unnecessary methods with inline code

Note: This holds only for smaller or restricted applications wherein the method logic is called at exactly one place or so. This is strict violation of object-oriented programming principles which advocate that each task or purpose should be served upon by an individual method as it warrants easy extensibility, and maintenance, apart from giving a clean picture of its job.
So, in any application, if we see that there might be future implementation change possibilities or the method is being called or may be called from multiple places in the application, then, even though if the current implementation has it that the method will be called from only one place, we have to put the logic in a separate method with an appropriate name and also mention in comment about the usage, if any.

Objective: "Put a method's logic into the body of its caller and remove the method”.

Friday, January 11, 2013

Exception in thread "main" java.lang.ExceptionInInitializerError


The API says:
Signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.

Lets take an example and see what is it exactly:

Tuesday, January 8, 2013

Time taken by various string concatenation methods

As the title goes, the objective of the folowing program will be to find out the time difference or performance various string concatenation methods:

i) + operator
ii) String class' concat() method
iii) StringBuffer class' append() method
iv) StringBuilder class' append() method

The program is followed by results in nanoseconds.