Concepts of Programming Notes For PG-DAC
Concepts of Programming Notes For PG-DAC
IACSD
Concepts of Programming Notes
1
IACSD Concepts of Programming
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.
2
IACSD Concepts of Programming
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
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.
3
IACSD Concepts of Programming
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.
4
IACSD Concepts of Programming
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
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.
5
IACSD Concepts of Programming
Language Fundamentals
Sample Java Program
6
IACSD Concepts of Programming
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.
Operators in Java
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Operation (Remainder after division)
7
IACSD Concepts of Programming
Operator Meaning
+ Unary plus: not necessary to use since numbers
are positive without using it
- Unary minus: inverts the sign of an expression
++ Increment operator: increments value by 1
8
IACSD Concepts of Programming
Here,
If the Expression is true, expression1 is assigned to the variable.
If the Expression is false, expression2 is assigned to the variable.
Java Arrays
An array is a container object that holds a fixed number of values of a single type. The length of an
array is established when the array is created. After creation, its length is fixed. You have seen an
example of arrays already, in the main method of the "Hello World!" application. This section
discusses arrays in greater detail.
An array of 10 elements.
Each item in an array is called an element, and each element is accessed by its numerical index. As
shown in the preceding illustration, numbering begins with 0. The 9th element, for example, would
therefore be accessed at index 8.In Java, array is an object of a dynamically generated class. Java
array inherits the Object class.
9
IACSD Concepts of Programming
Copying Arrays
The System class has an arraycopy method that you can use to efficiently copy data from one array
into another:
public static void arraycopy(Object src, int srcPos,
Object dest, int destPos, int length)
The two Object arguments specify the array to copy from and the array to copy to. The
three int arguments specify the starting position in the source array, the starting position in the destination
array, and the number of array elements to copy.
10
IACSD Concepts of Programming
Classes in Java
Everything in Java is defined in a class.
In its simplest form, a class just defines a collection of data
e.g.
class Employee {
int empid;
String name;
double salary;
}
Objects in Java
To create an object (instance) of a particular class, use the new operator, followed by an invocation
of a constructor for that class, such as:
new Employee();
The constructor method initializes the state of the new object.
The new operator returns a reference to the newly created object.
As with primitives, the variable type must be compatible with the value type when using object
references, as in:
Employee e = new Employee();
To access member data or methods of an object, use the dot (.)
notation: variable.field or variable.method().
11
IACSD Concepts of Programming
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.
12
IACSD Concepts of Programming
Java Packages
A package is simply a container that groups related types (Java classes, interfaces, enumerations, and
annotations). For example, in core Java, the System class belongs to the java.lang package. The
package contains all the related types that are needed for the basic java development.
13
IACSD Concepts of Programming
Built-in Package
Built-in packages are existing java packages that come along with the JDK. For example, java.lang,
java.util, java.io
User-defined Package
Java also allows you to create packages as per your need. These packages are called user-defined
packages.
package packageName;
Java uses file system directories to store packages.
For example:
└── com
└── test
└── TestPlanet.java
Now, edit Test.java file, and at the beginning of the file, write the package statement as:
package com.test;
Here, any class that is declared within the test directory belongs to the com.test package.
Java has an import statement that allows you to import an entire package, or use only certain classes
and interfaces defined in the package.
-----------------------------------------------------------------------------------------------------------------------------------
14