Java For Your Thoughts: Tips for Beginners

Source: twiki.org

by Aaron Ong

In Ateneo, IT students tackle Java as they go through their first programming courses. Some stumble along the way as they yell at their screens in utter frustration. Then there are those who breeze through Java programming as if it were a Sunday drive. With enough time, everyone gets it all together as they celebrate their newfound skills in creating awesome programs. No matter how good you may be, however, there may be some things that you can still learn before you go out to conquer the IT world.

1) Take Advantage of the Java API

Source: noucamp.org

The Java Application Programming Interface is a library maintained by Oracle that
documents everything found in the Java SDK’s ocean of packages, from classes and methods to the simplest variables. The API for the latest version (Java 7) can be found online at http://docs.oracle.com/javase/7/docs/api/.

At first, you may feel that you don’t really need it. A lot of the classes that you need to
know are taught in class, and you can simply memorize their syntax and use them. Later on, however, you may end up needing certain packages for your program. With the API, you can find the exact class that you need out of the thousands found in the library. Plus, your professors will be impressed if you go Magis and create a program with more advanced code.

Don’t have internet access at all times? You can choose to download the API from http://
www.oracle.com/technetwork/java/javase/documentation/java-se-7-doc-download-435117.html
. You’ll thank me the next time your router goes out.

2) Improve your code’s readability

Source: homeandlearn.co.uk

For your (and your professor’s) sake, it’d be best if you adhere to certain conventions.
Yes, your code would work either way, but whoever’s checking your code might not appreciate the garbled mess that they have to debug.

For example, you can name your variables and methods using camel case. You type
multi-worded variable names without spaces in between. The first word is always in all small letters, but from the second word onward, the first letter is capitalized. Apart from the naming format, you should also give them meaningful and indicative names. This is obvious, but it’s also something people take for granted.

There’s also indentation. Usually, when a new code block (anything in between curly
braces) is introduced, the entire block is indented. The code within the braces themselves is also indented. These help coders see the boundaries of each code block clearly. Certain IDE’s do this automatically, but if you’re using a plain editor, it’s best to keep this in mind.

Finally, it’s good style to include comments in your code. No matter how unnecessary
you think it may be, comments will help reduce the time it takes for other people to understand your code. This is useful for group work and project defenses. And if you ever need to revisit old code, you can jump right back in without cursing your past self for being so sloppy. So if your code is fairly complex or if someone else will read it, it’s best to put those double slashes to use.

If you want, you can even take your documentation further by using Javadoc, Java’s own
documentation utility. It creates HTML pages based on your comments, which are in the format of those in the Java API. It’s also integrated in certain IDE’s for your convenience. It’s not very practical for beginners, but you’ll need it should you ever become a software engineer. You can learn more about it at http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html.

3) Test Classes in BlueJ

Source:wikimedia.org

BlueJ is to programmers as training wheels are to bike riders. It’s the first IDE introduced
in CS 21A classes. Professors aren’t too thrilled when they see upperclassmen use it, so I don’t recommend being too attached to it. However, it does offer a nice feature that can help save time: you can instantiate classes outside of Java code. To do this, you just right-click on the box that represents a certain class, and select the constructor that you would like to use (e.g. new BankAccount( String ownerName ) ). You can name that instance and supply the constructor arguments. A red box that represents that instance should then appear at the bottom of the window. You can right-click on it and select one of its methods to execute it. You can also select Inspect to check its variables and their values.

How is this useful? It’s a faster way to test your classes. Instead of creating a tester program with a lot of print statements and an input loop, you can interact with the object instance directly. If you decide to make changes in your class (such as adding variables), you don’t need to update your tester program as well. Other methods of debugging exist, but as a CS 21A student before, I found this to be a quick and easy way to test my code.

There you go. I hope you find these tips useful. For the seemingly obvious tips, these are
the ones taken for granted—you’ll learn how important they are later on. With that, I wish you good luck on your quest to learn Java.


Read more articles

Comments