Skip to main content

Posts

Showing posts from January, 2007

How do exceptions work?

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.*; public class  LearningExceptions  {    public static  void  main ( String args []) {      LearningExceptions le =  new  LearningExceptions () ;      System.out.println ( "Hello, this is a test program to        understand Exceptions" ) ;      try  {        System.out.print ( "Please enter your name: " ) ;        String name = le.getName () ;        System.out.println ( "Hey "  + name +  " how are you doing?" ) ;      }  catch ( IOException ioe ) {        System.out.println ( "Looks like the standard input          stream does not like you!" ) ;      }    }    private  String getName ()  throws  IOException  {      BufferedReader br =  new  BufferedReader        ( new  InputStreamReader ( Sys

Blogging for learning professionals

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: Stephen Downes' website on learning... this is a must read for those who believe tha

Why does Java support exception handling?

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. ----- COMMENT: AUTHOR: Azagu EMAIL: DATE: 01/23/2007 10:24:36 AM Hi There, Greetings and Wish you a Happy New Year. I am following your website for the past couple of months and I liked it very much. Are you planning to put the mp3 version of this series as well on the website? It will help the users to load it to their mp3 player and listen to them. You are doing a great job. ----- COMMENT: AUTHOR: Parag DATE: 01/24/2007 03:26:09 PM Hi Azagu, Thank you for your kind words and a very happy new year to you too. Most of the mp3's heavily depend on some code or an example also posted as part of the blog post. I am not sure if the mp3 will be very useful independantly. Also, once I make them downloadable, my hosting bandwidth requirements will increase

JDBC transaction rollback

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.   import  java.sql.Connection; import  java.sql.DriverManager; import  java.sql.SQLException; import  java.sql.Statement; public class  UnderstandingJdbcTransactions  {    public static  void  main ( String []  args ) {      Connection conn =  null ;      Statement stmt =  null ;      String jdbcurl =  "jdbc:derby:testdb;create=true" ;      try  {        Class.forName ( "org.apache.derby.jdbc.EmbeddedDriver" ) ;        conn = DriverManager.getConnection ( jdbcurl ) ;        conn.setAutoCommit ( false ) ;        stmt = conn.createStatement () ;        int  rowsPopulated1 =           stmt.executeUpdate ( "INSERT INTO PEOPLE VALUES      

Coding conventions - class and method structure in Java

It is very important to follow a consistent coding structure in classes and methods. Following a consistent structure will increase the readability of your programs. Very often companies have their own set of guidelines for namng and coding conventions. If the company you work for has it's own standards then you should follow those. All team members should ideally adhere to the same standards to make the code more readable. If everyone follows their own individual standards, the developers who have to maintain the code will find it very difficult to understand the code, as they navigate from one class to another. It is like driving on a street where everyone drives on the left till you reach a stop light, after which everyone is driving on the right.  Most people will manage, but it will be difficult and slow. I have shown a simple class below with all the elements that a class usually comprises of. Click on the play button to hear an explanation of the class structure.

Podcasting

I just heard a podcast from Podcast Academy by Kevin Crossman on how to produce podcasts on a low budget. Overall a good podcast, with some useful links: Podtrac Podcast411 Books:

The Java Classpath

  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  { 2      public static  void  main ( String args [ ]) { 3        ... 4        edu.scit.studentreg.Student st = 5          new  edu.scit.studentreg.Student () ; 6        ... 7      } 8   } 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

Moving from 2006 to 2007

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 Learni

New Year Resolutions

ok so new year resolutions are meant to be broken. This year I resolve not to break my resolutions. So I'll keep it simple. I will exercise everyday I will publish 2 blogs and 1 podcast every week on my elearning website I will prepare for all workshops and teaching events well in advance... no more last minute preparation I will utilize my time judicously Well, this much I am sure I can manage.

final Methods In Java

what are final methods benefits design efficiency no choice to be made for overriden methods can be inlined making methods final for efficiency is not a good idea... limiting without significant gain example Vector... and then the new collection classes

String vs. StringBuffer vs. StringBuilder

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; public class  StringConcatanations  {    public static final  int  MAX_ITER =  10000 ;       public static  void  main ( String []  args ) {      concatenate () ;      concatenateWithStringBuffer () ;      concatenateWithStringBuilder () ;    }       public static  void