Unit - I: Chapter - 1
Unit - I: Chapter - 1
CHAPTER – 1
java
CONTENTS
1. INTRODUCTION
1.1. history of java
2. STRUCTURE OF JAVA PROGRAM
2.1. standard program and Applets
2.2. packages
2.3. importing classes and Packages 3. THE JAVA
COMPILER AND VIRUTAL
MACHINE
4. DOCUMENTATION COMMENTS
5. DATA TYPES
6. METHODS
6.1. parameters
6.2. Overloaded Methods
7. EXCEPTIONS
7.1. throwing an exception
7.2. handling exceptions
8. ACCESS MODIFIERS
9. INHERITANCE AND METHOD OVERRIDING
9.1. Super-classes and Subclasses
10. GENERIC METHODS
11. GARBAGE COLLECTION
12. RECURSION
12.1. recursive function
12.2. induction
12.3. recursive methods
13. TESTING AND DUBUGGING
13.1. what is testing
13.2. designing test data
13.3. debugging
1. INTRODUCTION
1.1. HISTORY OF JAVA
10. HotJava
- The first Java-enabled Web browser
characteristics of java
• Java is simple
• Java is object-oriented
• Java is distributed
• Java is interpreted
• Java is robust
• Java is secure
• Java is architecture-neutral
• Java is portable
• Java is high-performance
• Java is multithreaded
• Java is dynamic language
• Java is simple
Java is simple language, can be learned easily.
• Java is object-oriented
Java provides Abstraction, Encapsulation, Inheritance and
Polymorphism.
• Java is distributed
Java is tuned to the Web, So java program can access data
– across the Web as easily as they access data from local
system.
• Java is interpreted
Java is both Interpreted and compiled. The code is
complied to byte code that’s “ Binary “ and “ platform
independent”.
• Java is robust
Java forces the programmer to handle unexpected errors.
This ensure that java programs are “Robust” (Reliable) and
bug free and don’t crash.
• Java is secure
S. No Application applet
1. METHOD name is “ main” METHOD name is “ int”
Example:
import java.awt.*;
import java.awt.Button;
import java.awt.*;
in this syntax all the classes in awt package have to
imported
import java.awt.Button;
in this syntax only button class from awt package
have to imported
3. The java compiler and virtual machine
• Java source code is converted into Java Byte code (or) J-
code.
* When the program <programName> is compiled, the
program source code is read from the file
<programName.java>
* The compiled byte code is written in the file
<programName.class>
* Java Virtual Machine (JVM) is a software that
interpret and executes the byte code.
• The main task of JVM is – the loading of .class files
• JVM- has class loaders that loads the .class files, and
executed by the execution engine.
• Each java applications run inside a JVM.
• JVM starts when you start a java program and terminates
when the program ends.
• The no. of JVM starts when you start a java program and
terminates when the program ends.
• Abstract data types are based on primitive data types and have
more functionality than primitive data types.
• Example
“String” is an abstract data type that can store letters, digits
and other characters like /,(), : , ; , $ , #
syntax:
[<access_specifier>] [<modifiers>] <return_type>
<method_name> ([argument_list])
{
//statements
}
• Example:
public static int abc (int a, int b, int c)
{
return a+b*c+b/c;
}
Where:
abc - name of the method
int - return type of the method is ‘int’
(int a, int b, int c) – formal paramenters of the
method abc, each is of integer
type.
• If the method is invoked by the statement
Z = abc (2,x,y)
then 2,x,y are the ‘actual parameter’.
- type of error
- status of the program when the exception occurred
* Key words in Excepting:
try
catch
throw
throws
finally
• Try Block
syntax:
try
{
//statements that may cause an exception
}
* If an exception occurs within the try block, the appropriate
exception-handler that’s associated with the try block handles
the exception.
* A try block must have at least one catch block that follows it
immediately.
• catch Block
syntax:
try
{
//statements that may cause an exception
}
catch(……)
{
// error handling routines
}
• The catch statement take the object of the exception class that
refers to the exception caught.
• Once the exception is caught, the statements within the catch
block are executed.
• The scope of the catch block is based on the statement in the
preceding try block.
Exception handling mechanism
• In java, exception-handling facility handles abnormal &
unexpected situation in a structured manner.
• When an exception occurs, the java runtime system searches
for an exception-handler (try-catch-block).
• If a handler is not found in the current method, the handler is
searched for in the ‘calling method’.
• The searches goes on till the runtime system finds an
appropriate exception-handler.
• EXAMPLE:
// derivedclass.java
class baseclass
{
public int divide(int num1, int num2)
{
return num1 / num2;
}
}
public class derivedclass extends baseclass
{
public int divide(int a, int b)
{
return super.divide(a,b);
}
public static void main(String args[])
{
int result = 0;
derivedclass d1 = new derivedclass();
try
{
result = d1.divide(100, 0);
}
catch(ArithmeticException e)
{
System.out.println(“Error…division by zero”);
}
System.out.println(“The result is: “ + result”);
}
}
Multiple catch block - example
}
catch(ArithmeticException e)
{
System.out.println("Error---division by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Error----out of bounds");
}
catch(Exception e)
{
System.out.println("some other error");
}
System.out.println("The result is: " + result);
}
}
EXAMPLE-2
}
catch(ArithmeticException e)
{
System.out.println("Error---division by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Error----out of bounds");
}
catch(Exception e)
{
System.out.println("some other error");
}
System.out.println("The result is: " + result);
}
}
throw statement
• When a user enters a wrong login-ID (or) password, the throw
statement is used to throw an exception.
• The throw statement takes a single statement, which is an
object of the ‘Exception class’.
• Syntax
throw Throwableinstance;
* Example
throw ThrowObject;
• The compiler gives an error if the object ‘ThrowObject”
doesn’t belong to a valid ‘Exception class’.
• The ‘throw statement is commonly used in ‘programmer-
defined’ exceptions.
throws statement
• Throws statement is used to specify that ‘ an exception has to be
handled by the calling method. It’s used for an exception that a
method is capable of raising, but not handling.
• Syntax
[<access_specifier>][<modifier>]<return_type>
<method_name> (<arg_list>) [throws <exception_list>]
• Example
public void acceptpassword() throws IllegalAccessException
{
System.out.println(“ intruder !!”);
throw new IllegalAccessException;
}
Finally Block
• The finally block is used when it’s necessary to process certain
statements no matter, whether an exception is raised or not.
• Example
try
{
openFile();
writeFile(); // may cause an exception
}
catch(……..)
{
// process the exception
}
Finally
{ closeFile();
}
Common exceptions
1. ArithmeticException Exception
2. NullPointerException Exception
3. ArrayIndexOutofBoundsException Exception
8. ACCESS MODIFIERS
• An access specifier determines which features of a class (the
class itself, the data members, and the method) may be used
by the other classes.
• Java supports three specifiers (or) Access Modifiers
1. public
2. private
3. Protected
4. Default
1. Public access specifier
A class, data member (or) method that has a public access
specifier can be accessed by any object. An inner class can’t
have a public access specifier.
• Syntax
<access_specifier> <return_type> <method> ([argment list])
{ ----------
}
Example:
public class employee
{
string empname;
string empadd;
public void displaydetails( )
{
// place the code for displaying employee details
}
}
2. Private access specifier
• Only object of the same class can access a variable or a
method that has a private access specifier. The variables and
method of the inner classes can be declared as private.
• Example:
private int privateVariable;
Package p1
Class X Package p2
Class Z
Class Y
* A method accesme() has been declared in class X. The
following table shows the accessibility of the method
accessme() from class Y and Z.
RequestTicket ConfirmedTicket
Syntax: