0% found this document useful (0 votes)
4 views28 pages

Java Class 10th Chapter 1

The document is a comprehensive revision guide for ICSE Class X Java, covering key concepts of Procedure Oriented Programming (POP) and Object Oriented Programming (OOP). It details characteristics, advantages, and differences between POP and OOP, as well as fundamental principles of OOP such as encapsulation, inheritance, and polymorphism. Additionally, it includes practical examples, class definitions, and a guess paper for exam preparation.

Uploaded by

azhaanansari9411
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views28 pages

Java Class 10th Chapter 1

The document is a comprehensive revision guide for ICSE Class X Java, covering key concepts of Procedure Oriented Programming (POP) and Object Oriented Programming (OOP). It details characteristics, advantages, and differences between POP and OOP, as well as fundamental principles of OOP such as encapsulation, inheritance, and polymorphism. Additionally, it includes practical examples, class definitions, and a guess paper for exam preparation.

Uploaded by

azhaanansari9411
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Sachin Tripathi ICSE Class X – Java: Revision of Class IX

Page 1 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

Table of Contents
Table of Contents ................................................................................................................................................ 2
1.1 Procedure Oriented Programming (POP) .................................................................................................... 4
Characteristics of POP..................................................................................................................................... 4
Limitations of POP .......................................................................................................................................... 4
1.2 Object Oriented Programming (OOP) ......................................................................................................... 5
Characteristics of OOP .................................................................................................................................... 5
Advantages of OOP ......................................................................................................................................... 5
1.3 Difference between Procedure Oriented and Object Oriented Programming .......................................... 6
1.4 Elements and Principles of OOP .................................................................................................................. 7
1. Object .......................................................................................................................................................... 7
2. Class ............................................................................................................................................................. 7
3. Data Abstraction ......................................................................................................................................... 7
4. Encapsulation (Data Hiding) ....................................................................................................................... 7
5. Inheritance .................................................................................................................................................. 7
6. Polymorphism ............................................................................................................................................. 7
7. Dynamic Binding (Late Binding) ................................................................................................................. 7
8. Message Passing ......................................................................................................................................... 7
1.5 Class as an Object Factory ........................................................................................................................... 9
1.6 Class as a User-defined Data Type ............................................................................................................ 10
1.7 Message Passing between Objects ........................................................................................................... 11
Steps involved in message passing .............................................................................................................. 11
General syntax of a message ........................................................................................................................ 11
1.8 Introduction to Java ................................................................................................................................... 12
Important Features (Buzzwords) of Java ..................................................................................................... 12
JVM, JRE and JDK .......................................................................................................................................... 12
How a Java Program Executes ...................................................................................................................... 12
1.9 Introduction to BlueJ ................................................................................................................................. 13
Features of BlueJ ........................................................................................................................................... 13
Main Components of the BlueJ Interface .................................................................................................... 13
Steps to Work in BlueJ .................................................................................................................................. 13
1.10 Character Sets .......................................................................................................................................... 14
Types of Character Sets ................................................................................................................................ 14
1.11 Escape Sequence ...................................................................................................................................... 15
Commonly used Escape Sequences in Java.................................................................................................. 15
1.12 Tokens ...................................................................................................................................................... 16
Types of Tokens in Java ................................................................................................................................ 16
Rules for naming an Identifier ...................................................................................................................... 16
Some Reserved Keywords of Java ................................................................................................................ 16
Types of Literals ............................................................................................................................................ 16

Page 2 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.13 Data Types in Java.................................................................................................................................... 17


Types of Data Types ...................................................................................................................................... 17
The 8 Primitive Data Types — Size and Range............................................................................................. 17
1.14 Variables................................................................................................................................................... 19
Syntax of Declaration ................................................................................................................................... 19
Rules for naming a Variable ......................................................................................................................... 19
Types of Variables (based on scope) ............................................................................................................ 19
1.15 Constants.................................................................................................................................................. 20
Types of Constants in Java............................................................................................................................ 20
Declaring a Symbolic Constant — the ‘final’ keyword ................................................................................ 20
1.16 Type Conversion....................................................................................................................................... 21
Types of Type Conversion ............................................................................................................................. 21
1.17 Forms of Operators .................................................................................................................................. 22
Types (Forms) of Operators based on the number of operands ................................................................. 22
1.18 Types of Operators .................................................................................................................................. 23
(a) Arithmetic Operators .............................................................................................................................. 23
(b) Relational (Comparison) Operators ........................................................................................................ 23
(c) Logical Operators ..................................................................................................................................... 23
(d) Assignment Operators ............................................................................................................................ 23
(e) Increment and Decrement Operators .................................................................................................... 23
(f) Conditional (Ternary) Operator ............................................................................................................... 24
(g) Bitwise Operators .................................................................................................................................... 24
(h) instanceof Operator ................................................................................................................................ 24
1.19 Precedence and Associativity of Operators ............................................................................................ 25
Precedence Table (High to Low) ................................................................................................................... 25
Evaluating an Expression Step-by-Step ........................................................................................................ 25
1.20 The println() and print() Methods ........................................................................................................... 26
Guess Paper – 1 ................................................................................................................................................. 27
Section A — Answer all questions (2 marks each)....................................................................................... 27
Section B — Attempt any four questions (5 marks each) ........................................................................... 27
Guess Paper – 2 ................................................................................................................................................. 28
Section A — Answer all questions (2 marks each)....................................................................................... 28
Section B — Attempt any four questions (5 marks each) ........................................................................... 28

