Umpteen Tips and Tricks on Java Development

There can never be tips and tricks for developers without a few tips and tricks on Java. Java is without doubt the most popular language today, and is growing from strength to strength. In this article, we are not looking at specific code on Java, or analyzing and understanding problems in Java. This is because there are hundreds and thousands of tips and tricks available on Java, and no article can do justice to it.

Hence, we focus on general issues and approaches to Java programming, and on improving performance of Java codes. You will find mostly tips and a few tricks on Java programming in this article, which should help you further.

Before we get into our favourite Java tips and tricks, let me make a few confessions. One this in no way is conclusive, and we are only touching the tip of a large iceberg. All the tips presented are collected from different sources including the WWW and also tips sent by readers.

The article itself is divided into four sections. The first one is for starting and learning Java programming. The second section looks at some of the testing and learning, which you can do without investing on infrastructure. The third section looks at improving the performance of Java programs. And finally we look at applets. An important aspect of Java is J2EE. We advise you to read the tutorials on the same in this edition.

1. The quickest way to learn Java 

Here are some tips of learning Java really fast. 

a. If you are a beginner, read an introductory book on Java, and there really is no better or faster way to learn the basics. You can also get lot of good tutorials in some of our earlier editions. Make sure to get a book that considers your existing skills (have you programmed before, have you done object-oriented programming before). Nobody becomes a Java expert in a week, or even in a month. It can take you several months to get a good grasp of the technology.

b. Get the latest Java SDK. The March edition of Developer IQ has one. 

c. To get some practice as a beginner, write small programs. If you are more experienced, try something larger (if you have no idea on what to write, take a look at this list of student Java projects). It is not a good idea to write larger programs right from the beginning, although these are more interesting and challenging. Start with Hello World, and write some programs that do simple functions.

d. Learn Objected Oriented Programming Concepts— If you do not know OOPS you can never appreciate Java. There are several good books on OOPS. You can start there. Again I will recommend you to go through the back issues of Developer IQ, to understand OOPS.

e. If you are not sure about what to learn next, I recommend an order of topics to learn. Start of with elementary programming concepts. Learn Java applet programming. Then switch over to multithreading and Object Oriented programming. Have a sound understanding of classes, objects, methods and interfaces. Then it is worthwhile looking in SWING. Then look at JDBC. Then get into J2EE. Your best bet is again the tutorials on J2EE in this edition, which gives you a very good idea on the subject. Never try learning everything at the same time; go in a logical path.

2. Advanced Tips of Learning and Mastering Java

There are two websites, which should be there on your Favourites list. The first one java.sun.com, and the second one is IBM Developerworks. Both Sun and IBM are driving the Java dream today. And they give you the right directions on where technology is moving.

There are several Java utilities that are available freely. You need to master many of these. Here is some of the Java related software, which we feel you should learn and master.

Eclipse from IBM— Eclipse is a great tool from IBM, and is fast becoming the most popular Java IDE outside notepad. Hopefully, we will be having a tutorial or a workshop on how to use Eclipse in the next the edition. 

Tomcat Apache Server—
This web server is definitely the most popular web server around as far as Java development goes. You should learn how to run web pages of the Tomcat Server.

Learn to use third party packages— Java is a true object oriented language. You must learn how to use third party packages and call classes from them. There are several neat little packages available from Sun which helps you to understand the OOPS concepts better.

Look at Open-Source Tools— The Open-Source community is very active on Java. And there are several tools such as Jboss, Junit etc that are being used for commercial software development. It is worthwhile mastering these.

Check User groups- A good Java developer needs to keep himself updated. The best way is to take part in the user groups in the World Wide Web. There are several newsgroups too. You may find it difficult in the initial days, but if you consistently participate in discussions you will start appreciating what goes in that space.

Learn to use Javadoc— Create API documentation of your classes with javadoc (it is part of the JDK). Inserting special comments in front of the declaration of a class, interface, method or variable does this. It is useful, even if you don't distribute your code. After a couple of months, API docs are helpful when you revisit your code. Try using English as natural language - developers will most likely be able to at least understand it. Note that other output formats than just HTML are supported using doclets.

Finally, you should look at specializing somewhere. Hot areas are J2EE, Enterprise Java Beans, Java Server Pages and Servlets. You could also look at other areas such as Struts too. The more you know, the better is the chance of you being employed by some one. 

3. Getting Java programs to run faster

Java being an interpreted language is slower than programming languages such as C or C++. Hence, you have to devise practices that would make your Java programs run faster.

a. Do not optimize everything since Optimizing can cause new errors in your code, and may make it less readable. So comment all changes and why you did them. 

b. Make suitable changes to algorithm -Check the algorithm first. The highest improvements are in most cases changing the algorithm. So, check this first before your start "low-level" Java code optimization. 

c. Use a profiler and check out which part takes the maximum to execute. Normally less than 10% or the code takes more than 90% of the execution time. Check the improvement of each change using a profiler. To find out where to start with optimization and where you will get the best improvements, use a profiling tool. Use it also to check the result of each code change. Sun's JDK has a profiling option build in the Java interpreter

java -prof yourClass (for Applications)
java -prof sun.applet.AppletViewer yourPage.html (for Applets)

The output is written into the file java.prof.

Sun's Java Workshop 2.0 has also a very nice profiling option with graphical output. There are several other commercial profiling tools available which is worth a check.

d. Wherever you can, use integer arithmetic instead of floating-point arithmetic. The int is the data type you should use. Avoid using double or float, because integer operations are much faster than floating-point operations. Do not use short, because it needs often to be casted to int, because most methods return integer values.

e. Learn Instantiation: Creating fresh objects are time consuming, and hence, wherever possible try using instantiation. Try to avoid new operators, reuse existing objects. This has more advantages, because less memory is used and less garbage collection is needed, which both also increase the execution time. 

f. Make sure that the variable for the loop counter is a local integer variable and not an instance or class variable. Restructuring the loops may also improve the performance, because you can optimize the compare operation, e.g. for (int i = 0; i < max; i++). The comparison is done against a constant, which is faster than comparison with another variable. And the decrement operator directly with the comparison is faster than a special increment operation. 

g. Try to make your classes final. This means that no one can inherit from these classes, but a most Java applets/applications there is no inheritance of own classes at all. Methods should be declared private, final or static if possible. Public methods do only make sense if they are called from other classes. Private methods can only be called from the same class, final methods cannot be inherited and static method cannot access instance variables. Avoid declaring methods that are synchronized, because this is very "expensive". Sometimes, it may make sense to inline code instead of calling another method, especially if the called method is small. 

h. For graphics intensive applets/applications it is important to optimize the paint() method. Use clipRect() to set the area to be redrawn to a rectangle that is as small as possible. If you have text output, make sure that you create fonts and colors outside the paint() method (e.g. in the constructor or init() method). 

4. Optimizing Java applets

Finally, we take a look at Java Applets. The biggest problem with Java Applets is their size. Here is a tip that is too good for me to not to include. How do you reduce the Java applet size? Web Designers always face problems of large Java applets. Here is a favorite tip seen in the book Java:Cookbook.

1. Use compiler optimization: javac -O. Sometimes it makes the code bigger.
2. Use jar files instead of class files.
3. Try to use inheritance as much as possible: you can reuse more code and you have to add less new lines.
4. Try to use standard APIs. Often they are better optimized in size than some private exotic packages. Always try to use efficiently that what we have already, even though they have better methods and so on.
5. Use short names.
6. Do not initialize big arrays because they will be initialized and put directly into byte code. You can do it later on the fly.



Added on November 3, 2003 Comment

Comments

Post a comment

Your name:

Comment: