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.

Saturday, January 5, 2013

Java Keywords

As of Java 7, there are 50 keywords. I have compiled them for convenience by grouping them into different categories.

> The static keyword has been mentioned twice just for the sake of completing the category.
> In data types, all keywords but void may be used as data type as well as return type of methods, void can be used only as return type.

Wednesday, January 2, 2013

JVM - the runtime data areas (Part 2/2)

Part 2 is continuation of Part 1. If you have not read Part 1 or want to review, please head here.

I have created some independent images which have a small program on each and the memory allocation being shown. As I informed in Part 1, our main concern will be three runtime areas, viz., Method Area, Heap Area, and Java Stacks Area, so I have only shown them in the following explanations.

final and immutable things in Java, briefly!

I was always doubtful over two terms in Java (not limited to, though!), so they get a quote respect here:
final, and immutable
If one goes by English literature, both of them are twins or at least best friends. In Java, there's considerable difference in their usage, although their meanings are stunningly similar.
I've never heard of

JVM - the runtime data areas (Part 1/2)

Hi, This is Part 1 of a 2 part series that unfolds the abstract view of runtime memory allocation. You will understand how the variables get memory, how methods are created, how the control might flow. I have explained it in layman's language, for insight I will add links to a few external resources. Link to part 2 is provided at the bottom of this article.