Page 3 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.1 Procedure Oriented Programming (POP)


Definition: Procedure Oriented Programming is a programming paradigm in which a large program is divided
into smaller, manageable parts called procedures or functions. Each function performs a specific task, and the
program is designed around “what is to be done” rather than around the data.

In POP, importance is given to functions/procedures rather than to the data on which they operate. Data is
usually kept in global variables that can be freely accessed and modified by any function in the program.

Characteristics of POP
● Emphasis is on the algorithm / sequence of steps needed to solve a problem.
● Large programs are divided into smaller programs called functions.
● Most functions share global data, i.e. data moves freely around the system from function to function.
● Follows a top-down approach in program design (the problem is broken into sub-problems repeatedly).
● Data is not secure, since it is global and can be changed by any function accidentally.
● Examples of POP languages: C, Pascal, FORTRAN, COBOL, BASIC.
Limitations of POP
● Data is exposed to the entire program — no data hiding, so it is insecure.
● Difficult to relate real-world entities/objects to the program (does not model the real world naturally).
● Adding new data and functions is difficult without affecting existing code.
● Overuse of global data makes debugging and maintenance harder as the program grows.
● Does not support the concept of reusability of code through inheritance.

Exam Tip: Remember POP with the phrase “Functions over Data, Global Data, Top-Down” — this single line
answers most 2-mark POP questions.

Page 4 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.2 Object Oriented Programming (OOP)


Definition: Object Oriented Programming is a programming paradigm based on the concept of “objects”,
where an object is a self-contained unit that combines data (attributes/fields) and the functions (methods)
that operate on that data into a single unit called a class.

Unlike POP, OOP gives importance to data rather than procedures. Data is hidden inside objects and can be
accessed only through the object’s own methods, which makes the data secure.

Characteristics of OOP
● Emphasis is on data rather than on procedure.
● Programs are divided into objects, and each object contains both data and functions that act on that
data.
● Data is hidden inside objects (encapsulated) and cannot be accessed by outside functions directly.
● Objects communicate with each other through message passing (calling one another’s methods).
● Follows a bottom-up approach in program design (objects are identified first, then combined to build
the whole program).
● New data and functions can be added easily whenever required, without disturbing existing code.
● Supports the four pillars: Encapsulation, Abstraction, Inheritance and Polymorphism.
● Examples of OOP languages: Java, C++, Python, C#, BlueJ (built on Java).
Advantages of OOP
● Data security through encapsulation and data hiding.
● Code reusability through inheritance — saves development time.
● Easier to maintain, modify and debug because objects are independent units.
● Models real-world entities more naturally (e.g. a Student, a Car, a BankAccount can each be an object).
● Problems can be divided into smaller, self-contained pieces called objects.

Page 5 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.3 Difference between Procedure Oriented and Object Oriented


Programming
Basis Procedure Oriented Programming (POP) Object Oriented Programming (OOP)
Emphasis Gives importance to functions/procedures. Gives importance to data.
Approach Follows a top-down approach. Follows a bottom-up approach.
Data moves freely from function to Data is hidden and bound together with
Data movement
function; mostly global. functions inside an object.
Data is secure due to data
Security Data is insecure (no data hiding).
hiding/encapsulation.
Structure Program is divided into small functions. Program is divided into objects.
Functions communicate via Objects communicate via message
Communication
parameters/global data. passing.
Reusability Little scope for code reuse. Code is reused through inheritance.
Functions/operators can be overloaded
Overloading Not supported.
(polymorphism).
Examples C, Pascal, FORTRAN, COBOL. Java, C++, Python, C#.

Page 6 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.4 Elements and Principles of OOP


The entire object oriented paradigm rests on a set of core elements/principles. Each of these is individually
important for board examination — they are frequently asked as 1-2 mark “define/name” questions.

1. Object
An object is a real-world entity or a basic run-time unit in an OOP system. It is an instance of a class and consists
of: (i) State — represented by the data/attributes of the object, and (ii) Behaviour — represented by the
methods/functions of the object. Example: “student1” is an object of the class Student, having state such as
name and rollNo, and behaviour such as calculateGrade().

2. Class
A class is a blueprint, template or a user-defined data type from which objects are created. It defines the data
members (fields) and member functions (methods) that its objects will have. A class itself does not occupy
memory for data until an object is created from it.

3. Data Abstraction
Data Abstraction refers to displaying only the essential features of an object to the outside world while hiding
the background details/implementation. For example, a person driving a car knows how to use the accelerator
and brake (essential features) without knowing the internal mechanism of the engine (implementation
details).

4. Encapsulation (Data Hiding)


Encapsulation is the wrapping up of data and the methods that act on that data into a single unit called a class,
so that the data cannot be accessed directly from outside the class. Access to the data is only possible through
the class’s own methods, which protects the data from accidental modification. In Java this is achieved by
declaring data members as private and providing public getter/setter methods.

5. Inheritance
Inheritance is the mechanism by which a new class (called the derived/child/sub class) acquires the properties
(fields) and behaviours (methods) of an existing class (called the base/parent/super class). It supports the
concept of reusability — common features are written once in the parent class and reused by every child class.
Example: a class Vehicle can be the parent of child classes Car and Bike.

6. Polymorphism
Polymorphism means “many forms” — the ability of a message/function to behave differently depending on
the context in which it is used. In Java, it is mainly implemented through: (i) Method Overloading — same
method name with different parameter lists in the same class (compile-time/static polymorphism), and (ii)
Method Overriding — a child class provides a specific implementation of a method already defined in its parent
class (run-time/dynamic polymorphism).

7. Dynamic Binding (Late Binding)


Dynamic binding means that the code to be executed in response to a function call is decided at run time
rather than at compile time, depending on the actual type of the object. This allows a single interface to be
used for different underlying forms (a key part of run-time polymorphism).

8. Message Passing
Objects communicate with one another by sending and receiving information, called messages. A message is
essentially a request from one object asking another object to execute one of its methods. Message passing

Page 7 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

involves: creating classes, creating objects from the classes, and establishing communication among the
objects.

Exam Tip: A very common board question is: “State any two/four principles of OOP.” Be ready to write the
name PLUS a one-line definition for each — not the name alone.

Page 8 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.5 Class as an Object Factory


A class acts like a “factory” that manufactures objects. Just as a factory has a design/mould according to which
many identical products are manufactured, a class provides a single definition (of fields and methods) using
which any number of objects can be created — each object having its own copy of the fields but sharing the
same method definitions.

Analogy: Consider the class Car as a factory design. Using this one design, we can “manufacture” (instantiate)
many actual car objects such as car1, car2 and car3 — each having its own colour and registration number (its
own state), but all following the same blueprint (the same class).

Example — a class acting as an object factory:

class Car
{
String colour;
String regNo;

void display()
{
[Link]("Colour: " + colour);
[Link]("Reg. No: " + regNo);
}
}

class TestCar
{
public static void main(String args[])
{
Car car1 = new Car(); // object 1 manufactured from the class
Car car2 = new Car(); // object 2 manufactured from the class

[Link] = "Red";
[Link] = "UP32-AB-1234";

[Link] = "Blue";
[Link] = "UP32-CD-5678";

[Link]();
[Link]();
}
}

Output:
Colour: Red
Reg. No: UP32-AB-1234
Colour: Blue
Reg. No: UP32-CD-5678
Notice that only one class (Car) was written, but it acted as a factory to produce two independent objects (car1
and car2), each with its own data.

Page 9 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.6 Class as a User-defined Data Type


Java provides built-in (primitive) data types such as int, float, char and boolean to store simple values.
However, real-world entities like a Student, an Employee or a BankAccount cannot be represented by a single
primitive value — they need several related pieces of data grouped together. A class allows the programmer
to create such a new, user-defined data type.

Once a class is defined, its name can be used exactly like a built-in data type to declare variables (which are
then called objects/reference variables) that can store the composite data described by that class.

Example — class used as a user-defined data type:

class Student // Student is now a new (user-defined) data type


{
String name;
int rollNo;
double marks;
}

class TestStudent
{
public static void main(String args[])
{
Student s1; // s1 declared, just like "int x;"
s1 = new Student(); // memory allocated to the object

[Link] = "Aarav";
[Link] = 12;
[Link] = 92.5;

[Link]([Link] + " " + [Link] + " " + [Link]);


}
}

Output:
Aarav 12 92.5
Here, just as “int” is a built-in type used to declare variables like “int x;”, “Student” is a user-defined type used
to declare the reference variable “Student s1;”. This is why a class is described as a user-defined data type /
extensible data type.

Page 10 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.7 Message Passing between Objects


Definition: Message passing is the process by which objects of a program interact and communicate with each
other. Sending a message to an object means requesting that object to invoke (call) one of its methods, usually
with some data (arguments).

Steps involved in message passing


● Step 1: Creating classes that define objects and their behaviour.
● Step 2: Creating objects from the classes.
● Step 3: Establishing communication among objects, i.e. one object calling a method of another object.
General syntax of a message

[Link](argumentList);

This statement is itself a message being sent to “objectName”, asking it to execute “methodName” using the
given arguments.

Example — one object sending a message to another:

class Calculator
{
int add(int a, int b)
{
return a + b;
}
}

class TestMessage
{
public static void main(String args[])
{
Calculator calc = new Calculator(); // object created
int result = [Link](15, 20); // message passed to "calc"
[Link]("Sum = " + result);
}
}

Output:
Sum = 35
In this example, the object “calc” of class Calculator receives the message add(15, 20) from the main() method,
executes it, and returns the result 35, which demonstrates communication between the caller and the object.

Page 11 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.8 Introduction to Java


Definition: Java is a high-level, object-oriented, platform-independent programming language developed by
James Gosling and his team (the “Green Team”) at Sun Microsystems. It was originally called “Oak” and was
later renamed “Java”. Java was released to the public in 1995. Sun Microsystems was acquired by Oracle
Corporation in 2010, which now owns and maintains Java.

Important Features (Buzzwords) of Java


