Last week ITVidya.com featured me in their member spotlight. Many thanks to Ajay, and the team at ITVidya.com.
Notes: This text was originally posted on my earlier blog at http://www.adaptivelearningonline.net
Last week ITVidya.com featured me in their member spotlight. Many thanks to Ajay, and the team at ITVidya.com.
Now let us understand how Exceptions work. The code shown below is a simple program that shows how to use Exceptions. Listen to the audio below as you view the code.
import java.io.*; |
Java2html |
Now let us compare how error handling is done with and without Exceptions. The PSEUDOCODE shown below has two methods: the first one is called aFunctionWithoutExceptions(), and the second one is called aFunctionWithExceptions(). As their names suggest the first method does error handling, the old way, by inspecting return values, and the second method does error handling with Exceptions.
//PSEUDOCODE |
Java2html |
When inspecting return values:
When using Exceptions:
Today Ajay and me completed a workshop on New Media for students at SCIT. I absolutely love talking about the potential of New Media, specifically blogging, and podcasting. Now it's not that blogging and podcasting are going to change the world, but the underlying concept of participatory media WILL. Blogging is a mechanism to achieve the vision of participation. Maybe over time it will evolve, maybe it will be replaced with something else, but the vision of grassroots participation is so powerful, that it cannot and should not be ignored.
Here are the slides from the presentation, followed by links to some blogs and websites we spoke about.
UPDATE: If you live in Pune or can travel there, be sure to come to BlogCampPune. It is going to be the first BlogCamp in Pune, and I'm sure it will be a great event. Many thanks to Tarun for taking the initiative to organize it.
Links:
Here's a nanocast on why we should we use use exceptions to handle error conditions in programs. I hope you enjoy it :-)
This is the first episode in a multi-part series on Exception handling in Java.
We are advised to rollback a JDBC transaction in the catch block that handles SQLException, just as shown in the code below. But what if a runtime exception is thrown while after midway in the transaction? Control will never go to the catch block and the transaction will never be rolled back.
|
Java2html |
01 public class ClassStructure { |
Java2html |
Many developers who are new to Java often have trouble with the CLASSPATH. It may seem complex, but once you understand the underlying concept, it is not difficult at all.
If you have programmed in C++, you will probably know that the PATH environment variable is used to locate dependencies. However, Java uses the CLASSPATH environment variable to locate dependencies. Take a look at the program below.
1 public class StudentRegistrationSystem { |
Java2html |
Notice that this program needs a class called 'Student'. The compiler as well as the runtime will need the Student class. The question is, how do they find it? They use the CLASSPATH environment variable.
The CLASSPATH is a list of directories, zip files or jar files.
Let us now understand how a JVM locates a class using the CLASSPATH. The Java runtime iterates through all entries in the CLASSPATH and searches for the class file in every directory, zip or jar file until the class is found. If it is not found, a ClassNotFoundException is thrown. If we put a class file in a directory not listed in the CLASSPATH, then as far as the Java runtime is concerned, it does not exist.
But wait, this is not the full story. Given a particular directory in the CLASSPATH, how does the JVM locate a class that it is searching for? Does it look for the class only in that directory, or does it look for the class in all sub-directories also? To help the JVM, we must follow certain conventions. I am assuming that you are familiar with the concept of packages in Java. If you are not, then you can read a simple explanation at Jarticles.com. The convention used is to match the directory heirarchy in which a class file is put, with the package name of the class.
Let us understand this concept with an example we have used above. Where will the runtime find the class edu.scit.studentreg.Student? When the JVM needs to locate the class edu.scit.studentreg.Student, it will look at the first entry in the classpath. Suppose it is c:/scit/classes. The JVM will now try to locate the class by zeroing in to the appropriate location based on the package name of the class. First it looks for a directory 'edu' in c:/scit/classes, if it finds the directory then it looks for 'scit' inside 'edu' and 'studentreg' inside 'scit'. Once in the 'studentreg' directory it looks for a file called Student.class. If the JVM finds the file using this algorithm, it is loaded, othewise it goes to the next entry in the classpath and tries to locate the file using the same mechanism. If the file cannot be found in any of the directories specified in the classpath, then a ClassNotFoundException is thrown.
The animation below explains how the JVM locates a class. Right click on area below and select "play" to start the animation.
So now you know, if you get a NoClassDefFoundError, it is because the Java runtime cannot locate your class file from entries in it's classpath. If the CLASSPATH variable has not been set, the current directory is used as the only classpath entry. If it contains any entries, then the current directory will not be considered part of the classpath, unless it is explicitly added.
Recommended Books:
In 2006 I did a lot research on elearning, instructional technology, and psychology of learning. After much deliberation I came to the conclusion that learning is all about taking in new information, reflecting upon it, practicing it, conversing with others and then sharing. This entire process not only strengthens the learners own understanding of a topic, but also helps the community by knowledge sharing.
Learning can be very effective when done as a conversation, rather than a monologue (with either a teacher or a book). Which is why some experts suggest that you should talk to your book while learning, by making notes. Having a conversation helps the learners derive knowledge rather than just having it spoonfed to them.
New media technology gives us an entire new way to have these conversations, by blogging and podcasting. I have written here, and here about the benefits of blogging. In the spirit of enabling learning through conversations, I created a website - Adaptive Learning Solutions, in Jne 2006. For most of 2006, I posted articles on learning and how the internet can be leveraged for information and to create a personal learning environment. This was to create a base for the readers to understand how to effectively use the net. Now the time has come to focus on posts for the people I set out to help - senior students of computer science and fresh employees working in software development. In 2007 you will mainly see articles that explain best practices in software design and development in Java. This will be the primary focus, however I will from time to time also write about Agile methodologies, and extreme programming. I hope to publish at least 2 posts every week and even start a screencast, which will be a video of a program being written in Eclipse, while I explain principles and best practices of software development.
All of you who would like to improve your software development skills are welcome to visit the website, and post your questions, comments, and suggestions.
Wishing everyone a very a happy and prosperous new year.
We all know that using StringBuffer to perform String concatenations is far more efficient than conctenating strings using the '+' operator. Since JDK 1.5 a new class called StringBuilder has been added. StringBuilder is just like StringBuffer, except that it's methods are not synchronized. You should use StringBuilder to improve your performance if you do not need thread safety.
Shown below is an example that concatanates 10000 strings using the '+' operator, StringBuffer, and StringBuilder, and prints the operation time in nanoseconds. The ability to get time in nanoseconds is another feature that was added in JDK 1.5.
package biz.adaptivesoftware.examples.string; |
Output:
Concatanating using the + operator |
Java2html |