Bitwise Learning Java Unit 1
Bitwise Learning Java Unit 1
I T S
E
OBJECT ORIENTED E
G
A R N
N I
T W I
PROGRAMMING WITH JAVA
I S
E
(BCS-403)
G
A R N
N I
Complete Notes
OOPS WITH JAVA (BCS-403)
UNIT 1 - SYLLABUS
E
Programming Structures in Java: Defining Classes in Java, Constructors, Methods, Access
Specifies, Static Members, Final Members, Comments, Data types, Variables, Operators,
Control Flow, Arrays & String.
Object Oriented Programming: Class, Object, Inheritance Super Class, Sub Class, Overriding,
L E
G
Overloading, Encapsulation, Polymorphism, Abstraction,
N Interfaces, and Abstract Class.
A R
N I
Packages: Defining Package, CLASSPATH Setting for Packages, Making JAR Files for
Library Packages, Import and Static Import Naming Convention For Packages
2
OOPS WITH JAVA (BCS-403)
Introduction
T Wof Java
History I S
I
The Green Team (1991): Java was conceived by James Gosling, Mike Sheridan, and Patrick
E
Naughton at Sun Microsystems. This team was known as the "Green Team".
Initial Project: They initiated a project to develop software for consumer electronic devices like
set-top boxes, televisions, and remote controls.
Language Choice: They initially thought of using C++, but found it too complex and
platform-dependent (memory management issues, lack of portability).
G
GreenTalk & .gt: Gosling developed a new language initially called "GreenTalk" with the file
E
extension .gt. A N
R N I
Oak: The language was renamed to "Oak" after an oak tree that stood outside James Gosling's
office. Oak is considered a symbol of strength and is a national tree of countries like the USA,
France, and Germany.
3
OOPS WITH JAVA (BCS-403)
Introduction
T W I S
I
Java (1995): "Oak" was already a registered trademark of Oak Technologies. The team had to
E
rename it. Suggestions included "Dynamic", "Revolutionary", "Silk", "Jolt", and "DNA".
The Name "Java": They settled on "Java", inspired by Java coffee from Indonesia. It reflects
the unique, dynamic, and lively nature of the language.
The Logo: The logo represents a steaming cup of coffee.
The Shift to the Web: The original goal (consumer electronics) failed because the market wasn't
G
ready. However, the internet was emerging. In 1995, Netscape incorporated Java technology into
E
A
its browser, making Java famous for "Applets" N
(interactive
I web programs).
R N
Oracle Acquisition (2010): Sun Microsystems was acquired by Oracle Corporation. Now, Java is
an Oracle product.
4
OOPS WITH JAVA (BCS-403)
Introduction
T WHistory
Version I
I S
JDK 1.0 (1996): First public release.
E
J2SE 1.2 (1998): Playground changed to "Java 2".
Java 8 (2014): A major release introducing functional programming features like Lambda
Expressions.
Java 9 (2017): Introduced modularity.
Current Release Cycle: Since Java 9, a new version is released every 6 months (March &
L
G
E
N
September). Long Term Support (LTS)Aversions (like Java 17, 21) are released every few years for
R N I
enterprise stability.
5
OOPS WITH JAVA (BCS-403)
Introduction
W I of Java)
T (Features
Why Java?
I S
Java's popularity stems from a set of features often referred to as "Java Buzzwords".
E
1. Platform Independent (Write Once, Run Anywhere - WORA):
Unlike C/C++, which compiles to machine-specific code (e.g., .exe for Windows), Java
compiles to Bytecode (.class file).
This Bytecode is not specific to any machine. It can run on any OS (Windows, Linux, Mac)
that has a JVM (Java Virtual Machine) installed. The JVM acts as a translator between
bytecode and the hardware.
L
G
E
2. Object-Oriented:
A I N
R N
Java is based on the Object Model. Everything in Java is an Object (except primitive data
types like int, char).
It strictly follows OOP principles: Encapsulation (binding code and data), Inheritance (code
reusability), Polymorphism (one name, many forms), and Abstraction.
6
OOPS WITH JAVA (BCS-403)
Introduction
W I of Java)
T (Features
Why Java?
I S
3. Simple:
E
Java is designed to be easy to learn.
It removed complex and confusing features of C++ such as explicit pointers (to prevent direct
memory access), operator overloading, and multiple inheritance (through classes) to avoid
ambiguity (Diamond Problem).
4. Robust (Strong Memory Management):
L
G
E
A
Garbage Collection: Java has an automatic GarbageN
Collector that runs in the background to
R N I
destroy unused objects, preventing memory leaks.
Exception Handling: It captures series errors and eliminates the risk of crashing the system.
Type Checking: It has strong compile-time and runtime type checking.
7
OOPS WITH JAVA (BCS-403)
Introduction
TW
Why Java? (Features of Java)
I S
5. Secure: I
No Pointers: Since there are no explicit pointers, programmers cannot access memory
E
locations directly, preventing unauthorized data access.
Sandbox: Java programs run inside a virtual machine sandbox, separating them from the host
OS.
Byte-code Verifier: Checks code fragments for illegal code that can violate access rights.
6. Multithreaded:
Java supports Multithreading at the language level. This allows writing programs that can
L
G
perform many tasks simultaneouslyE (e.g., downloading a file while playing music). It defines
A N
Thread class and Runnable interface. R N I
7. Distributed:
Java is designed for the distributed environment of the internet. It supports technologies like
RMI (Remote Method Invocation) and EJB (Enterprise JavaBeans) to create applications that
run on multiple networks. 8
OOPS WITH JAVA (BCS-403)
Introduction
W I of Java)
Why Java? (Features
T
8. Dynamic: I S
B
Java is more dynamic than C/C++. It supports dynamic loading of classes (loading classes on
E
demand at runtime) and dynamic compilation (JIT).
9. High Performance:
Although interpreted languages are typically slower, Java enables high performance using the JIT
(Just-In-Time) Compiler, which converts bytecode to native machine code on the fly for frequently
executed sections.
Uses of Java
L
G
E
Desktop applications A N
Web Application R N I
Enterprise application
Robotics
Games
Embedded Systems 9
OOPS WITH JAVA (BCS-403)
Introduction
Types of Java
W Application
T I S
I
1. Stand-alone Applications (Desktop Applications)
Also known as Desktop Applications or Window-based Applications, these are traditional software programs that
E
need to be installed on every machine where they are intended to run.
Definition: An application that runs independently on a computer without requiring a web browser.
Characteristics: They interact directly with the operating system and hardware.
Technologies Used: AWT (Abstract Window Toolkit) and Swing are typically used to create these applications.
Examples: Media players, antivirus software, and Acrobat Reader 1.
2. Web Applications
L
G
E and create dynamic web pages.
These are applications that run on the server side
A pages andI is N
Definition: An application that generates dynamic accessed via a web browser over a network (like
the Internet). R N
Characteristics: They reside on a server and communicate with the client.
Technologies Used: Servlet, JSP (JavaServer Pages), Struts, Spring, and Hibernate are used for creating web
applications.
Examples: Websites like [Link], [Link], and various e-commerce platforms 1-3. 10
OOPS WITH JAVA (BCS-403)
Introduction
W Application
Types of Java
T I
I S
3. Enterprise Applications
E
These applications are designed for a corporate environment and are distributed in nature.
Definition: Large-scale applications used to solve enterprise-level problems.
Characteristics: They possess features like high-level security, load balancing, and clustering. They often run on
a distributed system.
Technologies Used: EJB (Enterprise JavaBeans) is commonly used to create enterprise applications.
Examples: Banking applications 1, 2.
L
G
4. Mobile Applications E
A N
R N I
These are applications created specifically for mobile devices.
Definition: Software designed to run on smartphones and tablets.
Technologies Used: Android and Java ME (Micro Edition) are used to create these applications.
Examples: WhatsApp and other mobile apps 2.
11
OOPS WITH JAVA (BCS-403)
Introduction
Java EnvironmentW(JDK,
I JRE, JVM)
T S three components is critical for any
I
Understanding the distinction and relationship between these
B
Java developer.
E
1. JVM (Java Virtual Machine)
Definition: An abstract machine (virtual computer) that provides a runtime environment in which
Java Bytecode can be executed. It "exists" only inside the computer's memory.
Role: It acts as an interpreter between the Bytecode and the underlying Machine Code.
Platform Dependent: While Java Bytecode is platform-independent, the JVM is platform-
L
G
dependent. You install a Windows JVM E on Windows and a Linux JVM on Linux.
A N
Architecture of JVM: R N I
a. Class Loader Subsystem: Loads, links, and initializes class files (.class).
Bootstrap ClassLoader (Loads standard JDK classes like [Link].*).
Extension ClassLoader (Loads extensions from lib/ext).
System/Application ClassLoader (Loads user-defined classes from CLASSPATH). 12
OOPS WITH JAVA (BCS-403)
Introduction
T W
Java Environment I JRE, JVM)
(JDK,
S
Architecture of JVM:
I
E
b. Runtime Data Areas (Memory)::
Method Area: Stores class structures, static variables, code.
Heap: Where Objects are allocated.
Stack: Stores local variables and method call frames.
PC Register: Keeps track of the current instruction.
Native Method Stack: For non-Java code (C/C++).
L
G
E
c. Execution Engine: A I N
R N
Interpreter: Reads byte-code stream then executes the instructions.
JIT (Just-In-Time) Compiler: Improves performance by compiling sections of bytecode to
native code so they don't need to be interpreted every time.
Garbage Collector: Reclaims memory from unreferenced objects. 13
OOPS WITH JAVA (BCS-403)
Introduction
T W
Java Environment I SJRE, JVM)
(JDK,
Architecture of JVM:
I
E
L
G
E
A N
R N I
14
OOPS WITH JAVA (BCS-403)
Introduction
T W
Java Environment I SJRE, JVM)
(JDK,
Internal Structure of JVM:
I
E
L
G
E
A N
R N I
15
OOPS WITH JAVA (BCS-403)
Introduction
T W I S
Java Environment (JDK, JRE, JVM)
2. JRE (Java Runtime Environment)
I
E
Definition: It is the implementation of the JVM. It is a
software package that provides the class libraries and
other resources that a specific Java program needs to
run.
Who uses it?: End-users who only want to run Java
programs, not develop them.
L
G
Components: E
JVM: The engine to run code. A I N
R N
Library Classes: Pre-compiled code ([Link], [Link])
like [Link].*, [Link].*, [Link].*.
Supporting Files: Configuration files.
Equation: JRE = JVM + Library Classes ([Link])
16
OOPS WITH JAVA (BCS-403)
Introduction
Java Environment (JDK, JRE, JVM)
T W I S
3. JDK (Java Development Kit)
I
Definition: It is a full-featured software development kit for Java
B
developers.
E
Who uses it? Programmers who need to write, compile, and debug Java
code.
Components:
JRE: To run the programs you write.
Development Tools:
javac: The Compiler.
L
G
java: The Loader/Interpreter. E N
A
javadoc: For generating documentation.R N I
jar: For creating archives.
jdb: The Debugger.
Equation: JDK = JRE + Development Tools
Hierarchy Summary: JDK contains JRE. JRE contains JVM 17
OOPS WITH JAVA (BCS-403)
Introduction
T W I S
I
E
L
G
E
A N
R N I
18
OOPS WITH JAVA (BCS-403)
Introduction
Java SourceW
FileIStructure
T S
I
A Java source file (.java) follows a strict structure to ensure the compiler understands it.
E
Documentation Section (Optional): Comments (// or /* ... */) providing info about the code.
Package Statement (Optional): Must be the first executable line if present.
Syntax: package [Link];
It groups related classes together.
Import Statements (Optional): Used to bring other classes into visibility.
G
E
Syntax: import [Link];
A an interface.
I N
Interface Statement (Optional): If defining R N
Class Definition (Mandatory): A source file can contain multiple classes, but only one public class.
Rule: The source file name must match the public class name exactly.
Example: If public class Student { ... } exists, file must be [Link].
19
OOPS WITH JAVA (BCS-403)
Introduction
T W
Java Source FileIStructure
S
I
Main Method: The entry point.
E
public static void main(String[] args)
public: Accessible by JVM.
static: Called without object creation.
void: Returns no value.
String args: Command line arguments.2
L
G
E
A N
R N I
20
OOPS WITH JAVA (BCS-403)
Introduction
Compilation andW
Execution
I Process
T S
This is a two-step process: I
E
Step 1: Compilation
Tool: javac (Java Compiler).
Input: Source Code ([Link]).
Action: The compiler checks for syntax errors. If error-free, it translates source code into Byte-
code.
Output: Class File ([Link]).
L
G
Byte-code: A highly optimized set ofEinstructions designed
N to be executed by the Java Virtual
A
N I code.
Machine (JVM). It is not machine code; it isRintermediate
21
OOPS WITH JAVA (BCS-403)
Introduction
Compilation andW
Execution
I Process
T S
Step 2: Execution I
E
Tool: java (JVM Launcher).
Input: Bytecode ([Link]).
Action:
Class Loader: Loads the class into memory.
Byte-code Verifier: Checks the bytecode for security and validity.
Interpreter/JIT: Converts bytecode into Machine Code (0s and 1s) specific to the underlying
L
G
E
Operating System (Windows/Linux/Mac).
A N
Hardware: The CPU executes the machine N I
R code.
Visual Flow:Source Code (.java) -> Compiler (javac) -> Bytecode (.class) -> JVM (Interpreter/JIT) ->
Machine Code (Binary) -> OS/Hardware
22
OOPS WITH JAVA (BCS-403)
Introduction
Compilation andW
Execution Process
T I S
I
B
Code: First Java Program
E
L
G
E Compile: javac [Link]
A N Run: java HelloWorld
R N I
23
OOPS WITH JAVA (BCS-403)
T W
Data Types and IVariables
S
I
Java is statically typed (variables must be declared before use).
E
Primitive Data Types (8 Types):
Boolean: boolean (1 bit, true/false). Default: false.
Character: char (2 bytes, Unicode \u0000 to \uffff). Default: \u0000.
Integer:
byte (1 byte, -128 to 127).
Variables:
short (2 bytes).
Local: Inside methods/blocks. Must be
L
G
int (4 bytes). Default: 0.
E initialized before use. Stored in Stack.
long (8 bytes). Suffix L (e.g., 100L).A I N
Floating Point: R N Instance: Inside class, outside methods.
Belong to object. Stored in Heap.
float (4 bytes). Suffix f (e.g., 10.5f).
Initialized to default values.
double (8 bytes). Default for decimals.
Static: Declared with static. Belong to
Non-Primitive: String, Array, Class, Interface.
class. Stored in Method Area. 24
OOPS WITH JAVA (BCS-403)
T W I S
I
E
L
G
E
A N
R N I
25
OOPS WITH JAVA (BCS-403)
T W I
I Types & SVariables
Code: Data
E
L
G
E
A N
R N I
26
OOPS WITH JAVA (BCS-403)
T WDefinitions
Operators (Detailed I S & Code)
I
Shift:
E
<< (Left Shift): 10 << 2 (10 * 2^2 = 40).
>> (Signed Right Shift): 10 >> 2 (10 / 2^2 = 2). Preserves sign.
>>> (Unsigned Right Shift): Fills leading bits with 0.
Ternary: (condition) ? trueVal : falseVal.
Assignment: =, +=, -=, etc.
G
E
A N
R N I
27
OOPS WITH JAVA (BCS-403)
E
L
G
E
A N
R N I
28
OOPS WITH JAVA (BCS-403)
E
switch: Supports int, char, String, enum:
G
E
A N
R N I
29
OOPS WITH JAVA (BCS-403)
WStatements
Control Flow
T I S
Iteration (Loops). I
for loop: for(init; condition; incr/decr).
E
while loop: Entry controlled.
G
E
A N
R N I
do-while: Exit controlled (runs at least once).
for-each (Enhanced for): for(Type var : array).
30
OOPS WITH JAVA (BCS-403)
E
continue: Skips current iteration.
return: Exits method.
G
E
A N
R N I
31
OOPS WITH JAVA (BCS-403)
E
2D Array (Matrix): int[][] arr = new int;
Jagged Array: Different number of columns per row.
G
E
A N
R N I
32
OOPS WITH JAVA (BCS-403)
T W I S
I
E
L
G
E
A N
R N I
33
OOPS WITH JAVA (BCS-403)
E
StringBuffer/StringBuilder: Mutable. StringBuffer is synchronized (thread-safe), StringBuilder is
not.
G
E
A N
R N I
34
OOPS WITH JAVA (BCS-403)
Programming Structures in Java
E
Constructor: Initialize object. Same name as class. No return type.
Access Specifiers: private (class), default (package), protected (package + sub-class), public (global).
G
E
A N
R N I
35
OOPS WITH JAVA (BCS-403)
Object Oriented Programming
Inheritance
T W I Skeyword.
Definition: Acquiring properties of parent class. extends
I
Types in Java: Single, Multilevel, Hierarchical. (Multiple is not supported via class).
E
L
G
E
A N
R N I
36
OOPS WITH JAVA (BCS-403)
Object Oriented Programming
Types ofW I
Inheritance
T S
I
Supported in
Type Structure
E
Java
G
Hierarchical E A → B, A → C Yes
A N
R N I
Multiple A, B → C No
Hybrid Mixed No
37
OOPS WITH JAVA (BCS-403)
Object Oriented Programming
Polymorphism
T W I S parameters.
Compile-time (Overloading): Same method name, different
I
Run-time (Overriding): Same method name, same parameters, parent vs child class.
E
L
G
E
A N
R N I
38
OOPS WITH JAVA (BCS-403)
Object Oriented Programming
Encapsulation
T W
Abstract Class: abstract keyword. Can have abstract I
(no body) and concrete methods.
I S
Interface: interface keyword. 100% abstract (before Java 8). Variables are public static final. Methods
E
are public abstract.
G
E
A N
R N I
39
OOPS WITH JAVA (BCS-403)
Packages
E
User-defined: package mypack; at top of file.
G
E
A N
R N I
Compile & Run: javac [Link] -> java B
40
OOPS WITH JAVA (BCS-403)
Packages
E
Static Import
Access static members without class qualification
G
E
A N
R N I
41
OOPS WITH JAVA (BCS-403)
Packages
E
[Link]: For Input/Output operations.
[Link]: For networking support.
[Link]: For GUI (Abstract Window Toolkit)
G
E
A N
R N I
42
OOPS WITH JAVA (BCS-403)
Access Modifiers with Packages
E
Package) Package)
Private Yes No No No
G
E
Protected Yes A
Yes I NYes No
R N
43
OOPS WITH JAVA (BCS-403)
E
Name Must have the same name as the class name Can have any name
Return Type Does not have a return type, not even void Must have a return type
Invoked implicitly by the system when an object is Invoked explicitly by the programmer using method
Invocation
created using the new keyword calls
G
E
Inheritance A
Cannot be inherited by subclasses N Can be inherited by subclasses
R N I
If no constructor is defined, the Java compiler
Default The compiler does not provide any default method.
provides a default (no-arg) constructor
E
Can have abstract (no body) and concrete (body) Only abstract methods (before Java 8);
Methods
methods. Default/Static allowed (Java 8+).
Variables Can have final, non-final, static, non-static Variables are implicitly public static final.
Inheritance A class can extend only one abstract class. A class can implement multiple interfaces.
G
E
A N
Constructor Can have constructors R N I Cannot have constructors.
45
OOPS WITH JAVA (BCS-403)
Non-Static (Instance)
Feature Static Method
Method
T W I
Association I
Belongs to the class itself. S Belongs to a specific object (instance) of
the class.
E
No specific keyword (default for
Keyword Declared using the static keyword.
methods).
Memory is allocated only once when the class Memory is allocated each time an object
Memory is loaded. is created.
G
Access to Data
E
Can only access static data members and
other static methods directly. It cannot access
Can access both static and non-static
A N (instance) data members and methods.
R N I
non-static (instance) variables directly.
T W I S
Synchronization
I
It is synchronized (Thread-Safe) It is non-synchronized (Not Thread-Safe).
E
Two threads can't access the Two threads can call the methods
Thread Safety
methods simultaneously simultaneously.
G
E
Introduced In A N
Java 1.0 (Legacy) Java 1.5.
R N I
Use when thread safety is
Usage Use when thread safety is not required
required.
47
OOPS WITH JAVA (BCS-403)
Important Codes
T W I S
I
E
L
G
E
A N
R N I
48
OOPS WITH JAVA (BCS-403)
Important Codes
T W I S
I
E
L
G
E
A N
R N I
49
OOPS WITH JAVA (BCS-403)
Important Codes
T W I S
I
E
L
G
E
A N
R N I
50
OOPS WITH JAVA (BCS-403)
Important Codes
T W I S
I
E
L
G
E
A N
R N I
51
OOPS WITH JAVA (BCS-403)
Important Codes
T W I S
I
E
L
G
E
A N
R N I
52
OOPS WITH JAVA (BCS-403)
Important Codes
T W I S
I
E
L
G
E
A N
R N I
53
OOPS WITH JAVA (BCS-403)
Important Codes
T W I S
I
E
L
G
E
A N
R N I
54
OOPS WITH JAVA (BCS-403)
Important Codes
T W I S
I
E
L
G
E
A N
R N I
55
OOPS WITH JAVA (BCS-403)
Important Codes
T W I S
I
E
L
G
E
A N
R N I
56
OOPS WITH JAVA (BCS-403)
Important Codes
T W I S
I
E
L
G
E
A N
R N I
57
OOPS WITH JAVA (BCS-403)
Important Codes
T W I S
I
E
L
G
E
A N
R N I
58
OOPS WITH JAVA (BCS-403)
Important Codes
T W I S
I
E
L
G
E
A N
R N I
59
OOPS WITH JAVA (BCS-403)
Important Codes
T W I S
I
E
L
G
E
A N
R N I
60
OOPS WITH JAVA (BCS-403)
Important Codes
T W I S
I
E
L
G
E
A N
R N I
61
62