ICSE Class 9 Java Notes
ICSE Class 9 Java Notes
CHAPTER 1
Introduction to Object-Oriented Programming Concepts
Programming language: The sequences of set of instructions of any
programming language used to find the solution of the problem is known as
programming language or computer language.
POPs OOPs
It divides the program into small It divides the program into
parts known as methods. objects.
It follows a top-down approach. It follows a bottom-up approach.
It deals with algorithm. It deals with data.
It is less secure. It is more secure.
Examples: C, Fortran Examples: C++, Java
❖ Java history
Java is a third-generation programming object-oriented
programming language with various features for writing codes or applets.
James Gosling, Patrick Naughton, Chris Warth, Ed Frank and Mike
Sheridan at Sun Microsystems, Inc., USA imagined the Java in 1991. It
was developed by the name ‘Oak’ and renamed as ‘Java’ in 1995.
Java was first known (internally) as GreenTalk, but it became
known as Oak before finally becoming Java.
Interpreter: The program that translate the source program or source code
to machine language or low-level language step by step or line by line is known
as interpreter.
Compiler: The program that translate the source program or source code
to machine language or low-level language at once is known as compiler.
7
• Multithread
• Architecture neutral
• Interpreted
• High performance
• Distributed
• Dynamic
***
10
CHAPTER 2
Elementary concept of Classes and Objects
Object: An identifiable entity with some characteristics (or data), a
state and behavior (or function) is known as an object.
OR
An object is a self-contained entity which contains attributes and
behavior.
An object has three states:
i) Identity
ii) State
iii) Behavior
Example: Bike is an object
Characteristics– Black colour, 150cc engine, company
Bajaj
Identity- Bajaj Discover 10
Behavior- Starting the engine, changing the gear, using the
break etc.
Class: A class defines the common data and methods as single unit
that will be shared by a set of objects.
OR
A class is a blueprint that defines the variables and the methods
common to all objects.
OR
A class is a collection of objects of similar types.
***
12
CHAPTER 3
Class as Basis of all Computation
Character set is a set of valid characters that a language can recognize. A
character set represents any letters, digits or any other sign.
Java uses the Unicode character set.
Unicode character set: Unicode is a two-bytecode character code
set that has characters representing almost all characters in human’s
alphabets and writing systems around the world including Arabic,
Chinese, and many others languages of the world.
There is total 65,536 characters in the Unicode character set.
Characters ASCII values
A to Z 65 to 90
a to z 97 to 112
0 to 9 48 to 57
'' 32
Valid identifiers
Myfile
isPrime
var
n1
mum_1
Invalid identifiers
DATA-REC
27AC
break
[Link]
14
v. Shift operator
vi. Java shorthand operator
Arithmetic operators: The operators that is used to perform
arithmetic operations are called arithmetic operators.
Operator symbols: +, -, *, /, %
Given a=5 and b=3: -
Examples: a+b // Answer: 8
a-b //Answer: 2
a*b //Answer: 15
a/b //Answer: 1
Note: In the modulo (%) operator, there are two cases:
Case 1: If a>b, then:
the answer will always be remainder.
Example: a%b //Answer: 2
(Given a=5 and b=3)
Case 2: If a<b, then:
the answer will always be a.
Example: a%b //Answer: 3
(Given a=3 and b=5)
&&
Expression1 Expression2 Result
false false false
false true false
true false false
true true true
||
Expression1 Expression2 Result
false false false
false true true
true false true
true true true
!
Expression1 Result
false true
true false
Dot operators: The operator that connects an object or class with its
properties (variables) or behaviours methods) is called dot operators.
Examples: [Link]();
(obj is object name and display is function name)
new operators: The new operator is used to create (instantiate)
objects of a class or arrays and also allocates memory on the heap for the
object or array and returns a reference to it.
Examples: Scanner sc =new Scanner ([Link]);
int arr[]=new int [10];
Java shorthand operators: Java shorthand operators (also called
compound assignment operators) allow you to combine an operation and
assignment in a single statement.
Examples: a+=5 represents a=a+5
a-=5 represents a=a-5
a*=5 represents a=a*5
a/=5 represents a=a/5
a%=5 represents a=a%5
Bitwise and shift operators is not in class 10 syllabus.
19
Precedence of operators:
Order Operator(s)
1 ( ), [ ]
2 ++, --, ~, !
3 *, /, %
4 +, -
5 >>, >>>, <<
6 >, >=, <, <=
7 ==, !=
8 &
9 ^
10 |
11 &&
12 ||
13 ?:
14 =
Literals: Literals are the constants values whose value does not
change during the execution of the program.
Types of literals:
There are six types of literals:
i) Integer literal
ii) Floating/real literal
iii) Character literal
iv) String literal
v) Boolean literal
vi) null literal
Comment: Comment are the part of the program that does not
execute during the execution of the program.
Type of comments:
There are two types of comments:
i) Single line comments
ii) Multiline comments
Primitive data
types
Non-primitive
data types
Integer (byte,
short, int, long)
Floating point
(float, double)
Primitive data types
Character (char)
Boolean (boolean)
Data types
Classes
Non-primitive data
Interfaces
types
Arrays
byte 1 byte
short 2 bytes
int 4 bytes
long 8 bytes
float 4 bytes
double 8 bytes
char 2 bytes
Precision
float 12 digits
double 15 digits
−2𝑛−1 to 2𝑛−1 − 1
26
Bi-default values
byte 0
short 0
int 0
long 0
float 0.0
double 0.0
boolean false
Implicit type
conversion
Explicit type
conversion
27
float double
***
28
CHAPTER 4
MATH CLASS
Introduction to math class:
➢ It belongs to the [Link] package
➢ [Link] is a bi-default package (import of [Link] package is
not required)
➢ It has many functions
Example:
[Link](argument);
[Link](argument);
[Link](argument1, argument2);
[Link]();
[Link]
1) [Link](argument);
This function returns absolute value of a numeric data by ignoring its sign
(+ or -).
[Link](-73); Answer: 73
[Link](+73); Answer: 73
[Link](-73.25); Answer: 73.25
2) [Link](argument);
This function returns the square root of a positive number.
[Link](81); Answer: 9.0
[Link](-81); Answer: no output
[Link](81.9); Answer: 9.049861877399014
3) [Link](argument1, argument2);
This function is used to find the power of numeric data.
[Link](2,3); Answer: 8.0
[Link](-2,3); Answer: -8.0
[Link](2.5,2); Answer: 6.25
29
4) [Link](argument1, argument2);
This function returns largest from two arguments.
[Link](15,20); Answer: 20
[Link](-1.5,1.5); Answer: 1.5
5) [Link](argument1, argument2);
This function returns smallest from two arguments.
[Link](15,20); Answer: 15
[Link](-1.5,1.5); Answer: -1.5
6) [Link](argument);
This function returns the cube root of the method argument.
[Link](125); Answer: 5.0
[Link](-27.0); Answer: -3.0
[Link](10 ); Answer:2.154434690031884
7) [Link](argument);
This function returns the smallest whole number greater than or equal to
the number by rounding up i.e. irrespective of the fractional part.
[Link](15.3); Answer: 16.0
[Link](15.52); Answer: 16.0
[Link](9.01); Answer: 10.0
[Link](-9.01); Answer: -9.0
[Link](10); Answer: 10.0
[Link](-0.03); Answer: -0.0
8) [Link](argument);
This function returns the largest whole number less than or equal to the
number by rounding up i.e. irrespective of the fractional part.
[Link](15.3); Answer: 15.0
[Link](15.52); Answer: 15.0
[Link](9.01); Answer:9.0
[Link](-9.01); Answer: -10.0
[Link](10); Answer: 10.0
[Link](-0.03); Answer: -1.0
30
9) [Link]( );
This function is used to generate random numbers between 0.0 and 1.0 .
Note: - The results of random will vary on every execution.
[Link]( ); Answer: 0.653248765892756
Answer: 0.345798972137946
10) [Link](argument);
This function adds 0.5 to the argument and returns the closest integer
value.
If the argument is not a number, then its returns zero.
If the argument is negative, infinity or less than the MIN_VALUE for
the data type , it returns the MIN_VALUE.
If the argument is positive, infinity or greater than the MAX_VALUE
for the data type , it returns the MAX_VALUE.
Example :-
[Link](1.5); Answer: 2
[Link](-1.5); Answer: -1
[Link]([Link]); Answer: 0
[Link](Float.NEGATIVE_INFINITY);
Answer: -2147483648
[Link](Double.POSITIVE_INFINITY);
Answer: 9223372036854775807
11) [Link](argument);
This function returns truncated value of an argument by rounding off
by 0.5 .
[Link](25.3); Answer: 25.0
[Link](25.5); Answer: 26.0
[Link](30.35); Answer: 30.0
[Link](15.99); Answer: 16.0
12) [Link]
This function returns the value of pie.
[Link]
Answer: 3.141592653589793
Method definition:
The compound body defined by user’s given name consists of a valid set of
statements (Java statements, operators, methods, etc.) within the curly braces to
perform some tasks known as method definition.
<access specifier> <modifier> Function_data_types Function_name (list of
parameters)
Example:
public static void main (int a, int b)
public void main ( )
public int getSum (int n)
Access Specifier:
There are five types of access specifiers:
i. public - access from everywhere
ii. private - access within the same class
iii. protected - access from same package or subclasses
iv. private protected - access only in subclasses
v. default - access only in same package
private has least accessibility and public has most accessibility.
Modifier:
The modifier may be static or final or native or volatile or synchronized.
The static keyword makes a method (or function) as a class function (also
known as static method), these means the function can be called or invoked
or executed through the same class and not through the object of the class.
But the final keyword changes the function into final or constant, thus the
statements of the functions cannot be modified as they become constant.
The return keyword is used to return a value from the function definition to
function statement.
OR
In other words, the return statement transfers the control back to function
call statement.
Example of non-return type function:
• Invoking a Function
To execute all the statements given within the function definition
body by providing actual values to the parameters (if parameters are
given within the brackets) is known as calling function or invoking
function or executing function or accessing function.
Syntax:
object_name.function_name( );
[Link]( );
Actual parameters:
Those parameters which appears in the function call statement are known as
actual parameters.
Example 1:
product (223, 70, 80);
Example 2:
int cd=223, float pr=70, qty=80;
product (cd, pr, qty);
Formal parameters:
Those parameters which appears in the function definition statement are
known as formal parameters.
Example:
public static void product (int code, int float price)
{
set of statements;
}
***
Scanner Class
************************************************************
Introduction to scanner class:
➢ It belongs to the [Link] package
It has many functions:
Let discuss each function with an example:
1. nextByte()
➢ It is a function of byte data type.
2. nextShort()
➢ It is a function of short data type.
3. nextInt()
➢ It is a function of int data type.
4. nextLong()
➢ It is a function of long data type.
5. nextFloat()
➢ It is a function of float data type.
6. nextDouble()
➢ It is a function of double data type.
7. next().charAt(0)
➢ It is a function of char data type.
8. nextBoolean()
➢ It is a function of boolean data type.
9. next()
➢ It is a function of string data type.
➢ It is used to accept string values without space.
[Link]()
➢ It is also a function of string data type.
➢ It is used to accept string values with spaces.
❖ Syntax of object creation:
<class_name> <object_name> = new <class_name>();
Example:
Demo d =new Demo();
Scanner sc=new Scanner([Link]);