● Simple: Java syntax is based on C++ but removes complex features like pointers and multiple
inheritance, making it easier to learn and use.
● Object Oriented: Almost everything in Java is treated as an object; it supports encapsulation,
inheritance and polymorphism.
● Platform Independent: Java source code is compiled into an intermediate form called bytecode, which
can run on any machine having a Java Virtual Machine (JVM), regardless of the underlying
hardware/OS. This is Java’s famous WORA principle — “Write Once, Run Anywhere”.
● Secure: Java programs run inside a virtual machine “sandbox” and do not use explicit pointers, which
protects the system from malicious code.
● Robust: Java has strong memory management, automatic garbage collection and exception handling,
which reduces the chances of program crashes.
● Multithreaded: Java can perform several tasks simultaneously within the same program using threads.
● Architecture Neutral / Portable: Bytecode is not tied to any particular processor architecture, so the
same .class file runs identically on any platform with a JVM.
● High Performance: Bytecode is very close to native machine code and is converted using a Just-In-Time
(JIT) compiler for fast execution.
● Dynamic: Java programs carry a lot of run-time information that can be used to verify and resolve
access to objects at run time.
JVM, JRE and JDK
● JVM (Java Virtual Machine): An abstract machine that provides a run-time environment in which Java
bytecode can be executed. It is JVM that makes Java platform independent, since a different JVM is
implemented for each platform, but all of them can run the same bytecode.
● JRE (Java Runtime Environment): A software package that provides the JVM, core class libraries and
other supporting files needed to run (but not compile) Java programs.
● JDK (Java Development Kit): A complete software development kit that includes the JRE plus
development tools such as the compiler (javac) and debugger, required to write, compile and run Java
programs.
Relationship: JDK ⊃ JRE ⊃ JVM (the JDK contains the JRE, and the JRE contains the JVM).

How a Java Program Executes


● The programmer writes source code and saves it with a “.java” extension.
● The javac compiler compiles the source code into bytecode, saved with a “.class” extension.
● The JVM interprets/executes this platform-independent bytecode, converting it to native machine
code for the specific computer, and the program runs.

Exam Tip: Whenever a question asks “why is Java called platform independent”, always mention bytecode +
JVM together — mentioning only one of the two loses marks.

Page 12 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.9 Introduction to BlueJ


Definition: BlueJ is an Integrated Development Environment (IDE) designed specially for teaching and learning
object-oriented programming using Java. It was developed at Monash University and the University of Kent,
and is widely used because it lets students create objects and directly test/interact with them without writing
a main() method.

Features of BlueJ
● Provides a simple and clean interface, ideal for beginners.
● Displays classes and their relationships visually through a UML-style class diagram.
● Objects can be created interactively on the “object bench” and their methods can be invoked directly
by right-clicking, without writing a main() method.
● Provides an inbuilt code editor, compiler and a terminal/console window for output.
● Allows step-by-step testing of individual methods, which is extremely useful for learning.
Main Components of the BlueJ Interface
Component Purpose
Contains menus such as Project, Edit, Tools, View, Help for various
Menu Bar
operations.
Provides quick-access buttons for common operations like Compile and
Tool Bar
New Class.
Shows all classes of the project as boxes, along with arrows depicting
Class Diagram (workspace)
relationships (e.g. inheritance).
The area at the bottom where created objects appear as red/orange
Object Bench
object icons, ready to be tested.
A separate console window that displays output produced by
Terminal Window
[Link]()/print() statements.
Opens when a class is double-clicked; used to write/edit the Java
Code Editor
source code of that class.
Steps to Work in BlueJ
● Create a new BlueJ project (Project → New Project).
● Create a new class (click “New Class” icon, provide a class name).
● Double-click the class icon to open the code editor and write the Java code.
● Click “Compile”; a red-striped icon turns plain once compilation succeeds.
● Right-click the class icon on the diagram and choose “new ClassName()” to create an object; give it an
object name.
● The new object appears on the object bench; right-click it and select a method to invoke (test) it.
● Output/results appear in the Terminal Window.

Page 13 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.10 Character Sets


Definition: A character set (charset) is the full collection of valid characters that a programming language can
recognise and use to write a program. A character can be a letter, digit, special symbol, or a blank space.

Types of Character Sets


● ASCII (American Standard Code for Information Interchange): A 7-bit / 8-bit character encoding that
can represent 128 (or 256 in extended ASCII) characters — sufficient mainly for English letters, digits
and common symbols.
● ISO-Latin-1: An 8-bit character set that extends ASCII to represent 256 characters, covering many
Western European languages.
● Unicode: A 16-bit universal character encoding that can represent 65,536 distinct characters, covering
almost all the world’s written languages (English, Hindi, Chinese, Arabic, etc.) along with mathematical
and other symbols.

Exam Tip: Java uses the Unicode character set (16-bit) internally for the char data type — this single fact is a
favourite board fill-in-the-blank/one-word question.

Page 14 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.11 Escape Sequence


Definition: An escape sequence is a combination of a backslash (\) followed by a character (or digits) that has
a special, predefined meaning to the compiler. It is used to represent non-graphic (non-printable) characters,
such as a new line or a tab, inside character or string literals.

Even though an escape sequence is written using two or more characters, the compiler treats the whole
sequence as a single character.

Commonly used Escape Sequences in Java


