You are on page 1of 12

Computer Notes – Class 10

Compiled and edited by – Subhasis Ghosh Updated : August,2022

Chapter – 1 (Rivising Basic Java Concepts)

Features of Object Oriented Programming:


a. Encapsulation – Wrapping up of data and method in a single unit is called encapsulation.
The data members represent the state and the methods represent the behavior.
b. Data Abstraction - It is the act of representing essential features without including the
background details.
c. Inheritance – It is the process by which a class acquires the properties of another class. The
class that inherits the properties is called the sub class or the child class and the class from
which the properties are inherited is called the super class or the parent class.
d. Polymorphism – It means that the same function behaves differently on different situations.
It is implemented through function-overloading.

Concept of Objects

OBJECT :An object is an identifiable entity having two characteristics, i.e. state and behavior. It has
physical existence.
The object is implemented in software terms as follows:
i. Characteristics/attributes are implemented through member variables or data items of
the object.
ii. Behavior is implemented through member functions called methods.
iii. Data and methods are encapsulated into a single unit and given a unique name to give it an
identity.

Relation between Data abstraction and Encapsulation : Data abstraction is the act of representing essential
features without including the background details, where as encapsulation is the act of combining both data
and functions that operates on the data under a single unit. Encapsulation in a way implements abstraction.

Introducing Classes

CLASS : A class represents a set of objects that have a common structure and common behavior. It is an
object maker or object factory. It basically represents abstraction, where characteristics are implemented
through data and behavior is implemented through methods or functions.
1. Objects are the instances of a class. The object represents the abstraction represented by the class in
the real sense.
2. The class is the object factory. It contains all the statements needed to create an object, its attributes
as well as the statements to describe the operation that the object will be able to perform.

Introducing Java
Java is a third generation programming language.
1. Java is both a programming language and a platform (a combination of hardware & software).
2. The program or the code written by a programmer is usually called the source code.
3. The process of converting the source code into machine code is called compilation.
4. Compiler translates the program all at once, but interpreter translates it instruction by instruction.
5. JVM : The java byte code is a machine instruction for a java processor chip called java virtual
machine (JVM). The byte code is independent of the computer system it has to run upon.
6. JVM combined with Java APIs makes Java Platform.
7. The Java API (Application Programming Interface) are libraries of compiled code that can be used in
our programs.

My Program
MyProgram.java MyProgram.class
Complier JVM (running
(source code) (byte code)
application)

Characteristics of JAVA:
a. Write Once Run Anywhere
b. Light Weight Code
c. Security
d. Built-in graphics
e. Object-Oriented Programming
f. Supports Multimedia
g. Platform Independent
h. Open Product
8. Types of Java Programs:
a. Internet Applets – These are small programs that are embedded in web pages and are run on
the viewer’s machine in a secured manner i.e. limited access to system resources i.e. the hard
disk. They are executed by web browsers.
b. Stand-alone Program – An application program that reads from and writs to the files only on
local computers.
c. Servlets – They extend the functionality of web servers.
d. Package – A package is a set of number of classes.

Class as Basis of all Computation Chapter – 2

Character set : Character set is a set of valid characters that a language can recognize. A character
represents any letter, digit or any other sign. Java uses Unicode character set. Unicode is a two-byte
character code set that has characters representing almost all characters in almost all human alphabets and
writing system around the world including English, Arabic, Chinese and many more.

Token : The smallest individual unit in a program is known as a token.


Tokens

Keywords Identifiers Literals Punctuators Operators

a) Keywords – Keywords are the words that convey a special meaning to the language complier. These
are reserved for special purpose and must not be used as identifier names. Eg. class, int, public etc.
b) Identifiers – Identifiers are fundamental building blocks of a program and are used as the general
terminology for the names given to different parts of the program viz. variables, objects, classes,
functions, arrays etc.

Identifier forming rules of Java state the following:


i. Identifiers can have alphabets, digits and underscore and dollar sign characters.
ii. They must not be a reserved keyword or Boolean literal or null-literal.
iii. They must not begin with a digit.
iv. They can be of any length.
v. Java is case-sensitive i.e., upper-case letters and lower-case letters are treated
differently.
c) Literal (often referred to as constants) are data items having fixed data values.

Literals

Floating Boolean Character


Integer (6, 12, 60 ) String ("6", "abc") Null
(25.75) (true/false) ('A', '6')

