Beruflich Dokumente
Kultur Dokumente
Simple
According to Sun, Java language is simple because:
Syntax is based on C++ (so easier for programmers to learn it after C+
+).
Removed many confusing and/or rarely-used features e.g., explicit
pointers, operator overloading etc.
No need to remove unreferenced objects because there is Automatic
Garbage Collection in java.
Object-oriented
Object-oriented means we organize our software as a combination of
different types of objects that incorporates both data and behavior.
Object-oriented programming (OOPs) is a methodology that simplify
software development and maintenance by providing some rules.
Platform Independent
A platform is the hardware or software environment in which a program
runs. There are two types of platforms software-based and hardwarebased. Java provides software-based platform. The Java platform differs
from most other platforms in the sense that it's a software-based
platform that runs on top of other hardware-based platforms. It has two
components:
Runtime Environment
API(Application Programming Interface)
java is platform independent Java code can be run on multiple
platforms e.g.Windows, Linux, Sun Solaris, Mac/OS etc. Java code is
compiled by the compiler and converted into bytecode. This bytecode
is a platform independent code because it can be run on multiple
platforms i.e. Write Once and Run Anywhere (WORA).
Secured
Java is secured because:
No explicit pointer
Programs run inside virtual machine sandbox.
how java is secured
how java is secured
Classloader- adds security by separating the package for the classes of
the local file system from those that are imported from network
sources.
Bytecode Verifier- checks the code fragments for illegal code that can
violate access right to objects.
Security Manager- determines what resources a class can access such
as reading and writing to the local disk.
These security are provided by java language. Some security can also
be provided by application developer through SSL, JAAS, cryptography
etc.
Robust
Robust simply means strong. Java uses strong memory management.
There are lack of pointers that avoids security problem. There is
automatic garbage collection in java. There is exception handling and
type checking mechanism in java. All these points makes java robust.
Architecture-neutral
There is no implementation dependent features e.g. size of primitive
types is set.
Portable
We may carry the java bytecode to any platform.
High-performance
Java is faster than traditional interpretation since byte code is "close"
to native code still somewhat slower than a compiled language (e.g.,
C++)
Distributed
We can create distributed applications in java. RMI and EJB are used for
creating distributed applications. We may access files by calling the
methods from any machine on the internet.
Multi-threaded
A thread is like a separate program, executing concurrently. We can
write Java programs that deal with many tasks at once by defining
multiple threads. The main advantage of multi-threading is that it
shares the same memory. Threads are important for multi-media, Web
applications etc.
*Object
An entity that has state and behavior is known as an object e.g. chair,
bike, marker, pen, table, car etc. It can be physical or logical.
Pen is an object. Its name is Reynolds, color is white etc. known as its
state. It is used to write, so writing is its behavior.
Object is an instance of a class. Class is a template or blueprint from
which objects are created.
Object is a physical entity.
*Class
*Constructor
Constructor in java is a special type of method that is used to initialize
the object.
Java constructor is invoked at the time of object creation. It constructs
the values i.e. provides data for the object that is why it is known as
constructor.
There are basically two rules defined for the constructor.
Constructor name must be same as its class name
Constructor must have no explicit return type
There are two types of constructors:
Default constructor (no-arg constructor)
Parameterized constructor
*Encapsulation
Encapsulation in java is a process of wrapping code and data together
into a single unit.
for example capsule i.e. mixed of several medicines.
We can create a fully encapsulated class in java by making all the data
members of the class private. Now we can use setter and getter
methods to set and get the data in it.
The Java Bean class is the example of fully encapsulated class.
By providing only setter or getter method, you can make the class
read-only or write-only.
It provides you the control over the data. Suppose you want to set the
value of id i.e. greater than 100 only, you can write the logic inside the
setter method.
*Inheritance (is-a relationship)
Inheritance in java is a mechanism in which one object acquires all the
properties and behaviors of parent object.
Inheritance represents the IS-A relationship, also known as parent-child
relationship.
In the terminology of Java, a class that is inherited is called a super
class. Entertainment
The new class is called a subclass.
Extend keyword is used for inheritance.
}
}
int result=obj.sum(20,20); //Here how can java determine which sum()
method should be called.
*Method Overriding
If subclass (child class) has the same method as declared in the parent
class, it is known as method overriding in java.
If subclass (child class) has the same method as declared in the parent
class, it is known as method overriding in java.
Method overriding is used for runtime polymorphism.
Rules for Java Method Overriding:
1. method must have same name as in the parent class
2. method must have same parameter as in the parent class.
3. must be IS-A relationship (inheritance).
*Upcasting
When reference variable of Parent class refers to the object of Child
class, it is known as upcasting.
class A{}
class B extends A{}
A a=new B();//upcasting
*Downcasting
*Polymorphism
When one task is performed by different ways i.e. known as
polymorphism.
In java, we use method overloading and method overriding to achieve
polymorphism.
Polymorphism is derived from 2 greek words: poly and morphs. The
word "poly" means many and "morphs" means forms. So
polymorphism means many forms.
There are two types of polymorphism in java: compile time
polymorphism and runtime polymorphism.
Example :
to convense the customer differently, to draw something e.g. shape or
rectangle
*Compiletime Polymorphism ( static Polymorphism)
The process in which a call to the overloaded function resolved at
comppile time is konwn as Compiletime ploymorphism.
for example sending sms, you just type the text and send the
message. You don't know the internal processing about the message
delivery.
There are two ways to achieve abstraction in java
1. Abstract class (0 to 100%)
2. Interface (100%)
*Abstract Class
A class that is declared with abstract keyword, is known as abstract
class in java.
It can have abstract and non-abstract methods (method with body).
It needs to be extended and its method implemented.
It cannot be instantiated. means we cant write new A();
extend keyword is used for inheritance.
*Abstract Method
A method that is declared as abstract and does not have
implementation is known as abstract method.
*Interface
An interface in java is a blueprint of a class.
It has static constants and abstract methods only.
The interface in java is a mechanism to achieve fully abstraction.
It is used to achieve fully abstraction and multiple inheritance in Java.
Java Interface also represents IS-A relationship.
implements keyword is used for inheritance.
In other words, Interface fields are public, static and final bydefault,
and methods are public and abstract.
*Marke or Tagged Interface
An interface that have no member is known as marker or tagged
interface. For example: Serializable, Cloneable, Remote etc.
*Multiple inheritance is not supported through class in java but it is
possible by interface, why?
Because java uses the concept of string literal. Suppose there are 5
* StringTokenizer
The java.util.StringTokenizer class allows you to break a string into
tokens. It is simple way to break string.
Ex 1.
StringTokenizer st = new StringTokenizer("my name is khan"," ");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
Ex 2.
StringTokenizer st = new StringTokenizer("my,name,is,khan");
// printing next token
System.out.println("Next token is : " + st.nextToken(","));
* Java Regex
The Java Regex or Regular Expression is an API to define pattern for
searching or manipulating strings.
Java Regex API provides 1 interface and 3 classes in java.util.regex
package.
1.
2.
3.
4.
MatchResult interface
Matcher class
Pattern class
PatternSyntaxException class
* Exception
The exception handling in java is one of the powerful mechanism to
handle the runtime errors so that normal flow of the application can be
maintained.
Exception is an abnormal condition.
In java, exception is an event that disrupts the normal flow of the
program. It is an object which is thrown at runtime.
The core advantage of exception handling is to maintain the normal
flow of the application. Exception normally disrupts the normal flow of
the application that is why we use exception handling.
Types of Exception
a) There are mainly two types of exceptions: checked and
unchecked where error is considered as unchecked exception.
The sun microsystem says there are three types of exceptions:
1) Checked Exception
2) Unchecked Exception
3) Error
Checked Exception
a)The classes that extend Throwable class except RuntimeException
and Error are known as checked exceptions
b)e.g.IOException, SQLException etc. Checked exceptions are checked
at compile-time.
Uncheked Exception
1) The classes that extend RuntimeException are known as
unchecked exceptions
2) e.g. ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException etc.
3) Unchecked exceptions are not checked at compile-time rather
they are checked at runtime.
Error
a) Error is irrecoverable
b) e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
No. Final
finally
finalize
1
)
Finally is used to
place important code,
it will be executed
whether exception is
handled or not.
Finalize is used to
perform clean up
processing just before
object is garbage
collected.
2
)
Final is a keyword.
Finally is a block.
Finalize is a method.
* Multithreading In Java
Thread
a) A thread is a lightweight sub process, a smallest unit of
processing.
b) It is a separate path of execution.
c) Threads are independent, if there occurs exception in one
thread, it doesn't affect other threads.
d) It shares a common memory area.
e) Life cycle of a Thread
1) The life cycle of the thread in java is controlled by
JVM.
2) The java thread states are as follows:
New
The thread is in new state if you create an instance of
Thread class but before the invocation of start() method.
Runnable
The thread is in runnable state after invocation of start()
method, but the thread scheduler has not selected it to be
the running thread.
Running
The thread is in running state if the thread scheduler has
selected it
Non-Runnable (Blocked)
This is the state when the thread is still alive, but is
currently not eligible to run.
Terminated
A thread is in terminated or dead state when its run()
method exits.
f) How to create thread
There are two ways to create a thread:
1) By extending Thread class
2) By implementing Runnable interface.
A.
B.
C.
D.
E.
F.
G.
H.
I.
J.
K.
L.
M.
N.
O.
* Deadlock
Deadlock in java is a part of multithreading. Deadlock can occur in a
situation when a thread is waiting for an object lock, that is acquired by
another thread and second thread is waiting for an object lock that is
acquired by first thread. Since, both threads are waiting for each other
to release the lock, the condition is called deadlock.
3) notifyAll() method
Wakes up all threads that are waiting on this object's monitor.
Syntax:
public final void notifyAll()
* Interrupting a Thread:
If any thread is in sleeping or waiting state (i.e. sleep() or wait() is
invoked), calling the interrupt() method on the thread, breaks out the
sleeping or waiting state throwing InterruptedException.
If the thread is not in the sleeping or waiting state, calling the
interrupt() method performs normal behaviour and doesn't interrupt
the thread but sets the interrupt flag to true.
In this example, after interrupting the thread, we are propagating it, so
it will stop working. If we don't want to stop the thread, we can handle
it where sleep() or wait() method is invoked.
In this example, after interrupting the thread, we handle the exception
(using try catch block), so it will break out the sleeping but will not stop
working.
* Difference between wait and sleep?
Let's see the important differences between wait and sleep methods.
wait()
sleep()
should be notified by
notify() or notifyAll()
methods
* Garbage Collection
In java, garbage means unreferenced objects.
Garbage Collection is process of reclaiming the runtime unused
memory automatically. In other words, it is a way to destroy the
unused objects.
To do so, we were using free() function in C language and delete() in
C++. But, in java it is performed automatically. So, java provides better
memory management.
Advantage of Garbage Collection
1. It makes java memory efficient because garbage collector removes the
unreferenced objects from heap memory.
2. It is automatically done by the garbage collector(a part of JVM) so we
don't need to make extra efforts.
How can an object be unreferenced?
There are many ways: