You are on page 1of 5

Introduction to Java:

programming tips, tricks and


online resources
Welcome to my page with tips on Java programming! This is a general overview,
you won't find code examples (this is no How to do ... in Java? text; I do have
some code examples elsewhere). As a beginner, please don't be overwhelmed
by the large number of tools and resources. This page just wants to give an
overview. You don't need all of the tools mentioned here and you might want to
do some things differently. These are just my personal recommendations.
A lot of related links are given so that you can find out more if you are interested.
As an example, it is mentioned that an obfuscator makes it harder to create
meaningful source code from given bytecode. You may never need such a tool, it
is just mentioned so that you know what it is, that it is possible at all and where to
find existing obfuscators.

General advice
1. As a beginner, read an introductory book on Java, there really is no better
or faster way to learn the basics. Make sure to get a book that considers
your existing skills (have you programmed before, have you done object-
oriented programming before). Take a look at the reader comments
sections of online book shops, they are often revealing. Note that nobody
becomes a Java expert in a week, ten days or 21 days (read Peter
Norvig's article Teach Yourself Programming in Ten Years for more on that
topic).
2. To get some practice as a beginner, write small programs. If you are
more experienced, try something larger (if you have no idea what to write,
take a look at this list of student Java projects). It's not a good idea to write
larger programs right from the beginning, although these are more
interesting and challenging (e.g. games). Learn the basics first, read Jon
Skeet's "Learning Java in stages" for an explanation.
3. If you have to do an assignment for a programming course, Patricia
Shanahan's "So, You Need to Write a Program but Don't Know How to
Start" might give you a few ideas.
4. The most important online resource is Sun's Java site. They defined the
language, they offer specialized Java libraries (e. g. for 3D graphics,
access to serial ports or e-mail), tutorials, a bug database (where you can
find out if errors that you experienced are known already) and more.
5. For answers to frequently asked questions check out Peter van der
Linden's Java FAQ and Roedy Green's Java Glossary. Apart from the
more advanced tips, they both also list solutions for typical Java problems
(gotchas): FAQ, Glossary. Also see The Java IAQ: Infrequently Answered
Questions. You might prefer to read an FAQ list in your language, e.g. the
FAQ of the German newsgroup de.comp.lang.java. However, these Q&A
lists are no replacement for learning the basics from a Java book or
course.
6. Gain best practice knowledge from browsing the articles at Collected
Java Practices. Various Java code examples are at Réal Gagnon's Java
HowTo site. and Javaa! JavaFAQ.nu.
7. Get the latest stable Java Development Kit (JDK, sometimes SDK or
JSDK) for your platform (most people will use those from Sun). Beta
versions of JDKs often contain errors, so get them only if you want to
check out new features that you plan to use in the future. Make sure that
you have a copy of the HTML API documentation as well—it shows you
which classes are available and what methods and fields they offer. If the
downloads are too large for you (JDK + docs are more than 50 MB), check
out the CD-ROMs that come with computer magazines—they might
contain what you want and save you some download costs. Note that an
end user who wants to run your program only needs a Java Runtime
Environment (JRE), not a JDK. JREs are typically much smaller (Sun's
JREs are around 10 MB).
8. Make use of packages (sets of related classes) to get some structure into
your Java project (java.util is an example for a predefined package). You
could have a package for data classes, one for I/O, one for GUI code etc.
Jon Skeet explains in "Packages - A set of rules to keep your compiler
happy" how compiling and running code with packages is done.
9. Try to create cross-platform code, no hardcoding of directories, line-
feeds etc. Download Sun's JavaPureCheck tool. It lets you find out if your
code is 100 percent Java and you can check if your code is compatible
with any given Java version (e.g. 1.1). This is helpful to test compatibility
with versions other than the one you yourself are developing with. There
seem to be problems with using that no longer supported tool with more
recent JDKs.
10. While you're programming, always have a webbrowser open with the API
docs of the Java runtime classes. Nobody can memorize the exact syntax
of all of those classes.
11. Try to stick to code conventions, e.g. those defined by Sun. Others are
linked to at DMOZ. They define how code is to be formatted, give
recommendations for naming variables, what characters are to be used for
indentation etc. This makes the source code more readable, especially if
several people work on the same project. You may want to use a code
beautifier to automate the process.
12. Add meaningful comments to your code, don't comment on each and
every self-explaining line of source code. On the other hand, comments
should not be the only documentation of what your code does.
13. If you are writing a library, add small example programs to it. You may
think that it's obvious how to use your library, but others could save a lot of
time looking at the example code (and they will send you less mails asking
for help).
14. Create backups of your code and all related files (documentation etc.).
Follow the Tao of Backup (coverage, frequency, separation, history,
testing, security and integrity). Automate the process. Use something like
CVS.
15. Have a copy of the Java Language Specification available. View it on-line,
download it or get the paper version.
16. Make use of newsgroups if you can't solve your problems. Try
comp.lang.java.programmer or a local group, especially if you prefer your
native language (e.g. de.comp.lang.java in German). Pick the right
newsgroup, if your question is about AWT or Swing and if there is a
newsgroup like comp.lang.java.gui, post there. Don't crosspost (= send
the same message to several newsgroups) unless you really think it fits
into several groups; in that case, specify a followup-to group. Check older
newsposting by searching a Usenet archive like Google Groups. Before
you post to a group, read some of the recent messages to get a feeling for
the type of discussion (also see Eric Raymond's "How to Ask Questions
the Smart Way"). Don't expect people to do your homework for you. Don't
send a big chunk of Java code and simply ask why it doesn't work. Read
the FAQ for the group so that the group members do not have to answer
the same things over and over again. Learn about the rules of Usenet (the
groups are no chatrooms; things like real names, good quoting style etc.
have proven themselves over the years). Try to give something back by
answering questions yourself.
17. Other Java online resources can be found browsing directories like the
Open Directory or Yahoo.
18. Note that there are other steps in a software development cycle than
just coding (= writing Java source code). Learn about requirements
engineering, design, quality assurance etc. Take a look at other
programming languages to learn what's good in Java and what's missing.
Java isn't the right tool for all possible tasks.

Tools
1. Start your first experiments with Java using a simple text editor and a
shell (console, command prompt). Don't use a big integrated development
environment (IDE) right from the beginning, learn to get along with
compiler and VM. However, IDEs can be powerful tools, especially for
some jobs like the creation of GUI applications (Open Directory IDE list).
IDEs with integrated debuggers let you step through your code, look at
the current values of variables and more. This is very helpful to find errors.
2. Use Jikes as your Java compiler. It's free, it's faster than javac, it often
gives more meaningful error messages. Try the emacs mode +E, I find it
very readable although I don't use emacs. If you like neither javac nor
jikes, take a look at this list of Java compilers.
3. If you have to deal with the deployment of a Java application to an end
user, consider creating an executable JAR (maybe with a deployment tool;
Sun has created Java Web Start for this purpose). Creating a native
application (e. g. an .exe file under Windows) can be done using a native
compiler, but you should consider the consequences.
4. Use JLint to fix problems in code that compiles but is erroneous
nonetheless (for typical gotchas like forgotten breaks in switch
statements). Similar hints are given by jikes if you run it in pedantic mode
(switch +P).
5. Create API documentation of your classes with javadoc (it is part of the
JDK). This is done by inserting special comments in front of the
declaration of a class, interface, method or variable. It's 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.
6. Create HTML file versions of your source code with Java2HTML so that
one can browse the code easily in a webbrowser. That's faster than having
to load each interesting file into a text editor. Since Java 1.4 javadoc can
also produce HTMLized source code if you use the -linksource switch. But
there is no syntax highlighting which makes it harder to read the code.
7. To clean up import statements, run ImportScrubber. It will remove
unnecessary imports, replace * imports with the names of all classes that
are really required and sort all imports.
8. You can optimize bytecode for execution speed or size using compiler
switches (e. g. -O with javac) or external applications. Intelligent
repackaging of JARs can shorten transmission and startup times, there is
a JSR to accomplish just that. However, modern Java virtual machines
(like the one from Sun) come with built-in just-in-time (JIT) compilers that
will normally be able to create faster code when they can do the speed
optimizations. So you might want to check out what works best for your
application. Also see my page on Java class file size reduction.
9. If you are afraid of people reverse-engineering your bytecode (converting
.class files back to .java) with a decompiler, you can run your code through
an obfuscator to make it harder for them.

In case you're distributing your code to others


1. Create documentation, not only API docs, but a general text on how to use
your code. Include sample code.
2. Make it clear under which conditions the code can be used. Name a
license, if possible. Add information on how to contact you
3. Create a single homepage for your project, including contact information,
downloads, online browsing of API docs and HTMLized code etc.
4. Consider using a service such as SourceForge, which does a lot of
administration so that you can concentrate on developing.
5. Submit the address of your project's page to search engines (Google,
AltaVista etc.), software repositories like Freshmeat and web directories
(Open Directory, Yahoo etc.) so that people will be able to find your code.

You might also like