Stream Based I o
Stream Based I o
Rajam Naidu
Stream based I/O (java.io) -1
• This topic introduces one of Java’s most important packages, java.io,
which supports Java’s basic I/O (input/output) system, including file I/O.
• All streams behave in the same manner, even if the actual physical
devices to which they are linked differ.
Stream based I/O (java.io) - 2
• Java byte streams are used to perform input and output of 8-bit
bytes. Though there are many classes related to byte streams
but the most frequently used classes are , FileInputStream and
FileOutputStream.
• Scanner reads formatted input and converts it into its binary form.
• Scanner can be used to read input from the console, a file, a string,
or any source that implements the Readable interface or
ReadableByteChannel.
• For example, you can use Scanner to read a number from the
keyboard and assign its value to a variable.
Enumerated Data Type -1
• An enumerated type defines a list of enumerated values. Each value is an identifier. For
example, the following statement declares a type, named MyWeekDays, with values MON,
TUE, WED, THU and FRI in this order.
enum MyWeekDays{MON, TUE, WED, THU, FRI};
• A value of an enumerated type is like a constant and so, by convention, is spelled with all
uppercase letters.
• So, the preceding declaration uses MON, not mon. By convention, an enumerated type is
named like a class with first letter of each word capitalized.
• Once a type is defined, you can declare a variable of that type:
MyWeekDays day1;
Enumerated Data Type -2
• The variable day1 can hold one of the values defined in the enumerated type MyWeekDays or null,
but nothing else. Java enumerated type is type-safe, meaning that an attempt to assign a value
other than one of the enumerated values or null will result in a compile error.
• The enumerated values can be accessed using the syntax
• EnumeratedTypeName.valueName
• For example, the following statement assigns enumerated value BLUE to variable day1:
• day1 = MyWeekDays.THU;
• Note that you have to use the enumerated type name as a qualifier to reference a value such as
THU.
• As with any other type, you can declare and initialize a variable in one statement:
• MyWeekDays color = MyWeekDays.THU;
Type Wrappers -1
• Despite the performance benefit offered by the primitive types, there
are times when you will need an object representation.
• For example, you can’t pass a primitive type by reference to a
method. Also, many of the standard data structures implemented by
Java operate on objects, which means that you can’t use these data
structures to store primitive types.
• To handle these (and other) situations, Java provides type wrappers,
which are classes that encapsulate a primitive type within an object.
Type Wrappers -2
• Character is a wrapper around a char. The constructor for Character is
• Character(char ch)
• Boolean is a wrapper around boolean values. It defines these constructors:
• Boolean(boolean boolValue)
• Boolean(String boolString)
• double doubleValue( )
• float floatValue( )
• int intValue( )
• long longValue( )
• short shortValue( )
Autoboxing, auto-unboxing - 1
• Autoboxing is the process by which a primitive type is automatically
encapsulated (boxed) into its equivalent type wrapper whenever an
object of that type is needed.
• There is no need to explicitly construct an object. Auto-unboxing is
the process by which the value of a boxed object is automatically
extracted (unboxed) from a type wrapper when its value is needed.
There is no need to call a method such as intValue( ) or
doubleValue()
Autoboxing, auto-unboxing - 2
• Autoboxing and auto-unboxing greatly streamline the coding of several
algorithms, removing the tedium of manually boxing and unboxing values.
They also help prevent errors. Moreover, they are very important to
generics, which operate only on objects. Finally, autoboxing makes
working with the Collections Framework (described in Part II) much easier.