Unit 4
Unit 4
Java I/O and File: Java I/O API, standard I/O streams, types, Byte streams,
Character streams, Scanner class, Files in Java: File, FileInputStream and
FileOutputStream Classes
Packages and JAVA Library
• JAVA follows OOP
• Number of built in classes and interfaces are there – JAVA 8 - 217 Packages and
4240 Classes.
• For proper management of classes and interfaces, the related classes and
interfaces are grouped into packages and sub-packages.
• The default package is java.
• A large number of the packages and sub-packages are contained in the package
java.
Packages as Name Space
• A package provides a name space in which the classes and
interfaces have unique names.
• Same names may be used in different packages without
ambiguity.
• Similarly, the class name and interface also provide name spaces
in which the names of variables and methods are unique.
Built-in Packages
Package Name Description
java.lang Contains language support classes ( for e.g classes which defines primitive data types,
math operations, etc.) . This package is automatically imported.
java.io Contains classes for supporting input / output operations.
Contains utility classes which implement data structures like List, Hash Table,
java.util Dictionary, etc and support for Date / Time operations. This package is also called as
Collections.
java.awt Contains classes for implementing the components of graphical user interface ( like
buttons, menus, etc. ).
java.net Contains classes for supporting networking operations.
This package helps to develop GUI like java.awt. The ‘x’ in javax represents that it is
javax.swing an extended package which means it is a package developed from another package
by adding new features to it. In fact, javax.swing is an extended package of java.awt.
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
Constructors of Wrapper Classes
class Byte
Byte (byte Numb) The
Byte (String Strnumb) throws NumberFormatException NumberFormatE
xception occurs
class Short when the string
Short (short numb) is not in
appropriate
Short (String Strnumb) throws NumberFormatException
format.
class Integer
Integer (int numb)
Integer (String Strnumb) throws NumberFormatException.
class Float
Float (float numb or double number)
Float (String Strnumb) throws NumberFormatException
Constructors of Wrapper Classes
class Double
Double(double d)
Double (String Strnumb) throws NumberFormatException
class Character
Character (char ch)
class Boolean
Boolean (boolean value)
Boolean (String boolstring)
UseofValueOf.java
Methods of Wrapper Classes
•Integer Class
• public static Integer valueOf(int i)
• public static Integer valueOf(String s)
•Double Class
• public static Double valueOf(double d)
• public static Double valueOf(String s)
Boolean Class
public static Boolean valueOf(boolean b)
public static Boolean valueOf(String s)
Methods of Wrapper Classes
Method Description
byte byteValue() is used to convert an object of a wrapper class (such as
Integer, Float, Double, etc.) to a byte primitive type.
int intValue() Returns the value as an int primitive type.
long longValue() Returns the value as a long primitive type.
short shortValue() Returns the value as a short primitive type.
double doubleValue() Returns the value as a double primitive type.
float floatValue() Returns the value as a float primitive type.
These methods are defined in the Number class (which is the superclass of
classes like Integer, Long, Float, etc.), so they are available in various wrapper
classes.
Ex2.java
Methods for Conversion of Digital Strings
into numbers
• In Java, the parse methods are used to convert strings to their respective
primitive numeric types.
• They are commonly used to parse numeric values from user input, text files, or
other sources where numbers are stored as strings.
• These methods are part of the wrapper classes like Integer, Double, Float, Byte,
Short, and Long.
Ex3.java
Auto-boxing and Un-boxing
• Auto-boxing involves automatic conversion of the primitive
data types into its corresponding wrapper types.
• This includes the conversion of int to Integer, double to
Double, float to Float, boolean to Boolean, etc.,
• Unboxing is the reverse process in which the wrapping class
objects are converted into the corresponding primitive data
types
• This includes conversion of Integer to int, Long to long,
Double to double, etc
• Java 5 introduced the feature of auto-conversion of primitive
data types to the corresponding Wrapper class objects and
vice versa.
Autoboxing_UnBoxing.java
Methods isInfinite() and isNaN()
• The methods isInfinite() and isNaN() are used in Java to check
if a floating-point value is either infinite or "Not-a-Number"
(NaN).
• These methods are available in the Double and Float wrapper
classes.
• isInfinite() is occurer due to large number/small number
• Ex: 2522256897771/2, number/0
• isNaN() checks whether the invoking object is number or not
As per textbook
Exception Handling
Exception Handling
• types of errors
– Compile, run-time, logical errors(incorrect o/p)
• Logical Errors
• Multiplying when you should be dividing
• Adding when you should be subtracting
• Opening and using data from the wrong file
• Displaying the wrong output
39
try { General form-of exception handling block
// block of code to monitor for errors
}
catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
}
// ...
finally {
// block of code to be executed after try block ends
}
• Here, ExceptionType is the type of exception that has occurred.
Exception heirarchy
42
FAQs
• What is Throwable?
– Throwable is a super class that represents all errors and exceptions
• Which is the super class for all exceptions?
• What is the difference between exception & error?
– An exception is an error which can be handled. It means when an exception
happens, the programmer can do something to avoid harm.
– But an error is an error which can not be handled (Programmer can not do
anything –catastrophically errors)
• What are checked exceptions?
– That are checked at compilation time by the Java compiler
• What are unchecked exceptions?
– That are checked by JVM
try bocks
• The program code that is most likely to create
exceptions is kept in the try block, which is followed by
the catch block to handle the exception.
• In normal execution, the statements are executed and
if there are no exceptions, the program flow goes to
the code line after the catch blocks.
• However, if there is an exception, an exception object
is thrown from the try block.
• Its data members keep the information about the type
of exception thrown.
• The program flow comes out of the try block and
searches for an appropriate catch block with the same
type as its argument.
catch block
• A catch block is meant to catch the exception if the
type of its argument matches with the type of
exception thrown.
• If the type of exception does not match the type of
the first catch block, the program flow checks the
other catch blocks one by one.
• If the type of a catch block matches, its statements
are executed.
• If none matches, the program flow records the
type of exception, executes the finally block, and
terminates the program.
Multiple catch clauses
• In some cases, more than one exception could be raised
by a single piece of code.
• To handle this type of situation, you can specify two or
more catch clauses, each catching a different type of
exception.
• When an exception is thrown, each catch statement is
inspected in order, and the first one whose type matches
that of the exception is executed.
• If one catch statement is executed, the others are
bypassed, and execution continues after the try / catch
block.
• When you use multiple catch statements, it is
important to remember that exception subclasses must
come before any of their super classes.
• This is because a catch statement that uses a super
class will catch exceptions of that type plus any of its
subclasses. Subclass would never be reached if it
came after its super class.
• Since Java 7, a modification has been done in the
language so that one catch block can catch more than
one type of exception.
• The exception classes are connected by Boolean
operator |.
Java finally block
• Java finally block is a block that is used to
execute important code such as closing
connections (databases, network, disks,
commit in databases) etc.
• Java finally block is always executed whether
exception is handled or not.
Use of throw – to generate and for
rethrowing exceptions
• So far, you have only been catching exceptions that are
thrown by the Java run-time system. However, it is possible
for your program to throw an exception explicitly, using
throw statement.
• The general form of throw is shown here:
throw ThrowableInstance;
• Here, ThrowableInstance must be an object of type
Throwable or a subclass of Throwable.
• There are two ways to obtain a Throwable instance:
- creating one with the new operator
throw new exception_class("error message");
- using the parameter of catch clause -
throw exception_type_variable; (For rethrowing)