CoreJava Notes
CoreJava Notes
IACSD
1
PG-DAC Core Java Notes IACSD, Akurdi
Index
2
PG-DAC Core Java Notes IACSD, Akurdi
Features of Java
Object Oriented
Everything in Java is coded using OO principles. This facilitates code modularization, reusability,
testability, and performance.
Interpreted/Portable
Java source is compiled into platform-independent bytecode, which is then interpreted (compiled
into native-code) at runtime. Java code is "Write Once, Run Everywhere"
Simple
Java has a familiar syntax, automatic memory management, exception handling, single inheritance,
standardized documentation, and a very rich set of libraries .
Secure/Robust
Due to its support for strong type checking, exception handling, and memory management, Java is
immune to buffer- overruns, leaked memory, illegal data access. Additionally, Java comes with a Security
Manager too.
Scalable
Java is scalable both in terms of performance/throughput, and as a development environment. A
single user can play a Java game on a mobile phone, or millions of users can shop though a Java-based e-
commerce enterprise application.
High-performance/Multi-threaded
With its Just-in-Time compiler, Java can achieve (or exceed) performance of native applications. Java
supports multi-threaded development out-of-the-box.
Dynamic
Java can load application components at run-time even if it knows nothing about them. Each class
has a run-time representation.
Distributed
Java comes with support for networking, as well as for invoking methods on remote (distributed)
objects through RMI.
About JVM,JRE,JDK
Java Development Kit [JDK] is the core component of Java Environment and provides all the tools,
executable and binaries required to compile, debug and execute a Java Program. JDK is a platform specific
software and that’s why we have separate installers for Windows, Mac and Unix systems.
Java Virtual Machine[JVM] is the heart of java programming language. When we run a program, JVM
is responsible to converting Byte code to the machine specific code. JVM is also platform dependent and
provides core java functions like memory management, garbage collection, security etc.
Java Runtime Environment [JRE] is the implementation of JVM, it provides platform to execute java
programs. JRE consists of JVM and java binaries and other class libraries to execute any program successfully.
To execute any java program, JRE is required.
JVM architecture with journey of java program from source code to execution stage
As shown in the below architecture diagram, JVM subsystems are :
Class Loader Subsystem
Runtime Data Area
Execution Engine
3
PG-DAC Core Java Notes IACSD, Akurdi
Class Loader Subsystem
Loading : Class loader dynamically loads java classes. It loads, links and initializes the class file when it refers
to a class for the first time at runtime, not compile time. Class Loaders follow Delegation Hierarchy Algorithm
while loading the class files. 3 types are,
1. Boot Strap Class Loader – Responsible for loading classes from the bootstrap classpath, nothing but
rt.jar. Highest priority will be given to this loader.
2. Extension Class Loader – Responsible for loading classes which are inside the ext folder (jre\lib).
3. Application Class Loader –Responsible for loading Application Level Class path, path mentioned
Environment Variable etc.
4
PG-DAC Core Java Notes IACSD, Akurdi
Heap Area – All the Objects and their corresponding instance variables and arrays will be stored here.
There is also one Heap Area per JVM. Since the Method and Heap areas share memory for multiple
threads, the data stored is not thread-safe.
Stack Area – For every thread, a separate runtime stack will be created. For every method call, one
entry will be made in the stack memory which is called as Stack Frame. All local variables will be
created in the stack memory. The stack area is thread-safe since it is not a shared resource. The Stack
Frame is divided into three sub entities:
1. Local Variable Array – Related to the method how many local variables are involved and the
corresponding values will be stored here.
2. Operand stack – If any intermediate operation is required to perform, operand stack acts as
runtime workspace to perform the operation.
3. Frame data – All symbols corresponding to the method is stored here. In the case of any
exception, the catch block information will be maintained in the frame data.
PC Registers – Each thread will have separate PC Registers, to hold the address of current executing
instruction once the instruction is executed the PC register will be updated with the next instruction.
Native Method stacks – Native Method Stack holds native method information. For every thread, a
separate native method stack will be created.
Execution Engine
The byte code which is assigned to the Runtime Data Area will be executed by the Execution Engine. The
Execution Engine reads the byte code and executes it piece by piece.
Interpreter – The interpreter interprets the byte code faster, but executes slowly. The disadvantage
of the interpreter is that when one method is called multiple times, every time a new interpretation
is required.
JIT Compiler – The JIT Compiler neutralizes the disadvantage of the interpreter. The Execution Engine
will be using the help of the interpreter in converting byte code, but when it finds repeated code it
uses the JIT compiler, which compiles the entire bytecode and changes it to native code. This native
code will be used directly for repeated method calls, which improve the performance of the system.
Intermediate Code generator – Produces intermediate code
Code Optimizer – Responsible for optimizing the intermediate code generated above
Target Code Generator – Responsible for Generating Machine Code or Native Code
Profiler – A special component, responsible for finding hotspots, i.e. whether the method is called
multiple times or not.
Java Native Interface (JNI): JNI will be interacting with the Native Method Libraries and provides the Native
Libraries required for the Execution Engine.
Native Method Libraries: This is a collection of the Native Libraries which is required for the Execution Engine.
Bytecode
Bytecode is in a compiled Java programming language [by javac command] format and has the .class
extension executed by Java Virtual Machine (JVM). The Java bytecode gets processed by the Java virtual
5
PG-DAC Core Java Notes IACSD, Akurdi
machine (JVM) instead of the processor. The JVM transforms program code into readable machine language
for the CPU because platforms utilize different code interpretation techniques. A JVM converts bytecode for
platform interoperability, but bytecode is not platform-specific. JVM is responsible for processing & running
the bytecode.
JIT
The magic of java "Write once, run everywhere" is bytecode. JIT improves the performance of Java
applications by compiling bytecode to native machine code at run time. JIT is activated when a Java method
is called. The JIT compiler compiles the bytecode of that method into native machine code, compiling it "just
in time" to run. When a method has been compiled, the JVM calls the compiled code of that method directly
instead of interpreting it.
Typical compilers take source code and completely convert it into machine code, JITs take the same
source code and convert it into an intermediary “assembly language,” which can then be pulled from when
it’s needed. And that’s the key. Assembly code is interpreted into machine code on call—resulting in a faster
translation of only the code that you need. JIT have access to dynamic runtime information and are able to
optimize code. JITs monitor and optimize while they run by finding code more often called to make them run
better in the future.
JITs reduce the CPU’s workload by not compiling everything all at once, but also because the resulting
compiled code is optimized for that particular CPU. It’s why languages with JIT compilers are able to be so
“portable” and run on any platform or OS.
Platform independence
Java is a platform independent programming language, because your source code can be executed
on any platform [e.g. Windows, Mac or Linux etc..]. When you install JDK software on your system , JVM is
automatically installed on your system. When we compile Java code then .class file or bytecode is generated
by javac compiler. For every operating system separate JVM is available which is capable to read the .class
file or byte code and execute it by converting to native code for that specific machine. We compile code once
and run everywhere.
Language Fundamentals
Sample Java Program
6
PG-DAC Core Java Notes IACSD, Akurdi
7
PG-DAC Core Java Notes IACSD, Akurdi
Variables
Variable is nothing but identification of memory location.
Types of variables
Local
Variables declared inside method are local variable they have scope only within that methods.
Instance
Instance variables are declared inside class but used outside method.
Static
Static variable are same as that of instance variable but having keyword static.
They are also called as class variable because they are specified and can be access either class name
or object name.
8
PG-DAC Core Java Notes IACSD, Akurdi
9
PG-DAC Core Java Notes IACSD, Akurdi
Constructors
Constructors are like special methods that are called implicitly as soon as an object is instantiated
(i.e. on new ClassName()).
Constructors have no return type (not even void).
The constructor name must match the class name.
If you don’t define an explicit constructor, Java assumes a default constructor
The default constructor accepts no arguments.
The default constructor automatically invokes its base class constructor with no arguments,
as discussed later in this module.
You can provide one or more explicit constructors to:
Simplify object initialization (one line of code to create and initialize the object)
Enforce the state of objects (require parameters in the constructor)
Invoke the base class constructor with arguments, as discussed later in this module.
Adding any explicit constructor disables the implicit (no argument) constructor.
As with methods, constructors can be overloaded.
Each constructor must have a unique signature.
The parameter type list must be different, either different number or different order.
Only parameter types determine the signature, not parameter names.
One constructor can invoke another by invoking this(param1, param2, …) as the first line of its
implementation.
10
PG-DAC Core Java Notes IACSD, Akurdi
Inheritance
Inheritance allows you to define a class based on the definition of another class.
The class it inherits from is called a base class or a parent class.
The derived class is called a subclass or child class.
Subject to any access modifiers, which we’ll discuss later, the subclass gets access to the fields and
methods defined by the base class.
The subclass can add its own set of fields and methods to the set it inherits from its parent.
Inheritance simplifies modeling of real-world hierarchies through generalization of common
features.
Common features and functionality is implemented in the base classes, facilitating code
reuse.
Subclasses can extended, specialize, and override base class functionality.
Inheritance provides a means to create specializations of existing classes. This is referred to as sub-
typing. Each subclass provides specialized state and/or behavior in addition to the state and behavior
inherited by the parent class. For example, a manager is also an employee but it has a responsibility
over a department, whereas a generic employee does not.
Inheritance in Java
You define a subclass in Java using the extends keyword followed by the base class name. For
example:
class Manager extends Employee
{
// mgr class members
}
In Java, a class can extend at most one base class. That is, multiple inheritance is not
supported.
If you don’t explicitly extend a base class, the class inherits from Java’s Object class, discussed
later in this module.
Java supports multiple levels of inheritance.
For example, Child can extend Parent, which in turn extends Grandparent, and so on.
11
PG-DAC Core Java Notes IACSD, Akurdi
Why is polymorphism useful? It allows us to create more generalized programs that can be extended
more easily.
12
PG-DAC Core Java Notes IACSD, Akurdi
Interfaces
Interfaces are used for Role Based Inheritance & Programming by Contract. When we want a
common role/ behavior to be implemented by multiple classes in different hierarchies we use
interfaces.
Also for creating a contract as the class that implements an interface must implement all the
methods declared in the interface. In the Java programming language, an interface is a reference
type, similar to a class, which can contain only constants, method signatures, default methods, static
methods, and nested types.
Method bodies exist only for default methods and static methods. Interfaces cannot be
instantiated— they can only be implemented by classes or extended by other interfaces.
An interface defines a set of methods, without actually defining their implementation.
A class can then implement the interface, providing actual definitions for the interface
methods.
In essence, an interface serves as a “contract” defining a set of capabilities through method
signatures and return types.
By implementing the interface, a class “advertises” that it provides the functionality required
by the interface, and agrees to follow that contract for interaction.
Defining a Java Interface
Use the interface keyword to define an interface in Java.
The interface definition consists of public abstract method declarations. For example:
public interface Shape {
double getArea();
double getPerimeter();
}
All methods declared by an interface are implicitly public abstract methods.
If required any data member must be declared and initialized by the interface are also public static
&final.
Implementing a Java Interface
You define a class that implements a Java interface using the implements keyword followed by the
interface name. For example:
class Circle implements Shape { // ... }
A concrete class must then provide implementations for all methods declared by the interface.
Omitting any method declared by the interface, or not following the same method signatures
and return types, results in a compilation error.
An abstract class can omit implementing some or all of the methods required by an interface.
In that case concrete subclasses of that base class must implement the methods.
A Java class can implement as many interfaces as needed.
Simply provide a comma-separated list of interface names following
the implements keyword. For example:
class ColorCircle implements Shape, Color { // ... }
13
PG-DAC Core Java Notes IACSD, Akurdi
A Java class can extend a base class and implement one or more interfaces. In the
declaration, provide the extends keyword and the base class name, followed by
the implements keywords and the interface name(s).
14
PG-DAC Core Java Notes IACSD, Akurdi
Exception Handling
Exceptions are events that occur during the execution of programs that disrupt the normal flow of
instructions (e.g. divide by zero, array access out of bound, etc.).
In Java, an exception is an object that wraps an error event that occurred within a method and
contains:
Information about the error including its type
The state of the program when the error occurred
Optionally, other custom information
Exception objects can be thrown and caught.
Exceptions are used to indicate many different types of error conditions.
JVM Errors:
OutOfMemoryError
StackOverflowError
LinkageError
All exceptions and errors extend from a common java.lang.Throwable parent class. Only Throwable
objects can be thrown and caught.
Exception Hierarchy
15
PG-DAC Core Java Notes IACSD, Akurdi
Handling Exceptions
The try-catch-finally structure is used to handle exceptions.
try {
// Code bock
}
catch (ExceptionType1 e1) {
// Handle ExceptionType1 exceptions
}
catch (ExceptionType2 e2) {
// Handle ExceptionType2 exceptions
}
// ...
finally {
// Code always executed after the
// try and any catch block
}
Both catch and finally blocks are optional, but at least one must follow a try.
The try-catch-finally structure can be nested in try, catch, or finally blocks.
The finally block is used to clean up resources, particularly in the context of I/O.
If you omit the catch block, the finally block is executed before the exception is propagated.
Exceptions can be caught at any level.
If they are not caught, they are said to propagate to the next method.
Custom or User Defined Exception
If you wish to define your own exception:
Create an Exception class.
Decide if the exception should be checked or unchecked.
Checked extends Exception
Unchecked extends RuntimeException
Define constructor(s) that call into super’s constructor(s), taking message and/or cause
parameters.
16
PG-DAC Core Java Notes IACSD, Akurdi
17
PG-DAC Core Java Notes IACSD, Akurdi
18
PG-DAC Core Java Notes IACSD, Akurdi
Collection Hierarchy
19
PG-DAC Core Java Notes IACSD, Akurdi
20
PG-DAC Core Java Notes IACSD, Akurdi
Threading in Java
When you want application to provide the most responsive interaction with the user, even if the
application is currently doing other work we use multithreading.
Using multiple threads of execution is one of the most powerful ways to keep your application
responsive to the user and at the same time make use of the processor in between or even during
user events.
21
PG-DAC Core Java Notes IACSD, Akurdi
One Major disadvantage of Java synchronized keyword is that it doesn't allow concurrent read, which
can potentially limit scalability.
One more limitation of java synchronized keyword is that it can only be used to control access to a
shared object within the same JVM. If you have more than one JVM and need to synchronize access
to a shared file system or database, the Java synchronized keyword is not at all sufficient. You need
to implement a kind of global lock for that.
Java synchronized keyword incurs a performance cost. A synchronized method in Java is very slow
and can degrade performance. So use synchronization in java when it absolutely requires and
consider using java synchronized block for synchronizing critical section only.
Java synchronized block is better than java synchronized method in Java because by using
synchronized block you can only lock critical section of code and avoid locking the whole method
which can possibly degrade performance.
Deadlock
In Java, a deadlock is a situation where minimum two threads are holding the lock on some different
resource, and both are waiting for other’s resource to complete its task. And, none is able to leave the lock
on the resource it is holding.
In given figure Thread-1 has A but need B to complete processing and similarly Thread-2 has resource
Bbut need A first.
The Object class in Java has three final methods that allow threads to communicate about the locked
status of a resource.
wait() : It tells the calling thread to give up the lock and go to sleep until some other thread enters
the same monitor and calls notify(). The wait() method releases the lock prior to waiting and
reacquires the lock prior to returning from the wait() method
22
PG-DAC Core Java Notes IACSD, Akurdi
notify(): It wakes up one single thread that called wait() on the same object. It should be noted that
calling notify() does not actually give up a lock on a resource. It tells a waiting thread that that thread
can wake up. However, the lock is not actually given up until the notifier’s synchronized block has
completed.
notifyAll(): It wakes up all the threads that called wait() on the same object. The highest priority
thread will run first in most of the situation, though not guaranteed. Other things are same as
notify()method above.
23
PG-DAC Core Java Notes IACSD, Akurdi
Java Database Connectivity API (JDBC)
JDBC Overview
Standard for database-independent connectivity between Java applications and wide range of
relational databases (RDBMS)
JDBC Drivers
Type One
JDBC-ODBC Bridge plus ODBC driver
Depends on support for ODBC
Not portable
Type Two
JNI/Native Java driver
Requires DB native library
Not portable
Type Three
Pure Java to Middleware driver
Depends on Middleware server
Driver is portable, but the middleware server might not be
Type Four
Pure Java Driver
Talks directly to the RDBMS
Portable
24
PG-DAC Core Java Notes IACSD, Akurdi
Processing ResultSet
A table of data representing database result in response to a executeQuery()
Maintains a cursor pointing to the current row of data
Call boolean next() advance the cursor
Can be made scrollable and/or updatable
Provides getter methods for reading the current row (by column name or index)
Type sensitive: getString, getInt, getDate
Using ResultSetMetaData
Used to get information about the types and properties of the columns in a ResultSet object:
getColumnCount
getColumnDisplaySize
getColumnLabel, getColumnName
getColumnType, getColumnTypeName
25