Escape Sequence Meaning
\n Newline — moves the cursor to the beginning of the next line.
\t Horizontal tab — inserts a tab space.
\b Backspace — deletes the character before it.
\r Carriage return — moves the cursor to the beginning of the current line.
\f Form feed — moves the cursor to the start of the next logical page.
\' Single quote — inserts a literal single-quote character.
\" Double quote — inserts a literal double-quote character.
\\ Backslash — inserts a literal backslash character.
\ddd Octal character — represents a character using 1 to 3 octal digits.
\uxxxx Unicode character — represents a character using 4 hexadecimal digits.
Example — use of escape sequences:

class EscapeDemo
{
public static void main(String args[])
{
[Link]("Rahul says, \"Java is fun!\"");
[Link]("Name\tAge\tCity");
[Link]("Aman\t15\tLucknow");
[Link]("Line1\nLine2");
}
}

Output:
Rahul says, "Java is fun!"
Name Age City
Aman 15 Lucknow
Line1
Line2

Page 15 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.12 Tokens
Definition: A token is the smallest individual unit of a Java program that is meaningful to the compiler. Every
Java program is essentially a sequence of tokens, separated by whitespace, punctuation or operators.

Types of Tokens in Java


● Keywords: Reserved words that have a special, predefined meaning in Java and cannot be used as
identifiers (e.g. class, void, static, int, if, else, for, while, new, this, public, private).
● Identifiers: Names given by the programmer to variables, classes, methods and objects (e.g. rollNo,
Student, calculateArea).
● Literals (Constants): Fixed values that do not change during program execution, such as 25, 3.14, 'A',
"Hello", true.
● Punctuators (Separators): Symbols used to separate parts of the program, such as ; , . ( ) { } [ ].
● Operators: Symbols that perform operations on operands, such as + – * / % = == && ||.
Rules for naming an Identifier
● It can contain letters (a-z, A-Z), digits (0-9), underscore (_) and dollar sign ($).
● It must not begin with a digit; it can begin with a letter, underscore or $.
● It cannot be a reserved keyword (e.g. “class” or “int” cannot be used as identifiers).
● It cannot contain spaces or special symbols (other than _ and $).
● Java is case-sensitive, so “marksTotal” and “MarksTotal” are treated as two different identifiers.
● There is no limit on the length of an identifier, but it should be meaningful.
Some Reserved Keywords of Java
abstract, boolean, break, byte, case, catch, char, class, continue, default, do, double, else, extends, final,
finally, float, for, if, implements, import, instanceof, int, interface, long, native, new, package, private,
protected, public, return, short, static, super, switch, synchronized, this, throw, throws, transient, try, void,
volatile, while.

Types of Literals
Literal Type Example
Integer literal 10, -25, 0, 1000
Floating-point literal 3.14, -0.5, 2.0f
Character literal 'A', 'z', '5', '$'
String literal "Hello World", "ICSE"
Boolean literal true, false
Null literal null

Page 16 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.13 Data Types in Java


Definition: A data type specifies the type and size of the data that a variable can hold, along with the
operations that can be performed on it. Java is a strongly typed language — every variable must be declared
with a specific data type before it is used.

Types of Data Types


Java’s data types are broadly classified into two categories:

● 1. Primitive (Fundamental) Data Types: Predefined by the language; they store a single simple value
directly in memory. There are 8 primitive data types — byte, short, int, long, float, double, char and
boolean.
● 2. Non-Primitive (Reference/Derived) Data Types: Created by the programmer or provided by
libraries; they store the address (reference) of the object rather than the value itself. Examples —
String, Array, Class, Interface.
The 8 Primitive Data Types — Size and Range
Data Type Size Default Value Range / Values
byte 1 byte (8 bit) 0 –128 to 127
short 2 bytes (16 bit) 0 –32,768 to 32,767
int 4 bytes (32 bit) 0 –2,147,483,648 to 2,147,483,647
–9,223,372,036,854,775,808 to
long 8 bytes (64 bit) 0L
9,223,372,036,854,775,807
float 4 bytes (32 bit) 0.0f Single precision decimal (≈ 6–7 digits precision)
double 8 bytes (64 bit) 0.0d Double precision decimal (≈ 15 digits precision)
char 2 bytes (16 bit) '\u0000' 0 to 65,535 (single Unicode character)
boolean 1 bit (conceptual) false true or false only

Page 17 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

Example — declaring variables of different primitive data types:

class DataTypeDemo
{
public static void main(String args[])
{
byte b = 100;
int age = 15;
long population = 1400000000L;
float pi_val = 3.14f;
double area = 154.532;
char grade = 'A';
boolean pass = true;

[Link]("Byte : " + b);


[Link]("Int : " + age);
[Link]("Long : " + population);
[Link]("Float : " + pi_val);
[Link]("Double : " + area);
[Link]("Char : " + grade);
[Link]("Boolean : " + pass);
}
}

Output:
Byte : 100
Int : 15
Long : 1400000000
Float : 3.14
Double : 154.532
Char : A
Boolean : true

Page 18 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.14 Variables
Definition: A variable is a named memory location used to store a data value that can change (vary) during
the execution of a program. Every variable in Java must be declared with a data type before it is used.

Syntax of Declaration

dataType variableName; // declaration


dataType variableName = value; // declaration with initialisation

// examples
int rollNo;
char grade = 'A';

