You are on page 1of 54

Software is a collection of programms

installed on a machine to carry out user


specified taks.
JAVA is one of the OO programming language
(software)available.
Sofwares can be classified in 3 categories
System Software Fridge, Owen, Scanner,Printer
Application S/w Banking, Healthcare, Education Sector
Internet S/wyahoo.com

1
Front End Technologies
Platform Independent
Exe:awt, APPALETS, SWING
Platform Dependent
Exe: Developer 2000, Visual Basics.
Basically called as GUI Development.
Back End Technology
This involves interaction beween GUI and DB.
The data is stored in DB, and applications are
developed with respect retrievin, chnaging and
saving the data in DBs.

2
StandAlone application
Distributed or Web Based Application
JAVA can be used to develop both type of
Applications.
JAV A was developed by Sun Microsystems
which merged in Oracle Systems.
J2SE Standard Edition Stand Alone Apps
J2EEEnterprise Edition Server Side Apps
J2ME Mobile EditionWireless Apps

3
JAVA COMPILER

First.java First.class

First.class is a file developed by JAVA compiler which


is aslo called as Bytecode.
This Bytecode is understood by JVM.
This JVM after reading Bytecode converts it to a OS
understandable Instructions, which then carries out
the task.

4
API is a collection of Packages.
Packages is a collection of classes, Interfaces, Sub
Packages.
JAVA in turn contains many API.
API stands for Application Programming
Interfaces.
There are 2 memory locations
Stack Memory (Main () Method)
Heap Memory
All objects are stored in the heap, and methods on
staack.
String is a Object.
Variables are reference to the object.
5
Access Modifier

1. Final Key word can also be applied to method and class apart from variables. The
same functionality prevails everywhere.
2. A Method is a part or a sub program of a main class.
3. Methods are defined in same class or separate classes.

Access Modifies applies for Member,


Methods and Classes.

6
If you want to write a method in the same
program only, then you need to simply define the
method and call.
If you want to write the method outside the
main. Then you need to define the method in the
class.
You can create method in method.
STAIC word should be applied to a method
definition if you are accessing a method without
creating object.
Ore else you have to create the object and then
use it.

7
JAVA dosent support multi return values.
Different method to be applied.l
Overloading Same method name but different
signature.
Overriding: Same method name, body
different.(in sub Classes)
The process of binding data and associated
methods is called as class.
Class is a logical entity.
Object is a physical entity.
8
STATIC method is at class level.
Non Static Method is at Instance/ Object
Level.
A method in JAVA not only return
fundamental type, but also class name.
We cannot have two public (classes) in a single
files.
All static block will be loaded at the time of
loading the classes.

9
STATIC-A NON ACCESS MODIFIER
IT CAN BE APPLIED TO CLASS, METHODS, DATA
VARIABLE AND BLOCKS.
FOR A CLASS IT CAN BE APPLIED ONLY TO A
NESTED CLASS (NOT TO A TOP LEVEL CLASS).
In other words it can be applied to inheritted
class or class in class.
The nested static class can be accessed without
having an object of outer class.

10
Class can have only public and default access.
The public class needs to be in same name java file.
A single java file can contain more then one non public
class but can have only one public class.
A public class can be seen by all classes from all packages
A class with default access can be seen only by classes within
the same package.
A Java file with no public class have no naming restriction.
The Class can also have final, abstract and strictfp non access
modifiers.
An Abstract Class can not be instantiated.
A Final Class can not be subclassed.
A Class can not be both final and abstract.
Class visibility can be seen in 3 parameter
If a Class can extend another Class?
If a Class can create instance of another Class?
If a Class can access methods and variable of another Class?
11
CLASS
Class is a template for creating objects which
defines its state and behavior.
A class contains field(data
variables) and method to define
the state and behavior of its object.

12
VARIABLES
YOU CANNOT DECLARE A VARIABLE AS STATIC
IN MAIN METHOD.
When you write in curly brackets the the
scope is only till those brackets.

13
METHOD DEFINATION
YOC CANNOT DEFINE A METHOD IN MAIN METHOD.
YOU CANNOT DECLARE ANY METHOD IN ANY
METHOD.
You cannot declare a static variable in method, as it will
be a local variable. The scope is limited to method only.
Again Method defination can be of two types static and
instance.
The staic method can be called directly with method
name.
The Instantenous method has to called by creating a
mothod.

14
STACK M1()

Main()

HEAP

STATIC

J=20

15
INTERFACES
All the methods are interfaces.
Methods need to be implemented in child
classes.
Child class can be normal/abstract class.
You need to override all the methods declared in
interfaces.
Interfaces is saved as separate class.
The data members in the interfaces should be
static and final.

16
ABSTRACT CLASS AND METHODS
These classes cannot be instantiated.
If any method in a class is abstract then the
class has to be abstract class.
Abstract class can contain abstract and non
abstract class.
The abstract method has no body.
The child class has to implement the abstract
method.

