Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Programming in Java

IVth SEMESTER
CIC-212

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Var-Arg Methods

1. Methods with variable number of arguments


2. Declaration is like method(int…x), and we can invoke this
method by the following call:
1. method()
2. method(10,20)
3. method(10)
4. method(10,20,30) and so on…
Example
Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
Var-Arg Methods
1. Internally var-arg method is implemented by using single
dimension arrays concept.
Example
Cases:
1. Mixing Var-Arg parameter with normal parameter, Example
2. If we are mixing var-arg parameter with general parameter
then var-arg parameter should be last.
3. There can be only one var-arg parameter
4. In General var-arg method will get last priority, if no other
method matched then only var-arg method will be called.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


For each() Loops
1. Most convenient loop to retrieve the elements of Arrays and
Collections.
2. Limitations:
1. Not a General Purpose Loop
2. Applicable only for Arrays and Collections
3. Using for each loop we should retrieve all values of Arrays
and Collections and can’t be used to retrieve a particular set
of values.

Example

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Packages

1. Packages are used in Java in order to prevent naming conflicts, to


control access, to make searching/locating and usage of classes,
interfaces, enumerations and annotations easier, etc. A Package
can be defined as a grouping of related types (classes, interfaces,
enumerations and annotations) providing access protection and
name space management.
2. Some of the existing packages in Java are:
1. java.lang - bundles the fundamental classes
2. java.io - classes for input , output functions are bundled in this
package

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Creating a Package
1. When creating a package, you should choose a name for the package
and put a package statement with that name at the top of every source
file that contains the classes, interfaces that you want to include in the
package.
2. The package statement should be the first line in the source file.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Importing a Package

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Cases in Package Import

1. Case 1: Trying to access a public variable outside the package.


2. Case 2: Trying to access a variable with default access modifier
outside the package
3. Case 3: Trying to access a variable with private access modifier
outside the package
4. Case 4: Trying to access a variable with protected access
modifier outside the package
Example(package), Class File

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Access Protection in Packages

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Exception Handling in Java

1. An exception is a problem that arises during the execution of a program. An


exception can occur for many different reasons, including the following:
1. A user has entered invalid data.
2. A file that needs to be opened cannot be found.
3. A network connection has been lost in the middle of communications, or
the JVM has run out of memory
“Exceptional Handling is a task to maintain normal flow of the
program. For this we should try to catch the exception object
thrown by the error condition and then display appropriate
message for taking corrective actions”
Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
Types of Exception

1. Checked Exception: A checked exception is an exception that is typically a


user error or a problem that cannot be foreseen by the programmer
2. Checked exception can also be defined as “The classes that extend the
Throwable class except RuntimeException and Error are known as Checked
Exceptions”.
3. For example, if a file is to be opened, but the file cannot be found, an
exception occurs. These exceptions are checked at compile-time and
cannot simply be ignored at the time of compilation.
4. Example of Checked Exception are IOException, SQLException etc

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Types of Exception

1. Unchecked Exception: Also known as Runtime Exceptions and they


are ignored at the time of compilation but checked during execution of the
program.
2. Unchecked Exceptions can also be defined as “The Classes that extend the
RuntimeException class are known as Unchecked Exceptions”.
3. Example are ArithmeticException, NullPointerException etc.
4. Error: These are not exceptions at all, but problems that arise beyond the
control of the user or the programmer.
1. Errors are typically ignored in your code because you can rarely do anything
about an error.
2. For example, if a stack overflow occurs, an error will arise. They are also
ignored at the time of compilation.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Hierarchy of Exception

All exception classes are


subtypes of the
java.lang.Exception class.
The exception class is a
subclass of the Throwable
class. Other than the
exception class there is
another subclass called
Error which is derived from
the Throwable class.
Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
Handling Exceptions in Java

Following five keywords are used to handle an exception in


Java:
1. Try
2. Catch
3. Finally
4. Throw
5. Throws

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Try-catch Block

1. A method catches an exception using a combination of the try and catch


keywords. A try/catch block is placed around the code that might generate
an exception. Code within a try/catch block is referred to as protected
code, and the syntax for using try/catch looks like the following:

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Example Without Exception Handling

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Example With Exception Handling

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Multiple Catch Blocks

Example
In the example, two catch statement are used but first one is
of type Exception which is a superclass of
ArithmeticException (used in second catch). So any
exception thrown will be caught by first catch block which
makes second block unreachable and error is shown during
compile time.
Note: Order of Catch should be from child to parent class

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Finally Block

1. The finally keyword is used to create a block of code that follows a


try block. A finally block of code always executes, whether or not an
exception has occurred. Using a finally block allows you to run any
cleanup-type statements that you want to execute, no matter what
happens in the protected code.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Difference b/w final, finally & finalize

Final Finally Finalize


Final is used to apply Finally is used to place Finalize is used to
restrictions on class, important code, it will be perform clean up
method and variable. executed whether processing just before
Final class can't be exception is handled or object is garbage
inherited, final method not collected
can't be overridden and
final variable value can't
be changed.
Final is a keyword Finally is a block Finalize is a method
Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
Throw Keyword

1. The throw keyword is used to explicitly throw an exception.


We can throw either checked or unchecked exception.
2. The throw keyword is normally used to throw custom
exception.
3. For eg: throw new ArithmeticException(“/by Zero”); this
statement creates new Exception Object and hand over it to
the JVM manually.
4. Example

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Throws Keyword

1. The throws keyword is used to declare the exception, it


provide information to the programmer that there may occur
an exception so during call of that method, and programmer
must use exceptional handling mechanism.
2. Throws keyword is also used to propagate checked
exception.
3. Throws Keyword is used to delegate the responsibility of
Exception Handling to the Caller Method.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Creating Custom Exception

You can create your own exceptions in Java. Keep the following
points in mind when writing your own exception classes:
1. All exceptions must be a child of Throwable.
2. If you want to write a checked exception that is automatically
enforced by the Handle or Declare Rule, you need to extend the
Exception class.
3. If you want to write a runtime exception, you need to extend the
RuntimeException class.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Applet Programming

1. An applet is a Java program that runs in a Web browser.


Important Points regarding Applet are the following:
1. An applet is a Java class that extends the java.applet.Applet
class
2. A main() method is not invoked on an applet, and an applet
class will not define main().
3. Applets are designed to be embedded within an HTML page
4. When a user views an HTML page that contains an applet,
the code for the applet is downloaded to the user's
machine.
Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
Life Cycle of Applet

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Methods in Applet Life Cycle

1. init() Method: This method is intended for whatever initialization is needed for
your applet. It is called after the param tags inside the applet tag have been
processed.
2. start() method: This method is automatically called after the browser calls the
init() method. It is also called whenever the user returns to the page containing
the applet after having gone off to other pages.
3. stop() method: This method is automatically called when the user moves off the
page on which the applet is created. It can, therefore, be called repeatedly in the
same applet.
4. destroy() method: This method is only called when the browser shuts down
normally.
5. paint() method: Invoked immediately after the start() method, and also any time
the applet needs to repaint itself in the browser. The paint() method is actually
inherited from the java.awt.
Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
Example to create Applet in Java

Steps:
1. Create a Java file containing Applet Code and
Methods described in the previous slide.
2. Create a HTML file and embed the .Class File of
the Java file created in the first step
3. Run Applet using either of the following methods
Open the HTML file in java enabled web browser
Use AppletViewer tool(used only for testing
purpose)
Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
Passing Parameter to Applet

1. <PARAM..> tag is used to pass the parameter value


from HTML file to Applet code.
Getting Input from User

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi

You might also like