Chapter 2: An Overview of Java -
Detailed Notes
Introduction
Java’s features work together; understanding one often requires knowing others.
This chapter gives an overview of OOP, sample programs, control statements, blocks, lexical
issues, data types, and arrays.
Object-Oriented Programming (OOP)
Two paradigms: Process-Oriented (code acts on data) vs Object-Oriented (objects control
access to code).
OOP provides abstraction, encapsulation, inheritance, and polymorphism.
Advantages: better organization, easier debugging, reusability.
First Simple Program
Program:
class Example {
public static void main(String args[]) {
System.out.println("Java drives the Web.");
}
}
Explanation: Defines a class, main() is entry point, System.out.println prints text.
Output: Java drives the Web.
Second Short Program
Program:
class Example2 {
public static void main(String args[]) {
int num; // declare variable
num = 100; // assign value
System.out.println("This is num: " + num);
num = num * 2;
System.out.print("The value of num * 2 is ");
System.out.println(num);
}
}
Explanation: Demonstrates variables, assignment, and string concatenation.
Output:
This is num: 100
The value of num * 2 is 200
If Statement
Program:
class IfSample {
public static void main(String args[]) {
int x, y;
x = 10;
y = 20;
if(x < y) System.out.println("x is less than y");
x = x * 2;
if(x == y) System.out.println("x now equal to y");
x = x * 2;
if(x > y) System.out.println("x now greater than y");
if(x == y) System.out.println("you won’t see this");
}
}
Explanation: Demonstrates relational operators and if conditions.
Output:
x is less than y
x now equal to y
x now greater than y
For Loop
Program:
class ForTest {
public static void main(String args[]) {
int x;
for(x = 0; x < 10; x = x + 1)
System.out.println("This is x: " + x);
}
}
Explanation: Shows initialization, condition, iteration in for loop.
Output: Displays numbers from 0 to 9.
Using Blocks of Code
Program:
class BlockTest {
public static void main(String args[]) {
int x, y;
y = 20;
for(x = 0; x < 10; x++) {
System.out.println("This is x: " + x);
System.out.println("This is y: " + y);
y = y - 2;
}
}
}
Explanation: Demonstrates block of code using { }, shows decrement of y inside loop.
Lexical Issues
Case-sensitive language (Hello ≠ hello).
Identifiers: must start with letter, _, or $, cannot be a keyword.
Whitespace ignored except as separator.
Comments: // single-line, /* multi-line */, /** documentation */.
Java Class Libraries
Java’s strength lies in its extensive libraries.
Organized into packages such as java.lang, java.util, java.io.
Provide prebuilt classes and methods for reuse.
Data Types, Variables, and Arrays
Strongly typed language: each variable must have a type.
Primitive types: byte, short, int, long, float, double, char, boolean.
Type casting: implicit widening and explicit narrowing.
Arrays: declared with int nums[] = new int[10];, indexed from 0.
Summary
Chapter introduces basics of Java: classes, methods, variables, loops, if statements, and
arrays.
Explains OOP principles, case-sensitivity, strong typing, and role of libraries.