17
Excception Handling
Exception handling is a task to maintain normal flow.
It is used to avoid abnormal termination.
Example
Wrong input from the user.
File not found.
For every thread JVM creates a runtime stack.
First in last out.
Whenever exception occurs, JVM throws a exception error
which is in the form of name of exception , description of
exception and location of exception (Stack Trace Name).
Once exception occurs the flow will not proceed further.

18
Exception HandlingContd
An unwanted/unexpected event that causes
termination of normal flow of program is the
exception.
Graceful Termination is Exception Handling.
Exception are recoverable and errors are non
recoverable.
Exe: File not found , Ram not sufficient.
Exception are of two types
Check Exception
Non Check Exception.

19
Exception Handling Keywords
There are 5 keywords provided in JAVA to handle exception.
Try
Catch
Finally
Throw
Throws
Within try block if anywhere exception occurs then the rest
code will not be executed, even though the exception is
handled.
Try catch block.
Try block contains the probable code.
Catch block contains the message of the error.

20
There are 3 methods to print the message.
Printstacktrace().
Prints the name of the exception:Description:stack
Trace.
tostring().
Prints the name of the exception:Description
getmessage().
Prints the Description

21
We can have n number of catch block.
Try block as one (I guess)
There is somehting called as not reachable
code.
There are two classes exception and
ArithimeticException.
AE class is a subclass and E class is a super class.
The order shoud be from sub class to super class.

22
Finally Block
Finally block will always execute, even if
exceptions are handled properly/not
properly/no exception.
Check what is system.exit(0);.

23
THROWABLE CLASS
THROWABLE ACT AS A ROOT FOR JAVA
EXCEPTION HIRECHECY.
IT HAS TWO CHILD CLASSES
EXCEPTION CLASS (WHICH ARE RECOVERABLE)
ERROR CLASS (NON RECOVERABLE)
There are many child classes for exception.
EXCEPTION IS A CHILD CLASS IN THROWABLE
CLASS.

24
Exception class
IO Exception
File not found exception

Servlet Exception

Runtime Exception
Arithmetic Exception
Null Poniter Exception

Index Out of Box Exception


Array IOBE
String IOBW

25
Checked Excep/Unchecked Excep
Checked Exception are the exception which
are checked by compiler for smooth execution
of program.
In the program if there is a chance of rising
checked exception, then we should handle
that by using throws or try catch block
otherwise we will get compiletime error.
The exception which are not checked by
compiler are called as unchecked exception.

26
Arithimatic Exception is unchecked exception.
File not found is checked exception.
All Errors are unchecked exceptions.
Runtime Exception and its child classes, Error and its child classes
are all uncheked Exceptions.
Rest of all are checked exceptions.
There is also something called as fully checked and partially checked
Exceptions.
A checked exception is said to be fully checked if all its child classes
are also checked.exe IO Exception.
A checked exception is said to be partially checked if some of its
child classes are not fully checked. Exe: Exception and Throwable.
The only possibble partially checked classes in java are throwable
and exceptions.

27
IOExceptionchecked (Fully)
RuntimeExceptionUnchecked
InterruptedExceptionsChecked(fully)
Errorunchecked
ThrowableChecked (Partially)
Arithmetic ExceptionUnchecked
NullpointerException Unchecked
ExceptionChecked (Partially)
FileNotFoundExceptionchecked (Fully)
28
CUSTOMIZE EXCEPTION HANDLING
RISKY CODE Should be in try block.
Handling Code should be in catch block.
Class test{
Public static void main (String[] args){
System.out.println(Statement-1);
Try {syso(10/0);}
Catch{syso(10/5);}
Syso(statement-3);
}
}

29
Control flow in try catch
If exception rises apart from try block then it is
a abnormal termination.
With in try block if any exception rises rest of
the statements will not execute.

30
Methods to print exception messages

printStackTrace(). complete Information


Tostring(). name and description
Getmessage();only description.
Try with multiple catch blocks is recommended.
Order of catch block is important when we have
multiple catch blocks.
We have to child and then parent, otherwise we
will get complile time error.

31
Error class

VMEerror
Stack Overflow Error
Out of Memory Error

32
Finally & Finalize
It is a block which will execute even if the
exception is raised or not or whether handled or
not handled.
It is always associated with try catch block.
Try block contains risky code.
Catch block Exception handling code.
Finally cleanup code (it is related to try block).
Finalize is a method invoked by garbage collector
just before destroying the object, to perform
cleanup activities (cleanup activities related to
object).
33
Only Catch statement is invalid.
Only Finally Statement is invalid.
Try and then finally valid.
Only try is also invalid.
In try catch finally..order is important.
When try is there, it should be with either finally or
catch.
Catch without try is invalid.
Finally without try is invalid.
Nesting of try catch finally is allowed.
For T , C & F blocks curly blocks are mandatory.

