Krish Iyengar
Chapter 1: Revision of Class 9 syllabus
NOTE: CONCEPT-BASED QUESTIONS CAN COME FROM 9TH SYLLABUS SO JUST FLIP
THROUGH 9TH NOTES ONCE + WHATEVER I’VE GIVEN HERE
1. TYPES OF HIGH-LEVEL LANGUAGES (HLL)
a. Procedure-Oriented Language: Allows users to develop their logic by using several
functions
FEATURES LIMITATIONS
Programs divided into modules called No restriction on data values
functions
Functions share global data Causes debugging problems
Data values move freely from function to No reusability
function
b. Object-Oriented Language: Creates memory areas for both data and functions.
Features:
i. Secure data values
ii. Easier debugging
Principles of Object-Oriented Programming (OOP)
1. ENCAPSULATION: Hiding implementation details and only exposing a public interface
Variables and methods combined into one unit (object/class)
Secures data from unauthorised access using access specifiers:
ACCESS MODIFIER PRIVATE DEFAULT PROTECTED PUBLIC
Same class ✔ ✔ ✔ ✔
✔: can access Subclass of same package ✖ ✔ ✔ ✔
Different class of same package ✖ ✔ ✔ ✔
✖: can’t access
Subclass of different package ✖ ✖ ✔ ✔
Different class of different package ✖ ✖ ✖ ✔
Examples:
PRIVATE DEFAULT
1
Krish Iyengar
PROTECTED PUBLIC
2. INHERITANCE: Class/object acquiring properties of another class/object
Parent/base class provides properties
Child/derived class inherits properties
3. POLYMORPHISM: Defining 1 interface with many implementations
Method Overloading: Multiple methods with same name but di erent parameters
4. ABSTRACTION: Hiding internal details and only showing essential functionality
E.g. you don’t need to know the internal mechanics of a car to drive it
2
Krish Iyengar
Chapter 2: Library Classes
1. JAVA PACKAGES
PACKAGE PURPOSE
[Link] (default) Contains classes related to string manipulations
[Link] Contains classes for input/output methods.
[Link] Contains classes to implement GUI (Graphical User Interface)
[Link] Contains classes to support network operations.
[Link] Contains utility classes to implement data structure.
[Link] Contains classes for implementing applets.
[Link] Contains classes for mathematical operations.
2. KEYWORDS AND SIGNS:
a. “import”: Used to include a package
b. “*”: Indicates that all classes of imported package can be used
3. PRIMITIVE VS COMPOSITE DATATYPES:
PRIMITIVE DATATYPES COMPOSITE DATATYPES
Fundamental datatype created by Set of user-defined primitive
system developers datatypes
int, short, char, byte, double, float, arrays, classes, Strings
long, boolean
4. WRAPPER CLASS: Class that contains a primitive datatype
a. When an object to a wrapper class is created, memory is allocated to contain a
primitive datatype
b. Need of Using a Wrapper Class:
i. To store primitive values in objects.
ii. To convert String to other primitive datatypes and vice-versa
WRAPPER CLASS PRIMITIVE DATATYPE MEMORY SIZE
Boolean boolean 1 byte
Byte byte 1 byte
Character (NOT Char) char 2 bytes
Short short 2 bytes
Integer (NOT Int) int 4 bytes
Float float 4 bytes
Long long 8 bytes
Double double 8 bytes
CONVERTING FROM STRING TO PRIMITIVE CONVERTING FROM PRIMITIVE DATATYPES TO
DATATYPES STRING
String str = “12345” 1. Integer to String
1. String to Integer String str = [Link](num);
a. int num = [Link](str); 2. Long to String
b. int num = [Link](str); String str = [Link](num);
2. String to Long 3. Float to String
a. long num = [Link](str); String str = [Link](num);
b. long num = [Link](str); 4. Double to String
String str = [Link](num);
3
Krish Iyengar
String str = “1234.5”
3. String to Float
a. float num = [Link](str);
b. float num = [Link](str);
4. String to Double
a. double num = [Link](str);
b. double num = [Link](str)
Note: if str is not a valid number: gives
NumberFormatException
AUTOBOXING UNBOXING
| 5. CHARACTER DATATYPES
32 Space ( ) Automatic conversion Converting an object of
48-57 0-9 of primitive data type to wrapper class to primitive
65-90 A-Z object of equivalent datatype
wrapper
97-122 a-z
Need: Need:
1. Passes primitive 1. Used when wrapper
6. CHARACTER-ORIENTED FUNCTIONS datatype to a function object is to be passed to
that uses wrapper function having primitive
(char ch = ‘A’)
object as function argument
FUNCTION RETURN DATATYPE argument 2. When a data from array
[Link]() boolean 2. To add a primitive list is to be used as a
[Link]() boolean data in the list of array primitive data
[Link]() boolean elements
[Link]() boolean Example: Example:
[Link]() boolean 1. Double d = new 1. Double d = new
[Link]() char Double(100.0); Double(100.0);
double d2 = d;
[Link]() char
2. Integer i = new 2. Integer i = new
Integer(100); Integer(100);
int i2 = I;
4
Krish Iyengar
Chapter 3: Arrays (SDA, DDA)
1. SINGLE DIMENSIONAL ARRAY (SDA)
a. Syntax:
Accepting from user Intitialising (note: use {}, not [])
b. Length Function: [Link] NOT [Link]()
2. OPERATIONS ON ARRAYS
a. Searching
i. Linear Search: One of the simplest techniques in which the searching begins
from the 0th index
ii. Binary Search: The complete array is divided into two halves and the element is
searched in the 1st and 2nd half
LINEAR SEARCH BINARY SEARCH
Works on unsorted and sorted Works only on sorted arrays
arrays
Search begins from 0th index Array is divided into two halves
5
Krish Iyengar
b. Sorting
i. Bubble Sort: Array is sequentially scanned to choose the greatest/least number,
which is pushed to the end for that particular iteration
Ascending order Descending order
ii. Selection Sort: Selects a specific element and interchanges it to form
ascending/descending order of elements
Ascending order Descending order
BUBBLE SORT SELECTION SORT
Compares adjacent elements and Selects smallest/largest element
swaps if they are in wrong order from array and swaps with current
element
Performs more swaps Performs fewer swaps
c. Insertion: Adding an element into an array
6
Krish Iyengar
d. Deletion: Removing an element from an array
e. Concatenation/Merging: Combining 2 arrays to form a single array
3. DOUBLE DIMENSIONAL ARRAY (DDA)
a. Syntax:
b. Programs:
Sum of left diagonal (note: can only be Sum of right diagonal (note: can only be used if rows = columns)
done if rows = columns)
SINGLE DIMENSIONAL ARRAY DOUBLE DIMENSIONAL ARRAY
Element represented by single index (0, 1, 2 etc.) Element represented by pair of indices ((0,0),
(0,1), (0,2) etc.)
7
Krish Iyengar
Chapter 4: String Handling
The [Link] package contains Java Class Library (JCL), which contains String class
Default initial value for string: “”
[Link](): Accepts single word
[Link](): Accepts multiple words
FUNCTION RETURN TYPE NOTES
length() int Starts from 1 (e.g. length of “HELLO” is 5). For
all other functions, string is numbered from 0
to [Link]() - 1
charAt(int index) char If index exceeds length of string:
StringIndexOutOfBoundsException
indexOf(char ch) int If ch is not present: returns -1
indexOf(char ch, int int Checks whether character is present from
start_index) start_index onwards (includes start_index)
lastIndexOf(char ch) int Check last occurrence of ch
substring(int start_index) String Substring from start_index till end of string
substring(int start_index, int String Substring from start_index till end_index
end_index) (excludes end_index)
toLowerCase() String Converts entire string to lowercase
toUpperCase() String Converts entire string to uppercase
replace(char old_char, char String Replaces all instances of old_char with
new_char) new_char
replace(String old_str, String String Same as above, but with multiple characters
new_str)
concat(String str2) String Same as str1 + str2 (note: str1 – str2 is not a
valid statement)
equals(String str2) boolean Used to check if 2 strings are equal (note: str1
== str2 is not a valid statement)
equalsIgnoreCase(String boolean Same as above, but ignores upper/lowercase
str2) (e.g. “HELLO” and “hello” are the same)
compareTo(String str2) int If str1 = “APPLE” and str2 = “BANANA”:
1. Checks di erence in ASCII values of 1st
Note: make a clear distinction characters (i.e. A – B = 65 – 66 = -1).
between: 2. When it is not 0, it returns that value.
longer/shorter: talks about length
of strings
Else it checks di erence of 2nd, 3rd, 4th
bigger/smaller: talks about ASCII characters etc. until end of string
values of strings 3. If both strings are the same, returns 0
e.g. “BANANA” is longer than If str1 = “APPLE” and str2 = “APPLES”: returns
“MANGO” but smaller than it [Link]() – [Link]()
Note: this is called lexicographic analysis
compareToIgnoreCase(String int Same as above but ignores upper/lowercase
str2) (e.g. “HELLO” and “hello” are the same)
trim() String Removes leading and trailing spaces
endsWith(String str) boolean Checks whether string ends in str
startsWith(String str) boolean Checks whether string starts with str
valueOf() String Converts primitive value to string (ref.
Chapter 2)
8
Krish Iyengar
equals() compareTo()
Checks whether 2 strings are identical Checks whether a string is bigger/smaller
than another
Return type: boolean Return type: int
Note: do NOT use StringBu er Functions
9
Krish Iyengar
Chapter 5: User-Defined Methods
Method/Function: Collection of statements that are grouped together
1. ADVANTAGES OF USING METHODS:
o Reuse segment of code by simply using method name
o Divide complex tasks into segments
o Make debugging easier
o Occupy less space and executes faster
2. PARTS OF A METHOD
a. Method Header: First line in which method is declared. Contains:
i. Access Specifier: Scope of usage of method
1. public (default): Class members can be used outside the visibility of the
class
2. private: Member methods can only be used within the visibility of the class
3. protected: Used in the class as private members but can be used in
another class during inheritance.
ii. Static vs Non-Static methods:
STATIC METHODS NON-STATIC/INSTANCE METHODS
Can be accessed without creating an Cannot be accessed without creating
object an object
static keyword is used static keyword is not used
iii. Return Type: Datatype to be returned to the caller. If the method doesn’t return
aka function any value, void keyword must be used. If datatype is specified, method must
terminator include return statement
- Variables with same name (in this case,
sum) in di erent methods are treated as
di erent variables.
- The method you are passing the values to
(in this case, compute()) must have the
datatypes of the parameters separately (i.e.
compute(int a, b) won’t work)
- return can only give back 1 value. If more
than 1 return statement is included, only
the first will be executed.
- return must be the last line of the method
block. If any statements are included after
iv. Function Name: Name of
return, they won’t be executed.
function (you don’t say)
10
Krish Iyengar
v. Parameters: List of variables (with datatypes) which receive the values passed
ACTUAL PARAMETERS FORMAL PARAMETERS
Parameters passed to the method while Parameters passed to the method while
calling function. declaring function.
Datatypes not mentioned Datatypes mentioned
Example: sum = compute(a,b) Example: int compute(int a, int b)
b. Method Block: Statements to be executed
c. Method Signature: Place where method is called
3. PASS BY VALUE VS PASS BY REFERENCE
PASS BY VALUE PASS BY REFERENCE (aka ALIASING)
Used for primitive datatypes Used for non-primitive datatypes
A copy of the actual parameters is passed to formal A reference (aka alias) of the actual parameters is
parameters passed to formal parameters.
Changes in formal parameters don’t reflect on actual Changes in formal parameters reflect on actual
parameters parameters
Di erent memory locations Same memory location
4. PURE METHOD VS IMPURE METHOD
PURE METHOD (aka ACCESSOR METHOD) IMPURE METHOD (aka MUTATOR METHOD)
Returns a value Doesn’t return a value
Doesn’t change state of the object (i.e. actual Changes state of the object (i.e. actual
parameters don’t change) parameters change)
Takes place in call by value Takes place in call by reference
Restrictions needn’t be imposed Restrictions must be imposed
5. FUNCTION OVERLOADING
Example of polymorphism
(Flip page for example)
area() is being overloaded to calculate both area of rectangle and area of circle
Note: You can’t switch the order of the functions around.
11
Krish Iyengar
You can’t swap the order of
these 2 functions
(I wasted an entire page just for this screenshot so make sure you know it well)
12
Krish Iyengar
Chapter 6: Class as the Basis of all Computation
1. OBJECTS:
Class is an object factory: As many objects having common functions are created within a
class, i.e.:
o Object is an instance of a class
o Class is a blueprint/template/abstraction of objects
Def. instantiation: Process of creating a new object of a class
Syntax to create an object:
‘new’ keyword: Allocates space in
dynamic memory to store functions
belonging to an object
2. USER-DEFINED DATATYPES VS PRIMITIVE DATATYPES:
USER-DEFINED DATATYPES (e.g. class) PRIMITIVE DATATYPES (e.g. int, double etc.)
Created by users Built-in
Include many primitive datatypes Independent component
3. CONTENTS OF A CLASS: a. Class Wrapper: keyword
class + class name
b. Instance vs Static vs
Local Variables: Ref.
di erentiation table in
Chapter 8
c. Constructor: Initialises
instance variables
d. Member Methods, Access
Don’t worry about this Specifiers: Ref. Chapter 5
now, it has no purpose in for notes
this program either
✔: can be accessed
without object
❌: needs object to be
accessed
13
Krish Iyengar
Chapter 7: Constructors
1. CONSTRUCTOR: Member method having the same name as class
(initialises instance variables)
2. INVOKING A CONSTUCTOR:
3. FEATURES OF A CONSTRUCTOR:
a. Automatically called when creating an object
b. Doesn’t need return type as it is only used for initialising instance variables
c. Always public as it gets called from outside class while creating an object
d. Automatically overloaded
4. TYPES OF CONSTRUCTORS:
DEFAULT NON-PARAMETERISED PARAMETERISED
Automatically provided by the Can be provided by compiler or Can be provided by compiler or
compiler if no constructor is declared by programmer declared by programmer
declared
Initialises objects with default Initialises objects with values Initialises objects with values
values (ref. bottom) available within it passed during object creation
Note: All 3 of these codes give the same output a + b (3)
5. CONSTRUCTOR OVERLOADING:
Using di erent constructors with
same name but di erent parameters
6. CONSTRUCTORS VS METHODS:
CONSTRUCTORS METHODS
Same name as class Di erent name from
class
Doesn’t need to be Needs to be called
called
Doesn’t return a value May/may not return a
value
(Usually) public (Usually) DEFAULT VALUES double: 0.0 char: \u0000 (null)
public/private byte, short, int: 0 float: 0.0F String, array,
long: 0L boolean: false object: null
14
Krish Iyengar
Chapter 8: Encapsulation and Inheritance
1. ENCAPSULATION: Ref. Chapter 1 for notes
Promotes data hiding
2. DATA HIDING: Using data members of a class within the same class
Can be used in another class/main class, but must be in same program
3. SCOPE OF VARIABLES: Extent of a variable’s use in a program
Access Specifiers: Ref. Chapter 5 for notes
INSTANCE VARIABLES STATIC/CLASS VARIABLES LOCAL VARIABLES
Individual copy created for each Common copy created for all Can only be used by objects within
object objects method
static keyword not used static keyword is used static keyword not used
Can only be accessed by object Don’t need object to be accessed Don’t need object to be accessed
(within same method)
Belong to each object Independent of all objects Belong to the method
4. INHERITANCE: Ref. Chapter 1 for notes
Types of inheritance:
TYPE EXPLANATION
Single Inheritance Subclass derived from single class
Multiple Inheritance Subclass derived from multiple classes
Hierarchical Inheritance Multiple subclasses derived from single class
Nested/Multilevel Subclass 4 derived from subclass 3 derived from subclass 2… derived
from single class
Hybrid Inheritance Di erent types of inheritance are used together
---------------------------------------------------------------------
15