MC25106 Advanced Java Programming
Unit 1
Core Java Fundamentals: Java Virtual Machine, data types, variables, keywords, operators,
expressions, control statements, classes, objects, constructors, access control, method
overloading, static members, Arrays, Strings, Inheritance: types, constructors in inheritance,
method overriding, use of super, abstract classes – interfaces, dynamic method dispatch.
Packages and exception handling
History of Java:
The history of java starts from Green Team. Java team members (also known as Green Team),
initiated a revolutionary task to develop a language for digital devices such as set-top boxes,
televisions etc. For the green team members, it was an advance concept at that time. But, it was
suited for internet programming. Later, Java technology as incorporated by Netscape. Currently, Java
is used in internet programming, mobile devices, games, e-business solutions etc. There are given
the major points that describes the history of java. 1) James Gosling, Mike Sheridan, and Patrick
Naughton initiated the Java language project in June 1991. The small team of sun engineers called
Green Team. 2) Originally designed for small, embedded systems in electronic appliances like set-
topboxes. 3) Firstly, it was called "Greentalk" by James Gosling and file extension was.gt. 4) After
that, it was called Oak and was developed as a part of the Green project.
Java Version History
There are many java versions that has been released. Current stable release of Java is
Java SE 8. 1. JDK Alpha and Beta (1995) 2. JDK 1.0 (23rd Jan, 1996) 3. JDK 1.1 (19th Feb, 1997) 4. J2SE
1.2 (8th Dec, 1998) 5. J2SE 1.3 (8th May, 2000) 6. J2SE 1.4 (6th Feb, 2002) 7. J2SE 5.0 (30th Sep,2004)
8. Java SE 6 (11th Dec,2006) 9. Java SE 7 (28th July, 2011) 10.Java SE 8 (18th March,2014)
Features of Java
There is given many features of java. They are also known as java buzzwords. The Java Features
given below are simple and easy to understand.
1. Simple
2. Object-Oriented
3. Portable
1 Department of MCA
MC25106 Advanced Java Programming
4. Platform independent
5. Secured
6. Robust
7. Architecture neutral
8. Dynamic
9. Interpreted
10. High Performance
11. Multithreaded
12. Distributed
Java Virtual Machine (JVM)
Definition: JVM is an abstract computing machine that enables Java bytecode to be
executed on any platform without modification.
Role:
o Converts Java bytecode into machine code.
o Provides platform independence.
o Manages memory through Garbage Collection.
o Ensures security and performance optimization.
Architecture:
1. Class Loader – loads classes.
2. Bytecode Verifier – checks code for illegal code.
3. Interpreter – executes instructions.
4. Just-In-Time Compiler (JIT) – optimizes performance by compiling
bytecode to native code.
5. Garbage Collector – automatically frees memory used by objects that are no
longer referenced.
Key Features of JVM
✔ Platform Independence – “Write once, run anywhere.”
✔ Memory Management – Automatic garbage collection.
2 Department of MCA
MC25106 Advanced Java Programming
✔ Security – Bytecode verification and sandboxing.
✔ Performance Optimization – JIT compilation and efficient runtime execution.
✔ Multithreading Support – Multiple threads executed via separate stacks.
1. Java Application (.java)
The source code written by the programmer.
Contains classes, methods, variables, etc.
2. Java Compiler (javac)
Compiles the source code into bytecode stored in .class files.
Syntax and semantic checking happen here.
3 Department of MCA
MC25106 Advanced Java Programming
3. Java Bytecode (.class)
Intermediate, platform-independent code.
Contains instructions understood by the JVM.
4. Class Loader Subsystem
Loads .class files into memory.
Searches for classes in directories, JAR files, etc.
Supports dynamic loading during runtime.
5. Bytecode Verifier
Ensures that the code adheres to Java’s safety and access rules.
Prevents malicious or corrupt code from being executed.
6. Runtime Data Area
Memory areas used by JVM during execution:
Method Area: Stores class-level data like fields, methods, and runtime constant pool.
Heap: Stores objects and arrays; garbage-collected memory area.
Java Stack: Manages method calls and local variables.
Program Counter (PC) Registers: Keeps track of the current instruction for each
thread.
Native Method Stack: Handles calls to native libraries written in other languages like
C.
7. Execution Engine
Executes bytecode instructions.
Contains an Interpreter for running instructions one by one.
JIT Compiler: Improves performance by converting frequently used code into native
machine code.
8. Native Operating System
JVM relies on OS resources such as file systems, memory, and networking.
Native methods interface with OS-level libraries.
Data Types in Java
4 Department of MCA
MC25106 Advanced Java Programming
Data types represent the different values to be stored in the variable. In java, there are two types of
data types:
o Primitive datatypes
o Non-primitive datatypes
Primitive Data Types:
Data Type Size Description
byte 8 bits Stores small integers
short 16 bits Stores larger integers
int 32 bits Default integer type
long 64 bits Stores very large integers
float 32 bits Stores floating-point numbers
double 64 bits Stores high precision floating-point numbers
char 16 bits Stores single characters
boolean 1 bit Stores true or false
Non-primitive Data Types: Classes, Arrays, Interfaces.
Example: Add Two Numbers
classSimple{
public static void main(String[] args){
inta=10;
int b=10;
int c=a+b;
System.out.println(c);
}}
Output:20
Variables
Variable is a name of memory location. There are three types of variables in
java
Local Variables: Declared inside methods, constructors, or blocks.
Instance VariablesVariable is a name of memory location. There are three types of
variables in java
Static Variables: A variable that is declared as static is called static variable. It
cannot be local..
5 Department of MCA
MC25106 Advanced Java Programming
classA{
int data=50;//instance variable
static int m=100;//static variable
void method(){
int n=90;//local variable
} }//end of class
Keywords
Reserved words with special meaning. Examples: class, public, static, void, if,
else, for, while, return, this, super, new.
6. Operators
Operator in java is a symbol that is used to perform operations.
Arithmetic: +, -, *, /, %
Relational: ==, !=, >, <, >=, <=
Logical: &&, ||, !
Assignment: =, +=, -=, *=, /=
Unary: ++, --, -, +
Bitwise: &, |, ^, ~, <<, >>
Expressions
Expressions are essential building blocks of any Java program, usually created to produce a new
value, although sometimes an expression simply assigns a value to a variable. Expressions are built
using values, variables, operators and method calls.
Combinations of operators and operands.
Types of Expressions
While an expression frequently produces a result, it doesn't always. There are three types of
expressions in Java:
• Those that produce a value, i.e. the result of (1 + 1)
6 Department of MCA
MC25106 Advanced Java Programming
• Those that assign a variable, for example (v =10)
• Those that have no result but might have a "side effect" because an expression can include a wide
range of elements such as method invocations or increment operators that modify the state (i.e.
memory) of a program.
Example:
int a = 10;
int b = 20;
int c = a + b * 2;
Control Statements
Decision-making:
o if, if-else, if-else if-else, switch
Looping:
o for, while, do-while
Branching:
o break, continue, return
int age = 20;
if(age >= 18) {
System.out.println("Eligible to vote.");
} else {
System.out.println("Not eligible.");
Classes and Objects
Class: Blueprint for objects.
Object: Instance of a class.
class Car {
String color;
int speed;
7 Department of MCA
MC25106 Advanced Java Programming
class Test {
public static void main(String[] args) {
Car myCar = new Car();
myCar.color = "Red";
myCar.speed = 100;
Constructors
Special methods used to initialize objects.
No return type.
Overloading is allowed.
Example:
class Car {
String model;
Car(String model) {
this.model = model;
Access Control
Access Modifiers:
o private: Accessible only within the class.
o default: Accessible within the package.
o protected: Accessible within the package and subclasses.
o public: Accessible everywhere.
11. Method Overloading
8 Department of MCA
MC25106 Advanced Java Programming
If a class has multiple methods having same name but different in parameters,
it is known as Method Overloading. If we have to perform only one operation,
having same name of the methods increases the readability of the program.
Defining multiple methods with the same name but different parameters.
Example:
class MathOperations {
int add(int a, int b) {
return a + b;
double add(double a, double b) {
return a + b;
12. Static Members
Belongs to the class rather than an instance.
Shared by all objects.
Example:
class Counter {
static int count = 0;
Counter() {
count++;
13. Arrays
Collection of similar data types.
Single-dimensional and multi-dimensional arrays.
Example
9 Department of MCA
MC25106 Advanced Java Programming
int[] numbers = {1, 2, 3, 4, 5};
for(int n : numbers) {
System.out.println(n);
Strings
Immutable objects representing sequences of characters.
Useful methods: length(), substring(), charAt(), equals(), toLowerCase(),
split().
Example:
String str = "Hello Java";
System.out.println(str.length());
System.out.println(str.toUpperCase());
Inheritance
Mechanism by which one class acquires properties of another.
Types:
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
Example:
class Animal {
void eat() {
System.out.println("Eating...");
class Dog extends Animal {
void bark() {
10 Department of MCA
MC25106 Advanced Java Programming
System.out.println("Barking...");
Constructors in Inheritance
Child class can invoke parent class constructor using super().
Example:
class Animal {
Animal() {
System.out.println("Animal constructor");
class Dog extends Animal {
Dog() {
super();
System.out.println("Dog constructor");
Method Overriding
Subclass redefines a method of the superclass with the same signature.
Example:
class Animal {
void sound() {
System.out.println("Animal sound");
11 Department of MCA
MC25106 Advanced Java Programming
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
Use of super
Refers to superclass members and constructors.
Example:
class Animal {
String name = "Animal";
class Dog extends Animal {
String name = "Dog";
void display() {
System.out.println(super.name); // Accessing superclass field
Abstract Classes and Interfaces
Abstract Class: Cannot be instantiated; may have both abstract and concrete
methods.
Interface: Only method declarations (Java 8 onwards, default and static methods
allowed).
Example Abstract Class:
abstract class Animal {
abstract void sound();
12 Department of MCA
MC25106 Advanced Java Programming
interface Vehicle {
void drive();
Dynamic Method Dispatch
Runtime polymorphism where the method to be called is determined at runtime.
Example:
class Animal {
void sound() {
System.out.println("Animal sound");
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
class Test {
public static void main(String[] args) {
Animal a = new Dog();
a.sound(); // Calls Dog's method
Packages
Used to group related classes and interfaces.
13 Department of MCA
MC25106 Advanced Java Programming
Built-in packages: java.lang, java.util, java.io.
Example:
import java.util.ArrayList;
22. Exception Handling
Mechanism to handle runtime errors.
Keywords: try, catch, finally, throw, throws.
try {
int divide = 10 / 0;
} catch(ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("This block always executes");
14 Department of MCA