34
THROW
Programmer throws the exception object, JVM
catches the object
Manually we can do the same thing by using
throw keyword.
Throw new ArithmeticException(hjghjghj).
Throw to handover our created exception
object to the JVM.
In Implicit case main method is responsible to
create exception object and handover to JVM.
In explicit case programmer is creating exception
object explicitly and handing over to JVM using
throw key word.

35
Throw is used mainly for
customized/userdefined exception.
After throw we cannot write other statements
directly, there will be compiletime error saying
ureachable code.
We can throw only throwable type.

36
THROWS
If there is checked exception, then we need to either
have a try catch block or throws statement .
The error would be unreported exception XXX, must
be caught or declared to be thrown.
The error can be handles by using two ways
By using try catch.
By using throws.
Throws keyword is used to delegate the responsibility
of exception handling to the caller.
It may be another method or JVM.
Caller Method is responsible to handle that wxception.

37
Throws..
It is required only for checked exception.
Usage of throws keyword doesn't prevent
abnormal termination.
It is recommended to use try catch block.
It is required only to convince compiler.
There is no impact of throws keyword for
unchecked exception.
It can be used only @ constructors , and Methods
but not for class.

38
Keywords summary
Try to maintain risky code.
Catchto maintain handling code.
Finallyclean up code.
Throwto handover our created exception
object to the JVM explicitly (Manually).
Throwsdelegate the responsibilty of
exception handling to the caller method.

39
Errors Summary
Unreported exception XXX; must be caught or declared
to be thrown
Exception XXX already been caught
Exception XXX is never thrown in body of
corresponding try statement
Unreachable statement
Incompatible types found :Test, required
JAVA.lang.throwable
Try without catch or finally
Catch without try
Finally without try

40
MULTI THREADING
Here a program is divided inti two or more
subprograms(Process) which can be
implemented at the same time in parallel.
if there are two methtods m1() and m2(), and
if we want to execute the both methods
simultaneously then we need to use threading
concept.
Multitasking is divided in two categories (1)
Process Based and Thread Based.

41
Example watching movie and chating on
facebook, this is called process based.
Text Editor, you are editing text and also
printing.
It is Heavy weight it is light weight
Own memory address spaace Address space is shared
Interprocss is expensive Inter Thead is cheap

42
Benefits of Multithreading
Helps in doing multiple tasks.
Speed of the programs increase, say there are
10 methods..if there are no threads all the
methods need to be executed one after
another, where as with threads we can
execute concurrently.

43
Life cycle of thead
New Born
Start()
stop()

stop()
Running Runnable Dead

Yield()

sleep() resume() stop()


Suspend() notify()
Wait()

Blocked
44
To go to runnable or running state there newborn thread
uses start() method.
Runnin means thread is running
Runnabble means thead is ready to run but waiting for
availibility for the process.
Yield() running thread becomes runnable.
Suspend(),sleep(),wait() makes running or runnable thread
to block state.
Resume(), notify() from block to runnable.
Stop ()method gets killed.
Active Thread ->if It is either running or runnable.
Idle thread or not runnale means it is in block state.

45
SYNCHRONIZATION
It is only applicable for blocks and methods.
It cannot be applied for classes and variables.
If t1 and t2 two threads and access the same
method, there might be a possibility of data
inconsistency.
example bank balance 1000 and two withdraw
methods.
T.getname is the method that gives the method.
Just use synchronized.

46
THREAD PRIORITY
T1 and t2 two threads, they run normally if we
dont specify the priority.
Priority comes as MIN_PRIORITY
MAX PRIORITY
NORM_PRIRITY.

47
THREADS
BY USING implements Runnable.
BY USING extends Thread.
Process based is best suitable at OS Level.
Thread based is JAVA Language related.
One program contains different independent
tasks comes under threads.
To develop web servers we use threads like gmail
server which is accessed by many users at the
same time.

48
Start()new thread is created.
Run()normal run mehtod executed no
separate thread is created.

49
JOIN METHOD
Public static native void yield();
Public final void join()
Public final void join (long ms)
Public final void join (long ms, int ns)
Every join method will throw inturrupted
exception, so it should be jandled by throws
error, otherwise we will get CTE.

50
synchronize
Class level lock
Object level lock
if two threads wants to use a single method
then there is a posibility of data inconsistancy
So we use synchronize
If we write static synchronize then it is class
level lock.

51
SYNCHRONIZED BLOCK

52
JAVA I/O PACKAGE
File is keyword used to create file object.
File f = new File(G:\\abc.txt);
File f = new File(G:\\abc); Creates a
directory.
Fiel f = new File(subDirc, String);

53
54

You might also like