Professional Documents
Culture Documents
8.0 Introduction
In previous chapters we have seen how user defined classes can be created and used
for solving different real life problems. Any program needs input/output operations. For
input/output operations, java takes help of library classes. You have already noticed that
every java program, written so far, have made use of output statements like
System.out.println (....); or System.out.print (...); and so on. Those output statements
have three parts separated by dots (.). The rightmost portion – println (..) or print (..) --
stands for a method or function lying defined in a library of classes called System that
controls several aspects of the run-time environment. The library class System belongs to
the java.lang package that gets automatically included or imported with every java
program.
The System contains three predefined stream variables -- in, out, err. [A stream can be
thought of as a flow of bytes or characters.] System.out refers to the standard output stream
(console VDU), System.in refers to the standard input stream (i.e. console keyboard) and
System.err refers to the standard error stream which is also the console VDU, by default.
In chapter-7 we have also made use of the input statement System.in.read (..) that calls
the method to read a character from keyboard. The read () method is applicable on the
input stream System.in that is capable of reading from a keyboard.
Thus we find that a user defined class can import and use any member methods or
functions available within the java’s library classes. Such reuse of library classes in a java
program makes coding much easier, simpler and systematic. Although System classes get
automatically included through java.lang package, there are many more java’s built-in
library packages, which are to be explicitly imported using import statement while a user
wants to include them in a user defined class for reuse. In example-2.4, you have seen such
an import of classes from java.util.* package while defining the DateDemo class.
Java provides many built-in library classes for the support of operations involving
Input/Output, String handling, Graphics, Networking, etc. Therefore, Java can be regarded
as a language with many modules of class libraries.
In all previous chapters we have seen just the use of java’s I/O statements. Now we
have to understand the inner details that are going on behind every such call for
input/output operations. So we will begin this chapter with the discussions on simple I/O
operations and then pass on to the String Handling operations.
8.1 Simple Input/output Operations
Unlike most other languages Java does not provide any special statement for
input/output operations. It is all done by invoking the methods of java.io.* classes. System
classes can handle only simple I/O operations, which take place only through standard
input/output devices i.e. the console keyboard (System.in), standard output device – the
VDU screen (System.out) and standard error device – the same VDU screen (System.err).
For I/O operations through any other I/O devices and for handling different data
types, the java.io.* classes are to be utilized. A java program is capable of reading from
any input stream and can write to any output stream other than keyboard and VDU. Of
course, appropriate physical peripheral devices are to be connected with the logical stream
interfaces to perform proper I/O operations.
A stream is a logical entity that can either collect from an input device or hand-over
to an output device any data or information as a flow of bytes or characters. A stream gets
linked with a physical device by taking help of Java’s I/O classes. Physical devices may be
different but all linked streams behave in the same manner in a run time environment. That
is the beauty of having a separate library of I/O classes.
Stream Stream
Program Output
Input Stre
Buffer Buffer
There are two types of java streams – byte stream and character stream. Byte
streams are used for reading or writing binary data. Character streams are used for handling
character type data. Character streams are more efficient than byte stream for I/O
operations.
Byte streams are defined by two hierarchies – at top abstract level: InputStream
and OutputStream classes and -- at concrete level: for each one of them several subclasses
to handle different devices like – disk, files, memory buffers, etc.
Similarly, character streams are also defined at two levels -- on top: Reader and
Writer classes for Unicode character streams and -- at implementation level: different read
() and write () methods for different I/O devices.
All java programs automatically import java.lang package, which includes a class
called System. System.out refers to the standard output stream object and System.in refers
to the standard input stream object. However, these streams can be redirected to any
compatible I/O devices. Byte oriented System.in object is a more complex to handle. For
reading data from console keyboard, modern Java makes use of character oriented
stream objects to make program writing easier. We will first see how to read character(s)
from a keyboard.
Characters to be read from a keyboard come via a memory buffer (fig-8.1) and that
is why to handle any keyboard input, a BufferedReader and read () method is used. Each
time read () method is called, a character is read from the input stream and returns the same
as a binary equivalent of that character. Study the codes and outputs shown in picture 8.1 –
Picture 8.1 Top – Source codes; Bottom-- Outputs
(What is the integer equivalent of a ‘blank’ character?)
Now study another example-8.1 of keyboard inputs.
System.out.print (ch);
} while (ch != 'q');
System.out.println (“ \n Out of Loop as you have typed ‘q’ to quit.”);
}
Mark the inclusion of the statement “ throws IOException” after the main () method.
Also observe carefully the portion of the code
by which a new BufferedReader type object instance, called conin, gets created and
connected with the System.in ( i.e. keyboard) device as an object of
InputStreamReader class type.
Thus the physical keyboard becomes connected with the logical Input Stream. Moreover,
the read () method (which helps reading a character as its binary equivalent) is called by
the code – conin.read(). The binary representation of the input character is again converted
to char format for users to see that one as a character.
Picture 8.2
Run this program to see what it does. It will go on reading the typed characters until ‘q’
button is pressed (Picture 8.2). Why printing does not take place after every character entry?
As System.in is line buffered (that means no further action will be taken until a
line-end signal is sensed), no input has been passed to the program until you press Enter
(indicating line-end).
Whatever characters are typed before pressing the return key, remain stored in the
ReaderBuffer and handed over to the program when enter or return key is pressed.
Although print () statement is included in the while loop, immediate printing was not taking
place. That is because, the read () method (used for character reading) is putting the pressed
character to the input buffer and not to the printer stream directly.
Another member function readLine () of the BufferedReader class can be used for
String inputs from a keyboard.
import java.io.*;
public class BRLin {
public static void main() throws IOException
{
BufferedReader conin = new BufferedReader (new
InputStreamReader (System.in));
String str1;
String str2 = " "; // A Null String is created
System.out.println ("Enter text lines and quit by typing stop");
do {
str1 = conin.readLine();
System.out.println (str1);
str2 =(str2 + str1); // Concatenation of Strings
} while (!str1.equals("stop"));
System.out.print (str2);
}
}
Picture 8.3
By running this program you will observe the output as shown in Picture 8.3. The
entered string is echoed on the VDU screen line by line. When you type stop, only then the
entire concatenated typed material (str2) is printed out. Verify the outputs with the program
codes.
Reading integer data from a keyboard can be performed either using Character
Oriented Input operations or by using Byte Oriented Input operations. Both these input
operations are not so straightforward and easy as output operations. Character oriented
operation needs call of some conversion method to convert (i.e. parse) the String object into
some integer value.
int i = in.readInt();
char ch = in.readChar();
double d = in.readDouble();
......
As byte oriented IO operation using Data Streams is machine dependent, it will not
be wise to use them for modern java applications and that is why no example will be
shown here.
try {
// reading statements
}
catch (Exception e {...}
Let us now study an example of reading integer data through a keyboard using exception
handling mechanism, further details of which will be explained in chapter-11.
import java.io.*;
class CharOrintInput {
public static void main () throws IOException
{
// create a buffer reader using System.in
String str;
int i =0;
try
{ i = Integer.parseInt(str);} // converting string into integer value
catch (NumberFormatException e) {
System.out.println ("Invalid data entry Format.");
}
The statement – str = keyin.readLine(); – reads the keyed-in character sequence and
the statement -- i = Integer.parseInt(str); -- converts that character sequence into integer
value by taking help of Integer wrapper class’s parseInt(..) conversion method.
Please take note of the try—catch use of the exception handling mechanism.
We have already seen the use of print() and println() methods of the System.out class.
System.out is a byte stream and can be used for simple input/output operations only.
However, character based alternative is mostly accepted in modern times. The PrintWriter
stream is character based and widely used in professional programs for console output. It
has a construct –
import java.io.*;
public class PrintWrite
{
public static void main()
{
PrintWriter vdu = new PrintWriter (System.out, true);
vdu.println( " This shows the use of PrintWriter for write on console .");
int i =27;
vdu.println(" Printing an integer value : " + i);
double d= 41.3241;
vdu.println(" Printing a double value : " + d);
char ch = 'A';
vdu.println(" Printing a Character : " + ch);
String str = " Computer";
vdu.println(" Printing a String : " + str);
}
}
Picture 8.4
Run this program and see the output (Picture 8.4). Here one vdu object of
PrintWriter class type is created to logically link the VDU screen (i.e. System.out) with
the program output stream.
The use of the parameter true indicates that automatic flush of the output buffer will occur
when a newline (‘\n’) character appears.
The main beauty of using PrintWriter class is that any output device can be linked
with its println () method. Further discussions on this point will be made in chapter-11.
Once a String object is created, no change in the characters, comprising the string, can be
allowed in java. String objects can be created either implicitly (i.e. declaring within double
quotes) or explicitly (using new keyword).
The java.lang package contains the definition of both String and StringBuffer
classes. Once declared, the contents of a String instance cannot be changed. Every String
becomes a fixed length, immutable character sequence. Those are limitations of a String
class.
However, StringBuffer is a peer class of String having much more flexibility.
Unlike String, StringBuffer is capable of growing and changing. We will first discuss about
the String class and then about the StringBuffer class.
8.2.1 String class
A string can be created also from a given array of characters as shown below –
Please note that a character set can be used to create String explicitly using
the new operator.
However Strings can be created implicitly in a much simpler way by using the string
literals. A String object gets created automatically for every string literal. For example,
the statement --
String str2 = “ BlueJ”;
creates implicitly the string object str2 with “BlueJ” as the sequence of
characters.
Such implicitly created strings can call all methods supported by the String class.
For example, the length () method can be applied to string object “BlueJ” as
shown below –
A complete java program can help you in understanding the use of String class.
Concatenation means joining different strings together placing them one after another. A
sentence can be thought of as concatenation of words with blank (or white space)
characters placed in between. Strings can concatenate with other types of data as shown in
example-8.7.
Be careful about the use of (+) in a java statement while using multiple data types for
string concatenation. Observe carefully the outputs of example-8.8.
Result1: 27
Result2: 9 [Try to understand – why the outputs differ?]
Methods by which string objects can be manipulated are called accessor methods built-in
with the class String. We will discuss about the most commonly used accessor methods here.
boolean equals( String str) Compares this string with specified str
String replace( char oldCh, char newCh) replace oldCh with newCh in this string
String substring( int startIndex, int endIndex) returns a sub-string of this string
}
}
If you run this program, the following output will appear on the terminal screen --
Unlike objects of String class, objects of StringBuffer class are capable of growing
and changing. StringBuffer class defines 3 constructors –
The StringBuffer is more flexible and can support many additional methods like –
append (..) – concatenates existing string with another string or a number or an object
asked to be appended; it allows subsequent calls to be chained together. For example –
class appendExample {
public static void main() {
String longstr;
int i = 12;
StringBuffer strbfr = new StringBuffer (50); // buffer size is set to 50
//chaining of append method
longstr = strbfr.append(“Value”).append(i).append(“ is an Integer.”).toString();
System.out.println (longstr);
}
}
insert(..) – can insert string or char or object at specified location in an existing String; the
general form is as follows –
where position indicates at which point the insertion will take place. Now an example –
class insertExample {
public static void main() {
StringBuffer strbfr = new StringBuffer (“ I must Java.”);
strbfr.insert(7, “ learn ”);
System.out.println (strbfr);
}
}
By running this program you will get the output – I must learn Java.
Length () – The current length of a stringBuffer can be found out by calling this method.
Capacity () -- whereas the total allocated capacity of the string buffer can be found out by
calling capacity() method.
ensureCapacity( int capacity) – by calling this method you can ensure a desired capacity
of a string buffer instead of relying on the default allocation of buffer size.
setLength( int length) -- size of an already created string buffer can be reduced to a
specified length.
setCharAt(int position, char ch) -- change the character at the specified position with the
new character ch. charAt( int where) can be used to obtain the character at the specified
location.
reverse () – characters within a string buffer object can be reversed by calling reverse ()
method.
delete (int startpoint, int endpoint) -- can delete a sequence of characters from the
specified starting point unto the end point.
deleteCharAt( int position) – can delete a single character at the specified location.
replace( int startpoint, int endpoint, String str) -- can replace a set of characters from
the specified starting point to the end point by the specified string str.
substring( int startIndex) , substring( int startIndex, int endIndex) -- can return only a
portion of a string buffer from start point up to the end or from the start point up to the
specified end point.
The above list is not complete. For the complete list, you can refer to Java’s Complete
Reference Manual. However usefulness of the methods of string buffer class can be
understood from the example – 8.10 as shown below :--
As mentioned earlier, the keyword static is used to call a member method of a class without
creating an instance of that class. Besides the main () method, it is also possible to declare
any data member or method member of a class as static if we wish to access them
without any object reference.
It is illegal to refer to any instance variables inside a static method. Static methods and
variables can be used independently of any object. To call static method from outside
of a class, one can use –
Classname.method(...)
--- where Classname is the name of the class where the static method remains declared.
This form is similar to non-static methods where reference is made in the form of
objectname.method(). Just observe that in example- 8.11, the static method display () –
defined in the class StaticDemo – is used directly by the class StaticBeauty without
creating an object-instance. That is the beauty of a static method. In the similar way, static
data can also be used directly from anywhere simply by specifying
classname.variable_name.
class StaticBeauty {
public static void main( ) {
If you run this program, the following display will appear on the terminal screen –
value of a = 54
value of x = 6
value of x + a = 60
value of y = 36
value of z = 3.2
Java has a feature called packages, which helps in storing different class definitions
(for re-use) in an orderly manner. Packages can be regarded as containers of related classes
and interfaces [discussed in chapter 9 – section 6] avoiding any naming conflicts.
You have already made use of packages like java.io [containing input output
related classes], java.util [containing utility related classes], etc. The java language
package, java.lang, gets automatically included with any java program; but to include other
packages exclusive use of import statement becomes essential. You have already made use
of such import statements like
import java.util.*;
import java.io.*;
at the top of a java source code. The asterisk (*) indicates that all the classes of that package
are to be imported. However any particular class of a package can also be included by
specifying the class name as shown below:--
In addition to java’s own library packages, any programmer can create his/her own
packages containing useful classes or interfaces for subsequent reuse. Suppose you want to
develop bank-transaction software. The required classes like BalanceCheck, CashWithdraw,
DepositAmount, etc. can be compiled and stored in a package to which you can give a
name, say, Banking. Such a package can be included with any banking related program.
That is the main advantage of packaging which can avoid name conflicts and help re-use
very efficiently.
The *. class files generated by the compiler must be placed in a directory having
the name same as that of the package. The *. class files for java.util package are stored
under the directory util which is a sub-directory of the directory java.
In all previous examples, package statement has not been used although other three
parts have been used in programs as per specific requirements.
package <package_name>;
Suppose you choose the package-name as MyPackage. In that case all the *.class files
which are to be included as a part of MyPackage must be stored in a directory called
MyPackage. Directory name must match exactly with the package name.
Such a package hierarchy remains stored in a file system as pkg1\pkg2\pkg3 [in windows]
or pkg1/pkg2/pkg3 [in Linux/Unix]. The CLASSPATH can also take care of the directories
where you have stored your packages.
8.5 Conclusion
This chapter deals with the Input/Output and other library classes available in the java
packages. Special emphasis has been given on the methods available with input/output
classes, String classes and StringBuffer classes. With suitable examples their uses have been
demonstrated.
The specialty of static variables and methods has also been explained. A brief
introduction to java’s package concept has been given.
It may not be out of place to mention that besides the packages already mentioned,
java has many more packages like ---
Discussions about all these advanced topics have been kept out of scope from a beginner’s
book like this one. However, discussions about the uses of java.awt and java.applet classes
for window’s programming will be made in chapter-12.