JDK 1.4 Features

JDK 1.4 is one of the major versions released in February, 2002. The work started on the name of "Project Merlin" and includes the developments to support XML processing, java.nio package addition, Security restrictions, Logging API, JDBC 3.0 API, Assertions, Chained exceptions, Regular expressions and Drag and Drop. Two of these features, Assertions and Regular expressions are discussed here and the remaining are illustrated in the respective topics.

1. Assertions (of JDK 1.4 Features)

"assertion statement" evaluates to a boolean result where the output is assumed to be true always by the programmer. If the output of evaluation results in false, the assertion statement throws an exception java.lang.AssertionError at runtime. The assertion facility is more useful to check the validity of data. This is earlier achieved with if-else statements and exceptions.

"assert" is a keyword from JDK 1.4. The assert statement is used to know whether the user's input of two numbers evaluates to a sum greater than 10 or not. If the sum is not greater then 10, the assert statement throws exception. Following program illustrates.

import java.util.Scanner;
public class AssertPractice
{
     public static void main(String args[])
     {
          Scanner input = new Scanner(System.in);
          System.out.print("Enter an integer value:  ");
          int first = input.nextInt();
          System.out.print("Enter another integer value whose sum with the earlier is greater than 10:  ");
          int second = input.nextInt();
                                                   
          assert(first + second  > 10) : "Wrong values entered.  Values are " + first + " and " + second;

          input.close();
      }
}

JDK 1.4 Features
Assertions are the best practices for debugging and knowing logical errors crept unknowingly into the code. By default, the assert action is disabled in the JVM to increase the performance in regular practices. It must be enabled as follows while executing the program. Compilation is as usual.

java -ea AssertPractice

2. Regular Expressions (of JDK 1.4 Features)

Regular expressions is a concept first introduced with PERL (Practical Extraction and Report Language) language. For its importance, later many languages like VBScript and Java from JDK 1.4 supported this concept. Regular expressions are abridged form of code for evaluating and manipulating strings. String validations are easy but coding is very complex, sometimes, to write and understand. It is useful to validate passwords, social security numbers and email addresses etc. entered by the user. Java placed all the supporting classes in java.util.regex package.

Package java.util.regex

The three classes, available to support, in this package are Pattern, Matcher and PatternSyntaxException.

Finding the Occurrences of Matching Substrings

The following program finds the matching of characters in a given string. All matches starting and ending indexes are given.

import java.util.*;                          // for Scanner
import java.util.regex.*;                // for Pattern and Matcher

public class FindStartEndMatching
{
  public static void main(String args[])
  {
     Scanner input = new Scanner(System.in);
     System.out.print("Enter a string in which mathching is required:  ");
     String originalString = input.next();
     System.out.print("Enter a string of what is to be matched:  ");
     String matchString = input.next();

     Pattern pat1 = Pattern.compile(matchString);
     Matcher mat1 = pat1.matcher(originalString);

     boolean available = true;
     while(mat1.find())
     {
         System.out.println(mat1.group() + " string is found starting at index number  " + mat1.start() + " and ending at the index number " + mat1.end());
         available = false;
     }

     if(available)
     {
          System.out.println("Matching did not exist.");
     }
 }
}

Pattern pat1 = Pattern.compile(matchString);
Matcher mat1 = pat1.matcher(originalString);

compile() is a static method of Pattern class that takes a search string as parameter. The search string is converted and returned as a Pattern object. matcher() is a method of Pattern class that takes the original string as parameter in which matching is to be performed. This original string is returned as Matcher object.

mat1.group() + " string is found starting at index number " + mat1.start() + " and ending at the index number " + mat1.end()

The group(), start() and end() methods are defined in Matcher class. The end() method returns the ending character matched plus one. The group() method returns the total matching words (sequences) in the original string.

3. Splitting a String (of JDK 1.4 Features)

The JDK 1.4 version adds a new method to String class – split(). This method splits the string into independent words like a StringTokenizer. Following program uses split() method.

public class SplitIntoTokens
{
   public static void main(String args[])
   {
      String originalString1 ="Determination in work fetches";
      String tokens1[] = originalString1.split(" ");   
      for(int i = 0; i < tokens1.length; i++)
      {
         System.out.print(tokens1[i] + ", ");
      }

      System.out.println();
      String originalString2 ="Raju/Ramu/Rao";
      String tokens2[] = originalString2.split("/");   
      for(int i = 0; i < tokens2.length; i++)
      {
         System.out.print(tokens2[i] + " ");
      }
  }
}

JDK 1.4 Features

String tokens1[] = originalString1.split(" ");
String tokens2[] = originalString2.split("/");

The split() method of String class takes a string parameter comprising the delimiter. In the first statement, the delimiter is an empty space and in the second statement it is a forward slash (/). The method returns an array of strings where each element represents a word.

4. Exception chaining or exception wrapping (of JDK 1.4 Features)

Each runtime problem is represented by an exception class in Java. One problem may lead to another; that is, one exception may lead to another. This is known as Exception chaining. Exception chaining is useful to know the original cause of problem and is more useful to programmers for debugging and code maintenance. Exception chaining is discussed clearly with example code in Java Exception Chaining or Exception Wrapping.

1 thought on “JDK 1.4 Features”

  1. Hello, I think your site might be having browser compatibility issues.

    When I look at your blog in Ie, it looks fine but when opening in Internet
    Explorer, it has some overlapping. I just wanted to give you a
    quick heads up! Otjer then that, wonderful blog!

Leave a Comment

Your email address will not be published.