Chapter 1: Introduction to Object-Oriented Programming Concepts
Two categories of computer languages:
1. Low-Level Languages (not directly understood by the user)
2. High-Level Languages (not directly understood by the machine)
Two categories of Low-Level Languages:
1. Machine Language: Uses binary digits 0s and 1s.
Advantage: Directly understood by the machine as no translator is needed.
Disadvantage: It is machine dependent. Difficult to read and write for the user.
2. Assembly Language: Uses mnemonic codes (like LD for Load, ST for store).
Advantage: Comparatively easier to read and write for the user.
Disadvantage: It is machine dependent. A translator called assembler is needed to convert assembly language program into machine language program.
High-Level Languages are easier to read and write as it uses simple English-like keywords. Examples: BASIC, C, C++, Java, Python.
Advantage: It is machine independent. Easy to read and write.
Disadvantage: Needs a translator (compiler/interpreter) to convert from high-level language program to machine language program.
A compiler is a software that converts the entire source code (HLL program) into object code (machine language program) in one go. An interpreter is a software that converts the source code into a machine language program line by line.
A program written in HLL is called source code. A program in the form of machine language is called object code.
Types of High-Level Languages:
1. Structure-Oriented Programming Language: Uses a modular approach and consists of selection statements, loops, functions. Examples: ALGOL, PASCAL.
2. Procedure-Oriented Programming Language: Uses multiple functions, shares global data. It follows a top-down approach in problem solving. It puts more emphasis on functions. Examples: BASIC, COBOL, FORTRAN, C.
3. Object-Oriented Programming Language: Uses multiple objects to conquer a problem. Each object contains its own set of data and related functions. It uses a bottom-up approach in problem solving. It puts more emphasis on data. Example: C++, Java, Python, SmallTalk, Ruby, Eiffel.
An object is an instance of a class. It is a unique entity that includes data and related methods (functions). A class is a blueprint of an object. It acts as a data type for the objects.
Four Principles of OOP:
1. Abstraction: It is the act of representing essential features without highlighting the background details. Example: The Save button in MS Word saves the file while hiding the background details.
2. Inheritance: It is the act of inheriting properties and behavior from one class to another class. The class that inherits is called derived class or child class or subclass. The class from where the features get inherited is called parent class or superclass or base class. Inheritance allows code reusability.
3. Polymorphism: It is the process of showing different behaviors according to different situations. Example: function overloading.
4. Encapsulation: It is the process of wrapping of data and associated functions into a single unit.
Java is an object-oriented, platform-independent programming language. It was developed by James Gosling at Sun Microsystems in the year 1995. Java is both compiled and interpreted.
Bytecode is an intermediate code generated by compiling the Java source code. It is platform independent. JVM (Java Virtual Machine) is the interpreter that executes this bytecode.
A package in Java is a collection of related (similar) classes. The default package in Java is java.lang.
Keywords are the special words in Java that have a special meaning for the compiler. They are also known as reserved words. These keywords cannot be used as identifiers. Example: void, int, static, etc.
Two types of Java programs:
1. Java Application: It is a standalone program that doesn’t depend on any other application to execute.
2. Java Applet: It is a program that depends on a web browser to execute.
Comments are non-executable statements in Java. Semicolon is used to terminate a Java statement.
BlueJ is a freely available Java IDE (Integrated Development Environment). It is a window-based application. It can be downloaded from www.bluej.org. Internally, it used the JDK (Java Development Kit) to compile and execute Java programs.
Chapter 2: Elementary Concepts of Objects and Classes
The characteristics of a real-world object are referred to as data members, and the behavior is referred to as methods (functions) in a class or object. The state of an object is represented through the current values of its data members. The message passing (interacting) among objects is done through the methods.
Instantiation is the process of creating an object of a class.
Syntax: <class name> <object name> = new <class name>;
Example: Strudent s1 = new Student();
The new keyword is used to allocate space for the object being created.
A class acts as an object factory, because with one class, one can create multiple objects. A class is a user-defined data type because it specifies what operations can be performed on its objects. An object is an instance of a class, because each object is unique and has its own existence.
Chapter 3: Values and Data Types
Character Set is a collection of characters supported by any programming language. Java uses the Unicode character set. Unicode provides a unique number for every character, regardless of the platform, language or program. In Java, it uses 16 bits (2 bytes) to represent a character. It is backward compatible with the ASCII character set.
ASCII (American Standard Code for Information Interchange) is another character encoding scheme that uses 7 bits to represent a character. Following is the range of ASCII codes for the commonly used characters:
‘A’ to ‘Z’ (65 to 90)
‘a’ to ‘z’ (97 to 122)
‘0’ to ‘9’ (48 to 57)
Escape Sequences are special characters that help in representing characters that are not easy to type directly. They always start with a backslash.\t
means horizontal tab\n
means newline\"
means double-quotes\'
means single quotes\\
means backslash
Question: Give the output: System.out.println("\"Hello there!\"");
Answer: “Hello there!”
Tokens are the building blocks of a program. There are various types of tokens in Java:
1. Literals (Constants): Fixed or direct values in a program.
45 is an int literal
45L is a long literal
4.5 is a double literal
4.5F is a float literal
‘A’ is a char literal
true/false is a boolean literal
“Hello” is a String literal
2. Identifiers (Variables): Named storage location in a program.
Rules for naming variables or identifiers:
a) Must begin with an alphabet or a $ sign or an underscore sign.
b) Can include digits.
c) They are case-sensitive.
d) Must not be a keyword.
Two ways of initializing variables:
a) Static Initialization: It is the direct assignment of a constant and the variable is initialized during compile-time. Example: a = 5;
b) Dynamic Initialization: It is the initialization that takes place at runtime. Example: x = Math.sqrt(y);
3. Separators: To separate different parts of a program. Examples: semicolon, comma, etc.
4. Operators: Symbols that work with operands to give some meaningful result. Example: +, <, ++, &&, etc. The = is an assignment operator. It is used to assign a value to a variable.
5. Keywords: Special words in Java that have a special meaning for the compiler. Example: public, class, float, etc.
There are eight primitive data types in Java:
byte (1 byte)
short (2 bytes)
int (4 bytes) – default type for integers
long (8 bytes)
float (4 bytes)
double (8 bytes) – default type for floating-point numbers
char (2 bytes)
boolean (occupies 1 byte but uses 1 bit)
Types of arithmetic expressions:
1. Pure expression: all values are of the same data type. Example: 4 + 5
2. Impure expression: values of multiple data types are used. Example: 2.5 + 4
Type Conversion is the process of converting the data type of one value to another data type. Two types of type conversion:
a) Implicit type conversion (coercion): It occurs automatically, without user’s intervention.
Example: long num = 45;
b) Explicit type conversion: It is done forcibly by the user. Example: int num = (int)4.5;
Chapter 4: Operators in Java
An operator is a symbol that acts on operands to give some meaningful result. Operands are the values on which the operator works.
Arithmetic Operators allow us to perform arithmetic operations.
+ for addition
– for subtraction
* for multiplication
/ for division
% for modulo division (remainder)
Increment Operator (++) increases the value of a numeric variable by 1. Post-increment increases later, whereas pre-increment increases first.
Decrement Operator (++) decreases the value of a numeric variable by 1. Post-decrement decreases later, whereas pre-decrement increases first.
Relational Operators are used to compare two values. They always evaluate to boolean value. Types of relational operators:
< for less than
> for greater than
<= for less than or equal to
>= for greater than or equal to
== for equal to
!= for not equal to
Logical Operators are used to combine multiple conditions in a program. They always evaluate to boolean result and also work with boolean operands. Types of logical operators:
Logical AND (&&) evaluates to true if and only if both operands are true, otherwise false. It is binary in nature.
Logical OR (||) evaluates to true if at least one of the operands is true, otherwise false. It is binary in nature.
Logical NOT (!) converts a true operand to false and vice-versa. It is unary in nature.
Based on the number of operands, there are three types of operators:
1. Unary Operator works with only one operand. Example: +, -, ++, –, !, etc.
2. Binary Operator works with two operands. Example: +, -, *, >, <, etc.
3. Ternary Operator works with three operands. Example: ?:
A counter is a special numeric variable that counts the number of times any event or process takes place in a program.
An accumulator is a special variable that accumulates the result such as sum, product, etc.
Shorthand Operations are used to write certain assignment statements in short. Examples:x = x + 5;
can be written as x += 5;
y = y * 2;
can be written as y *= 2;
The conditional operator (symbol ?:) is an alternative to the if-else statement. It is also known as ternary operator because it works with three operands.
Example: int discount = (bill >= 5000)? 25 : 10;
Chapter 5: Input in Java
Three types of comments in Java:
1. Single-line comment: Starts with // and spans only one line and ends when ‘enter’ key is pressed. Example: sum = a + b; //finding the sum
2. Multiline comment: Starts with /* and ends with */ and spans multiple lines. Example: /*Author: Robin Gupta
Aim: To check whether a number is prime or not.
Date: August 9, 2025 */
3. Documentation comment: Used to create API documentation using the javadoc tool. Starts with /** and ends with */. Example:/**
This is a documentation comment.
It can span multiple lines.
@author Robin
@version 1.0
@param name the name of the person
@return a greeting message
*/
Input using InputStreamReader and BufferedReader class
import java.io.*;
class Input{
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Name: ");
String name = br.readLine();
System.out.print("Roll number: ");
int roll = Integer.parseInt(br.readLine());
System.out.print("Section: ");
char sec = br.readLine().charAt(0);
System.out.print("Height in metres: ");
double height = Double.parseDouble(br.readLine());
System.out.println(name);
System.out.println(roll);
System.out.println(sec);
System.out.println(height);
}
}
Input by using Scanner class
Scanner class was first introduced in JDK 1.5. It is available in the java.util package.
import java.util.Scanner;
class Input{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("Name: ");
String name = br.nextLine();
System.out.print("Roll number: ");
int roll = in.nextInt();
System.out.print("Section: ");
char sec = in.nextLine().charAt(0);
System.out.print("Height in metres: ");
double height = in.nextDouble());
System.out.println(name);
System.out.println(roll);
System.out.println(sec);
System.out.println(height);
}
}
Types of errors:
1. Syntax error (Compile-time error): Occurs when the grammar of the language is not followed. Example: Missing semicolon.
2. Runtime error (Exception): Occurs during program execution. Example: Division by zero.
3. Logical error: Occurs when the output comes but it is undesirable. Most difficult to spot. Example: * used instead of +.
Chapter 7: Conditional Statements in Java
System.exit(0);
is a statement used to terminate the Java program.
The switch case is a multi-branching statement. The break keyword in switch is used to exit from the switch block. The default keyword is used to execute a set of statements when none of the cases match. The fall through occurs when the break keyword is missing in any case and the program flow slips to the next case.
Chapter 8: Iterative Constructs in Java
Entry-controlled loops: The for loop and the while loop are entry-controlled. It means that the condition is checked before executing the loop body.
Exit-controlled loop: The do while loop is an exit-controlled loop. It means that the condition is checked after executing the loop body. It guarantees minimum one-time execution.
The break keyword in loops is used to exit from loops. The continue keyword is used to skip the current iteration of the loop and move to the next iteration of the loop.
An infinite loop is one that never terminates.
Examples:for(;;){
System.out.println("Hello there!");
}
while(true){
System.out.println("Hello there!");
}
do{
System.out.println("Hello there!");
}while(true);
Empty Loops
A loop without a body.
Example:for(int i = 1; i <= 5; i++);
System.out.println("Hello");