Rules for naming a Variable


● Must begin with a letter, underscore (_) or dollar sign ($) — never with a digit.
● Can contain letters, digits, underscore and $ after the first character.
● Cannot be a Java reserved keyword.
● Cannot contain spaces or special symbols such as @, #, %, etc.
● Is case-sensitive (“num” and “Num” are different variables).
● Should be meaningful and, by convention, written in camelCase (e.g. totalMarks, studentName).
Types of Variables (based on scope)
Type Description
Declared inside a class but outside any method; each object of the class gets
Instance Variable
its own separate copy. Declared without the ‘static’ keyword.
Declared with the ‘static’ keyword inside a class; a single copy is shared by all
Static (Class) Variable
objects of that class.
Declared inside a method, constructor or block; it exists only within that
Local Variable
method/block and must be initialised before use.
Example — instance, static and local variables together:

class VariableDemo
{
int rollNo; // instance variable
static String school = "St. Xavier's"; // static (class) variable
void show() {
int marks = 88; // local variable
[Link]("Roll No : " + rollNo);
[Link]("School : " + school);
[Link]("Marks : " + marks);
}
public static void main(String args[]) {
VariableDemo obj = new VariableDemo();
[Link] = 21;
[Link]();
}
}

Output:
Roll No : 21
School : St. Xavier's
Marks : 88

Page 19 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.15 Constants
Definition: A constant is a fixed value that does not change during the execution of a program. Unlike a
variable, once a constant is set, it cannot be modified.

Types of Constants in Java


● Integer Constants: Whole numbers without a decimal point. Can be expressed as Decimal (base 10,
e.g. 25), Octal (base 8, prefixed with 0, e.g. 031) or Hexadecimal (base 16, prefixed with 0x, e.g. 0x1F).
● Real (Floating-point) Constants: Numbers containing a decimal point or expressed in exponential form,
e.g. 3.14, 0.5, 2.5e3.
● Character Constants: A single character enclosed within single quotes, e.g. 'A', '9', '$'.
● String Constants: A sequence of characters enclosed within double quotes, e.g. "ICSE Board".
● Backslash (Escape Sequence) Character Constants: Special non-printable character constants formed
with a backslash, e.g. '\n', '\t' (see Section 1.11).
Declaring a Symbolic Constant — the ‘final’ keyword
In Java, a named/symbolic constant is created using the keyword ‘final’. Once assigned, the value of a final
variable cannot be changed anywhere else in the program; attempting to do so causes a compile-time error.

class ConstantDemo
{
public static void main(String args[])
{
final double PI = 3.14159; // symbolic constant
final int MAX_MARKS = 100;

double radius = 7;
double area = PI * radius * radius;

[Link]("Area of circle = " + area);


[Link]("Maximum marks = " + MAX_MARKS);
// PI = 3.14; // ERROR: cannot assign a value to final variable PI
}
}

Output:
Area of circle = 153.93791
Maximum marks = 100

Page 20 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.16 Type Conversion


Definition: Type conversion is the process of converting the value of one data type into another data type.
Java supports two kinds of type conversion.

Types of Type Conversion


(a) Implicit Type Conversion (Widening / Automatic Conversion)
This happens automatically when a value of a “smaller” data type is assigned to a variable of a “larger” data
type. It is done by the compiler itself and no data is lost.
Widening order (small to large): byte → short → int → long → float → double. (char also converts automatically
to int.)
class ImplicitDemo
{
public static void main(String args[])
{
int num = 10;
double result = num; // int automatically converted to double
[Link]("Result = " + result);
}
}

Output:
Result = 10.0
(b) Explicit Type Conversion (Narrowing / Type Casting)
This is done manually by the programmer when converting a “larger” data type into a “smaller” one. Since
some information may be lost in this process, the programmer must explicitly write the target type in
parentheses before the value — this is called type casting.

dataType variableName = (dataType) value;

Example — explicit type conversion (narrowing):

class ExplicitDemo
{
public static void main(String args[])
{
double price = 99.99;
int roundedPrice = (int) price; // explicit
casting; decimal part is lost

[Link]("Original price : " + price);


[Link]("After casting : " +
roundedPrice);
}
}

Output:
Original price : 99.99
After casting : 99

Basis Implicit Conversion (Widening) Explicit Conversion (Narrowing)


Done by Automatically by the compiler. Manually by the programmer using a cast operator.
Direction Smaller type → larger type. Larger type → smaller type.
Data loss No loss of data/precision. Possible loss of data (decimal part or magnitude).

Page 21 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.17 Forms of Operators


Definition: An operator is a symbol that instructs the compiler to perform a specific mathematical, relational
or logical operation on one or more operands. Based on the number of operands they act upon, operators are
classified into three forms.

Types (Forms) of Operators based on the number of operands


Number of
Form Description Example
Operands
Unary Operator One Acts on a single operand. -a, ++a, a--, !flag
Binary Operator Two Acts on two operands. a + b, a > b, a && b
Acts on three operands; Java has
Ternary Operator Three exactly one ternary operator, the condition ? value1 : value2
conditional operator ‘?:’.
Example — all three forms used together:

class OperatorFormsDemo
{
public static void main(String args[])
{
int a = 8, b = 3;

int unaryResult = -a; // unary operator


int binaryResult = a + b; // binary operator
String result = (a > b) ? "a is greater" : "b is greater"; // ternary

[Link]("Unary (-a) : " + unaryResult);


[Link]("Binary (a+b) : " + binaryResult);
[Link]("Ternary : " + result);
}
}

Output:
Unary (-a) : -8
Binary (a+b) : 11
Ternary : a is greater

Page 22 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.18 Types of Operators


Based on the kind of operation they perform, Java operators are classified as follows.

(a) Arithmetic Operators


Operator Meaning Example (a=10, b=3)
+ Addition a + b → 13
– Subtraction a–b→7
* Multiplication a * b → 30
/ Division (quotient) a/b→3
% Modulus (remainder) a%b→1
(b) Relational (Comparison) Operators
Compare two operands and return a boolean result (true or false).

Operator Meaning Example (a=10, b=3)


== Equal to a == b → false
!= Not equal to a != b → true
> Greater than a > b → true
< Less than a < b → false
>= Greater than or equal to a >= b → true
<= Less than or equal to a <= b → false
(c) Logical Operators
Combine two or more boolean (relational) expressions.

Operator Meaning Example


&& Logical AND — true only if both operands are true. (a>5 && b<5) → true
|| Logical OR — true if at least one operand is true. (a>5 || b>5) → true
! Logical NOT — reverses the boolean value. !(a>5) → false
(d) Assignment Operators
Operator Example Equivalent To
= a=5 assigns 5 to a
+= a += 5 a=a+5
-= a -= 5 a=a–5
*= a *= 5 a=a*5
/= a /= 5 a=a/5
%= a %= 5 a=a%5
(e) Increment and Decrement Operators
Unary operators that increase or decrease the value of a variable by 1.

Page 23 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

Operator Name Behaviour


Value is increased first, then used in the
++x (pre-increment) Increment before use
expression.
Value is used in the expression first, then
x++ (post-increment) Increment after use
increased.
Value is decreased first, then used in the
--x (pre-decrement) Decrement before use
expression.
Value is used in the expression first, then
x-- (post-decrement) Decrement after use
decreased.
Example — pre vs post increment:

class IncrDemo
{
public static void main(String args[])
{
int x = 5;
[Link](x++); // prints 5, then x becomes 6
[Link](x); // prints 6
[Link](++x); // x becomes 7, then prints 7
}
}

Output:
5
6
7

(f) Conditional (Ternary) Operator


Syntax: variable = (condition) ? valueIfTrue : valueIfFalse; — a compact shorthand for a simple if-else
statement.

(g) Bitwise Operators


Operate on the individual bits of integer operands.

Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise complement (NOT)
<< Left shift
>> Right shift
(h) instanceof Operator
A special binary operator used to test whether an object is an instance of a particular class (or its subclass).
Returns a boolean value. Syntax: objectName instanceof ClassName

Page 24 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.19 Precedence and Associativity of Operators


Operator Precedence: Operator precedence determines the order in which different operators in an
expression are evaluated when the expression contains more than one type of operator. Operators with higher
precedence are evaluated before operators with lower precedence.

Operator Associativity: When an expression contains two or more operators of the same precedence,
associativity determines the order (Left-to-Right or Right-to-Left) in which they are evaluated.

Precedence Table (High to Low)


Precedence Operator(s) Associativity
1 (Highest) () [] . (postfix ++ / --) Left to Right
2 ++ -- (unary prefix), + – (unary), ! ~ Right to Left
3 */% Left to Right
4 + – (binary, addition/subtraction) Left to Right
5 << >> >>> Left to Right
6 < <= > >= instanceof Left to Right
7 == != Left to Right
8 & (bitwise AND) Left to Right
9 ^ (bitwise XOR) Left to Right
10 | (bitwise OR) Left to Right
11 && Left to Right
12 || Left to Right
13 ?: (ternary) Right to Left
14 (Lowest) = += -= *= /= %= Right to Left
Evaluating an Expression Step-by-Step
Example: Evaluate 20 + 5 * 2 – 3

● Step 1: * has higher precedence than + and –, so 5 * 2 = 10 is done first.


● Step 2: Expression becomes 20 + 10 – 3.
● Step 3: + and – have equal precedence, so evaluate Left to Right: 20 + 10 = 30, then 30 – 3 = 27.
● Final Result = 27
Verification through code:

class PrecedenceDemo
{
public static void main(String args[])
{
int result = 20 + 5 * 2 - 3;
[Link]("Result = " + result);
}
}

Output:
Result = 27

Page 25 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

1.20 The println() and print() Methods


Java provides two commonly used methods, both belonging to the object ‘[Link]’ (which represents the
standard output stream, usually the screen), to display output.

Basis print() println()


Full form [Link]() [Link]() (‘ln’ = line)
Newline after Does NOT move the cursor to a new line Moves the cursor to the beginning of the
output after printing. next line after printing.
Effect on next
Next output continues on the SAME line. Next output starts on a NEW line.
output
All primitive types, String and Object (and
Overloaded for All primitive types, String and Object. can also be called with no arguments to
print just a blank line).
Example — print() vs println():

class PrintDemo
{
public static void main(String args[])
{
[Link]("Hello ");
[Link]("World"); // no newline, continues on same line
[Link](); // just moves to next line

[Link]("ICSE");
[Link]("Board"); // starts on a fresh line each time
}
}

