Java Programming Tutorials

Java Programming Tutorials And Articles
  Title / Author Replies
Java has a class called File, which actually represents not a file you can read or write, but rather a filename. Using this class is highly recommended instead of simple strings, because it makes your file naming operating-system independent. You can easily find out wether a filename is relative or...
When many items of data of the same class or type have to be stored, it is more efficient to use an array than separate variables or objects. For example, if the temperature on each day of the (non-leap) year had to be stored, rather than set up tempjan1, tempjan2... tempdec31 (365 variables)...
Automatic Conversion In Java type conversions are performed automatically when the type of the expression on the right hand side of an assignment operation can be safely promoted to the type of the variable on the left hand side of the assignment. Thus we can safely assign: byte -> short -> int...
Introduction A pattern of special characters is used to specify the format of the date. The example below demonstrates some of the characters. For a complete listing, see the javadoc documentation for the SimpleDateFormat class. Note: This example formats dates using the default locale...
Growing an Array in Java Suppose you have an array of some type that is full, and you want to grow it. Employee a = new Employee; // array is full int newLength = a.length * 11 / 10 + 10; Employee newArray = new Employee; System.arraycopy(a, 0, newArray, 0, a.length); a = newArray;That...
Introduction Assertion facility is added in J2SE 1.4. In order to support this facility J2SE 1.4 added the keyword assert to the language, and AssertionError class. An assertion checks a boolean-typed expression that must be true during program runtime execution. The assertion facility can be...
Introduction Handling character strings in Java is supported through two final classes: String and StringBuffer. The String class implements immutable character strings, which are read-only once the string has been created and initialized, whereas the StringBuffer class implements dynamic...
What Is GlassFish? Depending on how you look at it, GlassFish is any of the following: A community. The GlassFish community currently includes over 1100 members participating in the GlassFish project, with the objective of developing an open source application server that implements Java EE...
Drawing Lines The drawLine method belongs to the Graphics class. This example draws a fan of twenty lines from the bottom center of the Applet along the top edge. The applet's getSize method is used to find the dimensions of the applet. import java.awt.*; import java.applet.*; public...
How do I instantiate a bean whose constructor accepts parameters using the useBean tag? Replacing Characters in a String? Searching a String? Connecting to a Database and Strings Handling? What is a transient variable?
The classical input/output (I/O) library in Java contains classes that support I/O handling of streams with compressed data. You can easily compress and decompress any text or binary data to or from any I/O stream using either a file or any other stream (e.g., a servlet output stream). In this...
Exception is an abnormal condition that arises in the program during execution. When such a condition arises in the program, an appropriate code is written so that it can be handled. It is very much similar to the error codes returned by a subroutine in the C language. Whatever value the function...
Introduction The code which is expected to generate the exception must be enclosed in the try and catch block. The very basic form of the exception-handling block is as following: try { //code which is expected to generate the exception } catch (<ExceptionTypeClass> object) {...
Before you can catch an exception, some code somewhere must throw one. Any code can throw an exception: your code, code from a package written by someone else such as the packages that come with the Java platform, or the Java runtime environment. Regardless of what throws the exception, it's always...
The finally clause is written with the try-catch statement. It is guarenteed to be executed after a catch block or before the method quits. try { // statements } catch (<exception> obj) { // statements }
You have seen that an exception is thrown either implicitly in the try block and there is a catch block ready to handle it. But, there are methods, which throw the exception, but do not catch it within the method body. In this case the method which is throwing the exception must use a throws clause...
Introduction The code bound by the try block need not always throw a single exception. If in a try block multiple and varied exceptions are thrown, then you can place multiple catch blocks for the same try block in order to handle all those exceptions. When an exception is thrown it traverses...
Just like the multiple catch blocks, we can also have multiple try blocks. These try blocks may be written independently or we can nest the try blocks within each other, i.e., keep one try-catch block within another try-block. The program structure for nested try statement is: try { //...
Java defines several built-in classes for exception handling. All these classes are a part of the java.lang package which is automatically imported. That is why, while throwing an exception you did not specifically include any package for exception classes. Apart from the built-in exception...
The exception handling mechanism is a cleaner way to impose check upon the conditions which may lead to errors. But you should not be carried away by it and start using exception handling anywhere and everywhere you want. Exceptions must be carefully planned and implemented. The following cases...