Top 10 Java Programming Tips And Tricks


Java is a very important programming language and it is used in more than 4 billion devices. According to Oracle, over 5 billion Java cards are used in the whole world as it is the best Object-oriented programming language out there. Java is a bit complex language and it gets easier with daily practice. In this article, we are giving few tips and tricks for the beginners so that they can develop their skill in programming.

1. Single quote-Double quote dilemma:

You will notice that if you code “HaHa”, it would return Ha169. Double quotes are used to treat the characters as the string. On the other hand, single quotes are used to operand the char –valued to int values through the widening primitive conversion process. So, after the conversion, the numbers return 169.

2. Always return Empty Collections:

In a program, if a collection has no value and is returned, then make sure it returns with an Empty collection instead of Null elements. The unending ‘if else’ testing on Null elements are saved.

3. Avoid objects that are unnecessary:

Object Creation is a very expensive operation for Memory Utilization. So, only create or initialize objects which are necessary.

4. The Array – ArrayList dilemma:

The decision of choosing between Array and ArrayList is a tough one as they both have their advantages and disadvantages. Arrays and ArrayLists work as fixed and variable sizes respectively. Adding and removing elements are easier in ArrayList than Array. The array is multi-dimensional and ArrayList is one dimensional. So it all depends on requirements for you to use Array or ArrayList.

5. Avoid Memory leaks:

Java automatically manages the memory and memory leaks degrade the software performance. To avoid memory leakages release the database connection after the query. Also, try using Final block and releasing instances stored in Static Tables.

6. Reserve Memory:

There are some Java applications which need a lot of RAM and they are highly CPU intensive. They sometimes run slower than the other. But there is a way to improve the performance. Tomcat web server has 10 GB RAM and we can allocate RAM for the machine to run it.

Export JAVA_OPTS=”$JAVA_OPTS –Xms5000m –Xmx6000m –XX:PermSize=1024 – XX:MaxPermSize=2048m”

7. Simple String Search:

The Library method known as indexOf() is offered by Java. We use this method with String Object and in return, we get the position of the index of desired string. If it doesn’t find the string it returns -1.

8. Play sound using Java:

We all like to play sounds in Java and especially along with the games. It is a necessity. Here is an example of the codes which are used to play sound in Java.

9. Sending Email from Java:

Java offers a lot of services and sending email is one of them. It needs simple coding. First, we have to install the Java Mail Jar. Now set its path in the program’s classpath. Here is the code to send an email from Java.

10. Time measurement:
Java offers static methods in System class as there are applications which require precise time measurement.

-currentTimeMillis(): It returns time in MilliSeconds Epoch time.
long startTime = System.currentTimeMillis();
long estimatedTime = System.currentTimeMillis() – startTime;
-nanoTime(): It returns time in nanoseconds. nanoTime() is used to measure a relative time instead of absolute time.
long startTime = System.nanoTime();
long estimatedTime = System.nanoTime() – startTime;


Related Tips