Output:
Hello World
ICSE
Board
Both methods are overloaded, i.e. they can accept an argument of any data type (int, double, char, boolean,
String, or even an object) and will automatically convert it to its String representation before displaying it.
Example — println() with different data types:

class PrintlnOverload
{
public static void main(String args[])
{
[Link](101); // int
[Link](3.14); // double
[Link]('A'); // char
[Link](true); // boolean
[Link]("Result: " + (5 + 3)); // String concatenation
}
}

Output:
101
3.14
A
true
Result: 8

Page 26 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

Guess Paper – 1
Chapter 1: Revision of Class IX | Max. Marks: 40 | Time: 1 hour

Section A — Answer all questions (2 marks each)


Q1. Define Object Oriented Programming. Name any two languages that support OOP. [2]
Q2. State any two differences between Procedure Oriented Programming and Object Oriented
Programming. [2]
Q3. What is meant by ‘Data Encapsulation’? How is it achieved in a Java program? [2]
Q4. Why is a class called a ‘user-defined data type’? Explain with one example. [2]
Q5. What is meant by ‘message passing’ between objects? Name the three steps involved. [2]
Q6. Name the character set used internally by Java. How many characters can it represent? [2]
Q7. What is an escape sequence? Give the meaning of \t and \b. [2]
Q8. Define a token. Name the five types of tokens available in Java. [2]
Q9. Differentiate between Instance variables and Local variables. [2]
Q10. What is type casting? Give one example each of implicit and explicit type conversion. [2]
Q11. Name the four forms/types of operators based on the number of operands they use, with one example
each. [2]
Q12. Differentiate between the print() and println() methods of Java, with an example. [2]
Q13. What do you understand by the terms JVM and JDK? [2]
Q14. Rewrite the following using the ternary operator: if (marks >= 40) result = "Pass"; else result = "Fail";
[2]

Q15. State the data type and size (in bytes) of: (a) char (b) long (c) boolean (d) double. [2]

Section B — Attempt any four questions (5 marks each)


Q16. Explain, with the help of a suitable example, any four principles/elements of OOP (Object, Class,
Inheritance, Polymorphism, Encapsulation — choose any four). [5]
Q17. Explain the different components of the BlueJ IDE interface. Also list the steps to create and test an
object in BlueJ. [5]
(a) Study the following expression and evaluate it, showing the precedence order used at each step:
10 + 6 / 2 * 3 – 4 % 3
(b) Convert the following into Java code using appropriate variable declarations, and print the output: A
student’s roll number is 21, name is “Kavya” and marks obtained is 88.5 out of 100.
Q19. What will be the output of the following Java code segment? Justify your answer. [5]

int a = 5, b = 2;
[Link](a + b + "Result");
[Link]("Result" + a + b);
[Link](a++ + " " + ++b);

Q20. Write short notes on (any five): Keywords, Identifiers, Literals, final keyword, Unicode, Type casting.
[5]

Page 27 of 28
Sachin Tripathi ICSE Class X – Java: Revision of Class IX

Guess Paper – 2
Chapter 1: Revision of Class IX | Max. Marks: 40 | Time: 1 hour

Section A — Answer all questions (2 marks each)


Q1. What is Procedure Oriented Programming? State one limitation of this approach. [2]
Q2. Define the terms ‘Object’ and ‘Class’, bringing out the relationship between them. [2]
Q3. What is polymorphism? Name its two forms in Java. [2]
Q4. Explain ‘Data Abstraction’ with a real-life example. [2]
Q5. Who developed Java, and in which year was it released to the public? [2]
Q6. State any two features (buzzwords) of Java with a one-line explanation each. [2]
Q7. What is meant by ‘platform independence’ of Java? Which two components make this possible? [2]
Q8. What is the object bench in BlueJ used for? [2]
Q9. Give the escape sequence used for: (a) new line (b) double quote inside a string. [2]
Q10. What are literals? Give one example each of an integer literal and a character literal. [2]
Q11. Differentiate between Implicit and Explicit type conversion. [2]
Q12. What is the final keyword used for in Java? Give an example. [2]
Q13. Name the operator used to test whether an object belongs to a particular class. Give its syntax. [2]
Q14. State the associativity of the assignment operator (=) and the arithmetic operators (*, /). [2]
Q15. What output will the following statement produce? [Link]("5" + 2 + 3); [2]

Section B — Attempt any four questions (5 marks each)


Q16. Draw a comparison table showing at least five differences between Procedure Oriented Programming
and Object Oriented Programming. [5]
Q17. Explain the terms JVM, JRE and JDK. How is a Java program compiled and executed? Describe briefly.
[5]

Q18. Evaluate the following expression step by step, clearly stating the precedence rule applied at each step:
[undefined]

15 % 4 + 3 * 2 – 1
Q19. Write a Java program (class with a main method) that declares an int, a double, a char and a boolean
variable, initialises them with suitable values, and prints all four using println(). Show the expected output.
[5]

Q20. What will be the output of the following code? Explain your reasoning for each line. [5]

int p = 4, q = 4;
[Link](p++ + q);
[Link](p + ++q);
[Link](p + " " + q);

Page 28 of 28

You might also like