Hexadecimal
Decimal (6) Octal (06)
(0x6)

d) Punctuators : special symbols like . , ; { } [ ] ( )

e) Operators : operators are special symbols which act in presence of one or more operands.
Arithmetic operators : +-/*% Relational operator : > < >= <= ==
!=
Logical operators : && || ! Assignment operators : =
Increment operator : ++ Decrement operator : --
Shorthand operators : += -= *= /= %=
Unary operator : operator having only one operand. a++, a— (post fix and prefix operators)
Binary operator : operator requires two operands. a+b a-b a/b a*b a%b (all arithmetic
operators)
Ternary operator : operator requires three operands. ( ? : )
c=a>b?a : b
this means:
if (a>b)
c=a;
else
c=b;
Difference between ‘=’ and ‘==’

= ==
It is an assignment operator It is a relational operator
It assigns a value to a variable or a constant It compares two expressions and returns true or
false

• The . Operator : The dot (.) operator accesses instance members of an object or class members of a
class.
• The new operator : The new operator is used to create a new object or a new array. It allocates
memory location for the reference types.

Data Types : Data types are means to identify the type of data and associated operations of handling it.

Type Size
byte 8 bits (1 byte)
short 16 bits (2 byte)
int 32 bits (4 byte)
long 64 bits (8 byte)
float 32 bits (4 byte)
double 64 bits (8 byte)
char 16 bits (2 byte)
boolean Java reserves 8 bits but only
uses 1 bit
• Primitive data types are the fundamental data types of Java. These are in-built data types that serve
as the basic types for the derived or reference types. Java provides eight primitive data types which
are:
byte, short, int, long, float, double, char, boolean
• Reference data types are derived from one or more primitive data types. It is designed by the
programmer and thus it is called user defined data types. One or more than one similar or different
primitive type makes it a composite data type, also called reference data types.
• Variable :A variable is a named memory location, in which a data value of a particular data type can
be stored, modified and updated.
• Constant : A constant is a named memory location which holds a data value of a particular data type
and whose value cannot be changed. The keyword final is used to declare a constant.

Default Values of Variables

Type Initial Value


byte 0 (Zero) of byte type
short 0 (Zero) of short type
int 0
long 0L
float 0.0f
double 0.0d
char Null character i.ie., ‘\u0000’
boolean False
All reference types Null
• Scope of a variable : Scope generally refers to the program region within which a variable is
accessible. Scope of a variable refers to the region of the program within which a variable or a
constant is accessible. Scope of a variable is the region from the declaration of the variable to the end
of the block inside which it is declared.

• Expression : An expression in Java is any valid combination of operators, constants, and variables
i.e., a legal combination of Java tokens.
• Pure Expression : In pure expressions, all the operands are of same type.
• Mixed Expression : In mixed expressions, the operands are of mixed or different data types.

Math functions available through Math Class:

Functions Action
1. sin(x) This function returns the sine of the angle x in radians
2. cos(x) This function returns the cosine of the angle x in radians
3. tan(x) This function returns the tangent of the angle x in radians
4. asin(y) This function returns the angle whose sine is y
5. acos(y) This function returns the angle whose cosine is y
6. atan(y) This function returns the angle whose tangent is y
7. atan2(x,y) This function returns the angle whose tangent is x/y
8. pow(x,y) This function returns x raised to y
9. exp(x) This function returns e raised to x
10. log(x) This function returns the natural algorithm of x
11. sqrt(x) This function returns the square root of x
12. ceil(x) This function returns the smallest whole number greater than or equal
to x (rounded up)
13. floor(x) This function returns the largest whole number less than or equal to x
(rounded down)
14. rint(x) This function returns the truncated value of x
15. abs(a) This function returns the absolute value of a
16. max(a,b) This function returns the maximum of a and b
17. min(a,b) This function returns the minimum of a and b
• Type Conversion : The process of converting one predefined type into another is called Type
Conversion. There are two types of type conversions. They are:
• Implicit type conversion : It is a conversion performed by the compiler without programmer’s
intervention. The Java compiler converts all the operands up to the type of the largest
operand, which is called type promotion. This is also known as coercion. Eg. byte x=12;
int y=x;
• Explicit type conversion : The forceful conversion of one data type into another specific data
type is called Explicit Type casting. The type cast operator is used for this purpose.
Eg. int x=65;
char ch=(char) x;
• There are three types of variables. They are:
a. Instance variable
Variable declared outside the method definition are called instance variable.
Generally they are defined immediately after the first line of the class declaration. An
Instance Variable represents data for which each instance has its own copy.
b. Class variable
A Class Variable exists in a class as a whole. Class Variable exists in only one
location and is globally accessible by all instances of a class. They are also known as
static variable.
c. Local variable
Variable defined within the method are called the local variables. Their scope is
confined to the method in which they are declared.
Eg.
class abc
{
static int p; //class variable
int q; //instance variable

void test( )
{
int a=10; //local variable
:
:
}
}

Decision Making Statements

Nested-if-Statements – It is an if statement which has another if within its body.

1. The selection control statements available in java – if else and switch case.
2. Dangling else statement – In case of nested if else statement, when the number of else statements
are less than the number of if statements then ambiguity may arise because of unmatched number of
if else and this situation is called dangling else problem. To overcome this problem, { } are used.

3. Fall through – The fall of control to the following cases of matching case is called fall through. It
happens in absence of the break statement under the case labels.

4. Differentiate between:

Switch If-else
Switch checks only equality. If else evaluate relational and logical expressions also.

It cannot handle other data types other than ‘int’ and ‘if - else’ can handle all data types.
’char’.
It can compare with a set of constants only. It can have constants as well as variables for
comparisons.

Iteration through Loops

Iteration statement – Statements that allow a group of instructions to be performed repeatedly is known as
iteration statements. ‘for’, ’while’, ’do while’ are the iteration statements provided by JAVA.
1. Entry controlled loop : In entry controlled loop the test expression is given at the beginning of the
loop and hence if the test expression is true, then only the loop body will be executed.
2. Exit controlled loop : In an exit controlled loop, the test expression is given while exiting the loop.
In this case even if the test expression is false at the 1st iteration then also the loop body will be
executed at least once.
3. Empty loop – A loop having no loop body is called as empty loop. Eg.
for (int i=1; i<=10; i++)
{}
4. A set of statements that get executed repetitively in a program is called a loop.
5. A programming statement that allows the repetition of statements based on a condition is called a
looping statement or looping construct.

break continue
Differences
It terminates the iteration and skips rest of the loop It skips the rest of the loop statements and goes to the
body and comes out of the loop. loop expression for the next iteration.
Similarity
Both are jump statements

for while
Differences
Normally for loop is used where the no. of iteration is It can be used for known or unknown no. of iteration.
known.
The initialization of the counter value and update No initialization of the counter variable or update
statement are parts of the for loop declaration. statement can be mentioned with the while loop.
Normally numeric values are used as condition. Any type of condition can be mentioned including
multiple conditions.
Similarity
Both are entry controlled loops.

while Do-while
Entry controlled loop Exit controlled loop
If the condition is false the loop will not be executed at Even if the condition is false, the loop will be executed
all at least once.

Scanner Class

1. A stream is a sequence of characters. Stream supports different kinds of data like simple bytes,
primitive data types and objects. Some streams simply help in flow of data, other transform the data.
2. Scanner is a class in java.util packages used to read input from keyboard, data of various types like
int, double, char etc. This class further converts this input to its binary form.
3. Various methods of the scanner class:

Method Description
next() Returns the next token of any type
nextInt() Returns the next token as an int type value
nextByte() Returns the next token as a byte type value
nextLong() Returns the next token as a long type value
nextShort() Returns the next token as a short type value
nextFloat() Returns the next token as a float value
nextDouble() Returns the next token as a double value
4. The scanner class reads the input and breaks down this input into tokens. A token is a part of input
that is separated from other tokens by separation (white space).
Functions (Methods) Chapter – 5
1. The need for functions or methods are:
a. To cope up with the complexity
b. Hiding details
2. Function Prototype : A function prototype is the first line of the function definition which contains
function name, return type, number and type of parameters and access specifier.
public void test(int x, char y) //function prototype
{
:
:
}
3. A function signature refers to the number and types of arguments passed. It is available with function
prototype.
4. Actual parameters are the parameters that are passed during the method call.
5. Formal parameters are the parameters declared in the function prototype.
6. When a function is called by primitive data types as parameters, it is known as call by value.
7. When a function is called by non-primitive data types as parameters, it is known as call by reference.
8. The function in which the state of the parameter remains unchanged during function call, is known as
pure function. The parameter list may be of primitive or non-primitive data type.
9. The function in which the state of the parameters gets changed during function call is known as
impure function. The parameter list must be of non-primitive data type.
10. A function name having several definitions in the same scope that are differentiable by the number or
types of their arguments, is said to be an overloaded function. Process of creating overloaded
functions is called function overloading. It implements polymorphism.

Constructors Chapter – 6

1. A member function with the same name as its class is called Constructor and it is used to initialize
the data members of the object of that class. They have no return type, not even void.
2. There are two types of constructors:
a. Parameterized constructor
Those constructors which are able to take arguments are called the parameterized constructor.
b. Non- Parameterized constructor
Those constructors which don’t take any arguments are called non- parameterized
constructor.
3. The ‘this’ operator is used to refer to the current object. The ‘this’ keyword is useful in returning
the object (address) of which the function is a member.
4. In Java, the new operator allocates the memory for the objects during the run time of a program.
The memory occupied by these objects is de allocated when no references to the object exist.
Class as User Defined Type Chapter – 7

1. The data types are based on fundamental or primitive data type are known as Composite Data types.
Since these data types are created by users, they are known as User Defined Data types.

Primitive Data types User-defined Data types


These are built in data types. Java provides These are created by the user.
these data types.
The sizes of these data types are fixed. The sizes of these data types are variable as
their sizes depend upon their constituent
members.
These data types are available in all parts of a The availability of these dada types depends
Java program. upon their scope.
2. The dot (.) operator is used to refer to members of an object.
3. Accessor method : An accessor method is a method that returns the value of a data-member of the
class. Mostly accessor methods are provided for private members.
4. Muttator method : A setter method or mutator method is a method that sets/changes the value of a
data-member of the class. Mostly setter methods are provided for private members.
5. Name space collision : when the local variable and the global variable have the same name then the
local variable overrides the global variable. This situation is known as name space collision. To
avoid such situation the ‘this’ operator is used to refer the global variable exclusively.
6. Access specifier : Access specifiers control the access of members of a class within the java
program.
private access specifier : private members are accessible only by the members of the same
class only.
public access specifier : public members are accessible by all members of the same, members
of the other classes within the same package or other packages.
protected access specifier : protected members are accessible by the the members of the
same class and subclasses.
** if no access specifier is mentioned that member is treated as default or friendly.
** here ‘member’ means data members and member methods.
Using Library Classes Chapter – 8

1. Library Classes – Predefined classes of java which gets included in an application program itself are
called library classes. They are built in java classes.
2. Package – Classes are grouped together to make a package. These classes contained in the package
can be used in other program without physically copying them into the program.
3. Wrapper Classes – Wrapper Classes provide the facility to contain primitive data values in terms of
objects. We need a wrapper class for the following reasons:
a. The primitive value can be stored in objects.
b. To provide conversion system from character or string type to another primitive type.

Primitive data types Wrapper class


int Integer
byte Byte
double Double
char Character
boolean Boolean

Wrapper used to convert string to integer : Integer.parseInt( ) eg. int p= Integer.parseInt(“59”);

Wrapper class to convert string to double : Double.parseDouble( ) eg. double q=Double.parseDouble(“25.45”)

1. Exception Handling - Contradictory or unexpected situation or unexpected error, during program


execution, is known as Exception. There are two exception handling blocks : try block and catch
block. The finally block is the optional block.
2. Broadly there are three type of errors:
a. Compile time error – These errors occur during the compilation of the program due to the
violation of the programming language’s grammar rules like the syntactical errors.
b. Run time error – The error that occurs during the run time of the program.
c. Logical error – apparently the program will not generate any syntactical error, but while
execution it does not give desired output.
3. The two blocks used in exception handling are:
a. The try and the catch block
The exception thrown by the try block is handled by the catch block.
b. Finally block is executed in all times, whether try block throws an exception or not. Finally
block is the last block in a try catch construct.

Autoboxing : When a primitive value is directly assigned to compatible wrapper class object, it is known as
autoboxing.

Example : char ch=’a’;


Character obj=ch; //Autoboxing
Unboxing : When a wrapper class object is directly assigned to a primitive type, it is known as unboxing.
Example: int n=10;
Integer obj=n; //Autoboxing
int k=obj; // Unboxing

You might also like