ICSE Computer Applications
Reference Booklet
➔ Principles of OOPS
➔ ASCII Codes, Escape sequences, Tokens
➔ Data Type Specsheet
➔ Operator Precedence
➔ Java: Compiler, Interpreter, Platform Independence
➔ Math Functions, Random Number Generation
➔ Scanners, Types of Errors
➔ Boxing and Unboxing
➔ Parse and Character Functions
➔ Array Algorithms: Sort and Search
➔ Arrays: Insertion, deletion, merging
➔ String functions
➔ Functions, Overloading
➔ Encapsulation, Access Specifiers
➔ Overriding, Late binding
➔ Constructors
Principles of OOPS
Abstraction: It is the act of representing only the essential features/information,
while hiding complex implementation details to the user, focusing on “what” a
class does, than “how” it does it.
Encapsulation: It is the wrapping up of data and functions of an object as one
unit, that can be used to together for a specific purpose, while restricting direct
access to internal data and controlling its access.
Inheritance: It is the property by which a class [subclass] acquires (inherits) the
features and behaviour of another class [superclass], promoting code
reusability.
Polymorphism: It is the process of using a given method for multiple
operations. A method with the same name is made to perform different function
based on the given conditions.
ASCII Codes
A-Z 65 - 90
a-z 97 - 122
0-9 48 - 57
whitespace - ‘ ‘ 32
Escape Sequences
\t horizontal tab \v vertical tab
\b vertical tab \\ backslash
\n new line \f form feed
\’ single quote \0 null
\” double quote \r carriage return
Tokens: Each individual component of a Java program that carries some
meaning and takes active part in the program execution. The tokens are:
Literals
Identifiers
Assignment operators
Operators: Arithmetic, Relational, Logical
Punctuators: comma, semicolon, period
Separators: comma, parentheses, braces, square brackets
Keywords
Data Types Spec-sheet
Type Default Size Range
boolean false 1 bit true or false
char \u0000 2 bytes 0 to 65,535
(unsigned,
UTF-16)
byte 0 1 byte -128 to 127
short 0 2 bytes -215 to (215 - 1)
int 0 4 bytes -231 to (231 - 1)
long 0L 8 bytes -263 to (263 - 1)
float 0.0f 4 bytes ±1.4E-45 to
±3.4028235E38
(7 digit
precision)
double 0.0 8 bytes ±4.9E-324 to
±1.797693134862
3157E308
(15-16 digit
precision)
Operator Precedence Table
Operator Associativity
++ -- Right to Left
++ -- + - ~ ! (type) Right to Left
* / % Left to Right
+ - Left to Right
< <= >= > Left to Right
== != Left to Right
& Left to Right
^ Left to Right
| Left to Right
&& Left to Right
|| Left to Right
?: Right to Left
= += -= *= /= %= Right to Left
Java: Compiler, Interpreter, Platform Independece
Java is often called a "compiler-interpreter language" because it uses both a
compiler and an interpreter: the Java compiler (javac) converts source code into
bytecode. Bytecode is an platform-independent, intermediate code created
after the source code is complied. The bytecode can be interpreted on any
system with a JVM to run the program.
Java is platform-independent because it compiles to bytecode, a
platform-neutral instruction set, which is then executed by the JVM on any
device with a compatible JVM, enabling the "write once, run anywhere" - WORA
capability.
Math Functions
Method Description Return Type
min(a, b) return smallest & largest int/long/float/double
max(a, b) value respectively
sqrt(a) return square root and double
cbrt(a) cube root respectively
pow(a, b) returns value of ab double
abs(a) modulus function int/long/float/double
round(a) up to nearest integer - int/long
standard rounding
rint(a) to nearest integer - double
precise rounding
floor(a) nearest integer less than double
or equal to ‘a’
ceil(a) nearest integer greater double
than or equal to ‘a’
random() random real; 0 <= r < 1 double
log(a) return value of ln a and double
exp(a) ea respectively
sin(a), cos(a), tan(a) give respective values in double
radians
[Link](a) v.s. [Link](a)
[Link]([Link](2.5)); // 2.0 (to even integer)
[Link]([Link](3.5)); // 4.0
[Link]([Link](2.5)); // 3 (standard rounding)
[Link]([Link](3.5)); // 4
Random Number Generation
1) [Link]()
0 <= r < 1
2) [Link]() + k
k <= r < k + 1; k is the shifting factor
3) [Link]() * a
0 <= r < a; a is the scaling factor
4) (int)([Link]() * max)
0 <= i <= max - 1
5) (int)([Link]() * (max - min + 1)) + min
min <= i <= max
Scanners
[Link](): Prints on the current line and moves the cursor to a new
line after printing. Empty statement simply moves the cursor to a new line.
[Link](): Prints on the current line and does not shift the cursor.
Scanner issue with ‘nextLine()’
If you use nextLine() after nextInt(), it can seem like it is skipping input.
nextInt() leaves a newline character in the buffer, so nextLine()
immediately reads it.
[Link](); // Consume the leftover newline
[Link](); //accepts next word
[Link]().chatAt(0); //next character
[Link](','); //changes the delimiter from ‘ ’ to ','
[Link](); //accepts all characters until the next comma
Syntax Error: Occurs due to a grammatical error in the program, when the
programmer breaks the structure of the program. For example, missing
punctuators, incorrect instructions, undefined variables, etc.
Logical Error: Occurs when the program gets compiled successfully, but does
not produce the desired results. For example, incorrect formulae or conditions.
Runtime Error: Occurs at runtime when the program gets successfully
compiled, but the computer does not respond properly while executing a
particular statement. For example, division by zero, null reference, array index
out of bounds, etc.
Type of Error Detected by When
Syntax Compiler Compilation
Logical Programmer Run-time
Runtime JVM [Interpreter] Run-time
Switch Statement - Fall Through
The condition where a break statement is not used after a case, causing the
control to enter the next case for execution.
Wrapper Classes
Boxing: Conversion of a value of a primitive data type into an object of its
equivalent Wrapper class. When boxing is done automatically by the compiler
at compilation, it is called Autoboxing.
→ When we want to pass a primitive data type argument to a method that uses
a wrapper class as the function argument. Ex. ArrayList.
Unboxing: It is the opposite of Boxing. It is the conversion of the object of a
Wrapper class into its primitive data type. When unboxing is done
automatically by the compiler at compilation, it is called Auto-unboxing.
→ When the value from the object of a wrapper class is to be passed to a
function which accepts arguments of primitive data types
→ When the returned value of a method, whose return type is a Wrapper Class,
is to be used as a primitive data type.
int p = 10;
Integer P = [Link](p); // Boxing
Integer P = [Link](10);
int p = [Link](); // Unboxing
int p = 10;
Integer P = p; // Autoboxing
int p2 = P; // Autounboxing
Here p and p2 are of int type and P is of Integer type.
[Link](): Returns a primitive ‘int’ value. Autoboxing may be required
in some cases.
[Link](): Returns an object of the ‘Integer’ class. Auto-unboxing may be
required in some cases.
OPTIONAL TOPIC
Integer Caching [Object Pooling]
Integer a = [Link](100);
Integer b = [Link](100);
[Link](a == b); // true (same cached object)
Integer c = [Link](200);
Integer d = [Link](200);
[Link](c == d); // false (new objects)
Java caches Integer values from -128 to 127. For values outside this range, new
objects are created.
The String Pool
→ “hello” is added to the string pool
String s1 = "hello";
String s2 = "hello";
[Link](s1 == s2); // true (both point to the same
object)
→ New object is added to the heap, different memery locations
String s1 = new String("hello");
String s2 = new String("hello"); //new object, bypassing string
pool
[Link](s1 == s2); // false (different objects)
→ “intern()” method checks the pool for the string and returns the pooled
address, if it exists, else it is created
String s1 = new String("hello").intern(); //forces pooling
String s2 = "hello";
[Link](s1 == s2); // true (both now point to the
pooled object)
OPTIONAL TOPIC CONLCUDED
Character Functions
Method Return Type
[Link](c)
[Link](c)
[Link](c) boolean
[Link](c)
[Link](c)
[Link](c)
[Link](c)
Character [auto-unboxed to char]
[Link](c)
Array Algorithms
[Sorting]
Bubble Sort
int[] arr = {1, 3, 4, 8, 5, 6, 2, 9, 0, 7};
int temp = 0;
for(int i = 0; i < [Link] - 1; i++) {
boolean swap = false;
for(int j = 0; j < [Link] - 1 - i; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j]; arr[j] = arr[j + 1];
arr[j + 1] = temp; swap = true;
}
}
if(!swap) break;
}
for (int x : arr) [Link](x + " ");
Selection Sort
int[] arr = {1, 3, 4, 8, 5, 6, 2, 9, 0, 7};
int temp = 0;
for (int i = 0; i < [Link]; i++) {
int minIndex = i;
for (int j = i + 1; j < [Link]; j++)
if (arr[j] < arr[minIndex]) minIndex = j;
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
for (int x : arr) [Link](x + " ");
[Searching]
Binary Search
int low = 0, high = [Link] - 1, mid = 0;
int target = 5;
boolean found = false;
while(low <= high) {
mid = low + (high - low)/2;
if(mid == target) {
found = true;
break;
} else if (mid < target) low = mid + 1;
else high = mid - 1;
}
if(found) [Link]("Target found at index: " + mid);
else [Link]("Target not found");
Linear Search Binary Search
Works on sorted and unsorted arrays Words only on sorted arrays
Search begins at the start of the array, Array is divided into two halves and
i.e., from the 0th index, and continues one of the halves is searched, which is
till the element is found further split until the target is found
Works on single and Works only on single dimensional
multi-dimensional arrays arrays
Has time complexity O(n) Has time complexity O(logn)
It uses equality comparisions It uses ordering comparisions
Inserting Elements
int[] set = new int[6]; //new array
for(int i = 0; i < [Link] - 1; i++) set[i] = i;
//initialising all but one element
int index = 3, rep = 21;
//shifting elements
for (int i = [Link] - 2; i >= 3; i--) set[i + 1] = set[i];
set[3] = rep;
for(int x : set) [Link](x + " ");
Deleting Elements
int[] set = new int[6]; //new array
for(int i = 0; i < [Link]; i++) set[i] = i;
//initialising array
int index = 3;
//shifting elements
for (int i = index; i < [Link] - 1; i++) set[i] = set[i + 1];
set[[Link] - 1] = 0; //setting last element to zero
for(int x : set) [Link](x + " ");
//or you can choose to print only till the last element
Merging Arrays
int[] arr0 = new int[3]; //new arrays
int[] arr1 = new int[5];
int[] arr2 = new int[[Link] + [Link]];
for (int i = 0; i < [Link]; i++) arr0[i] = i;
//initialisation
for (int i = 0; i < [Link]; i++) arr1[i] = i;
for(int i = 0; i < [Link]; i++) arr2[i] = arr0[i]; //merging
arrays
for(int i = 0; i < [Link]; i++) arr2[i + [Link]] =
arr1[i];
for(int x : arr2) [Link](x + " ");
Functions and Overloading
Formal parameters are the parameters described in the method header of the
called function and receive their values from its calling function.
Actual parameters are the parameters/values passed to the calling function
when it is called.
Calling Function: Method which calls another method.
[Link](x);
Called Function: Method which is called by another method.
public int factorial(int a) {
if (a == 0) return 1;
else return a * factorial(a - 1);
}
Pass by value Pass by reference
Process of passing a copy of the Process of passing the reference
actual parameters to the formal [address/alibi] of the actual
parameters parameters to the formal parameters.
Any changes made in the formal Any changes made in the formal
parameters does not reflect in the parameters are reflected in the actual
actual parameters parameters
Usually for primitive data types Usually for objects and Arrays
Pure method Impure method
Does not change the internal state of Changes the internal state of the
the object object
Generally returns a value Generally does not return a value
Also known as getter/accessor Also known as setter/mutator
Early/Static binding: During function overloading, when an overloaded method
is called, the system finds the best match of the function arguments and the
parameter list [i.e., types and number of parameters] during the program
compilation, which is known as static or early binding.
Function overloading: It is the process of defining functions/methods with the
same method name, but different number and types of parameters.
Recursive function: It is a function that calls itself in its body.
public static int add(int a, int b) { return a + b; }
Method Header: public static int add(int a, int b)
Method Signature: add(int a, int b)
Best Match - Method Overloading
Two method are said to be the best match for overloading if:
→ Types of actual and formal parameters are same
→ Number of actual and formal parameters are same
→ Order of data types of actual and formal parameters is same
String Functions
Function Usage Return Type
[Link]() converts to lower case String
[Link]() converts to upper case String
replace(ch,ch1) replaces all occurrences of String
‘ch’ with ‘ch1’
replace(str, str1) replaces all occurrences of String
‘str’ with ‘str1’
[Link]() removes leading and trailing String
spaces
[Link](str1) if ‘str’ equals ‘str1’; boolean
case-sensitive
[Link](str1) if ‘str’ equals ‘str1’; boolean
case-insensitive
[Link]() returns length of String int
[Link](i) returns the character at ‘i’ char
[Link](m) [OR] characters from ‘m’ (incl.) String
[Link](m, [Link]())
[Link](m, n) characters from ‘m’ (incl.) to String
‘(n - 1)’ (incl.)
[Link](str1) concatenates ‘str’ and ‘str1’ String
[Link](ch) index of first occurence int
[Link](ch, i) index of first occurrence int
after ‘i’ (inclusive)
[Link](ch) index of last occurence int
[Link](str1) compareTo() case-insensitive int
[Link](str1) if ‘str’ starts with ‘str1’ boolean
[Link](str1) if ‘str’ ends with ‘str1’ boolean
[Link](str1) - returns an ‘int’
if str = str1, 0 ]
if str > str1, +ve ] - if str ≠ str1 (or) str = str1 and their lengths are equal
if str < str1, -ve ]
[Link]() - [Link]() ] - if common characters are equal
‘ ‘ (32) < ‘0’ to ‘9’ (48 to 57) < ‘A’ to ‘Z’ (65 to 90) < ‘a’ to ‘z’ (97 to 122)
Encapsulation
Access Specifiers
[for variables] default public private protected
same class ✅ ✅ ✅ ✅
same package subclass ✅ ✅ ❌ ✅
same package
non-sublcass
✅ ✅ ❌ ✅
different package
subclass
❌ ✅ ❌ ✅
different package
non-subclass
❌ ✅ ❌ ❌
[final keyword] Behaviour
variables constant (cannot be changed)
methods
cannot be overridden, but can be
overloaded
classes cannot be inherited
Instance vs Class Variables
- Instance Variables Class Variables
Definition Each object of the class has They are common fields for all
an individual copy objects of the class
Declaration declared without ‘static’ declared using ‘static’ keyword
keyword
Access Object name must be Object name is not required;
referred to handle them they can be access directly with
[Link]; the class name
[Link];
Copies One per object One per class
Memory When the object is created When the class is loaded
allocation
Use case Object-specific data Shared properties of all objects
Method Overriding
When a subclass provides its own implementation of a method that is already
defined in the super class. For overriding, the method header (name, return
type, parameters) of the original and overridden methods must be the same.
‘final,’ ‘private’ and ‘static’ methods cannot be overriden.
Late/Binding binding: During method overriding, when an overridden method
is called through a parent class reference, the system determines the
appropriate method to invoke based on the runtime type of the object, not the
reference type. This decision happens during program execution, which is known
as dynamic or late binding.
- Static Binding Dynamic Binding
Timing Compile-time (early binding) Runtime (late binding)
Methods static, final and private overridden methods
Performance Faster Slightly slower (runtime
lookup
Decided by Compiler JVM
Constructors
Special member methods with the same name as the class name. They help in
intiailising the data members in the classes to some initial values. They are
non-returnable.
Constructor Overloading
If a parameterised constructor is defined and no parameters are provided at the
time of object creation, the default constructor will not be used. Another
non-parameterised constructor must be explicitly defined in this case.
Copy Constructor
A constructor that is used to initialise the instance variables of an object by
copying the initial values of the instance variables from another object, is known as
a Copy Constructor. It can be either a Direct Entry Copy Constructor, or may
require an object to be passed.
A Direct Entry Copy Constructor is not recommended as it leads to unintended
affects due to reference sharing, where changes in one of the objects, are
reflected in the other object as well, since they both point to the same memory
location.
public class ClassExpt {
private int age;
ClassExpt(int age) { [Link] = age; }
public static void main(String[] argus) {
ClassExpt c1 = new ClassExpt(23);
ClassExpt c2 = c1;
[Link] = 33;
[Link]([Link] + " " + [Link]); //33 33
[Link] = 43;
[Link]([Link] + " " + [Link]); //43 43
}
}
Instead a copy constructor, in which the required object is passed, is preferred.
This way, the “new” keyword creates a new object, with the same values of the
instance variables of the intended object, pointing to a different memory
location. Any change in one of the objects, will not affect the other.
public class ClassExpt {
private int age;
ClassExpt(int age) { [Link] = age; }
ClassExpt(ClassExpt c) {
age = [Link];
//it is the same class, even though ‘age’ is private,
//we can access it directly
}
public static void main(String[] argus) {
ClassExpt c1 = new ClassExpt(23);
ClassExpt c2 = new ClassExpt(c1);
[Link] = 33;
[Link]([Link] + " " + [Link]); //33 23
[Link] = 43;
[Link]([Link] + " " + [Link]); //33 43
}
}
_______________________________________________________________