Computer Notes
Computer Notes
Procedure Oriented languages lay emphasis on Object Oriented languages lay emphasis on
functions and procedures rather than data. data rather than functions.
Limited and difficult code reusability leading to Versatile and easy code reusability leading
lengthy programs which are difficult to debug and to simpler programs which are easier to
maintain. debug and maintain.
8. The process of converting one predefined type into another is called type conversion. In an
implicit conversion, the result of a mixed mode expression is obtained in the higher most data type
of the variables without any intervention by the user. For example:
int a = 10;
float b = 25.5f;
c = a + b;
In case of explicit type conversion, the data gets converted to a type as specified by the programmer.
For example:
int a = 10;
double b = 25.5;
float c = (float)(a + b);
10. In static initialization, the initial value of the variable is provided as a literal at the time of
declaration. For example:
int mathScore = 100;
double p = 1.4142135;
char ch = 'A';
In dynamic initialization, the initial value of the variable is the result of an expression or the
return value of a method call. Dynamic initialization happens at runtime. For example:
int a = 4;
int b = Math.sqrt(a);
double x = 3.14159, y = 1.4142135;
double z = x + y;
In static initialization, the initial value of the variable is provided as a literal at the time of
declaration. For example: int mathScore = 100; double p = 1.4142135; char ch = 'A'; In dynamic
initialization, the initial value of the variable is the result of an expression or the return value of a
method call. Dynamic initialization happens at runtime. For example: int a = 4; int b =
Math.sqrt(a); double x = 3.14159, y = 1.4142135; double z = x + y;
12.(a) int m =155;
This assignment is correct as 155 is an integer literal and it is assigned to an int variable m.
(b) float f = 0.002654132; This assignment is incorrect as data type of 0.002654132 is double but
it is assigned to a float variable. The correct assignment will be float f = 0.002654132f;
(c) String str = 'Computer'; This assignment is incorrect as the String literal Computer is
enclosed in single quotes. It should be in double quotes. The correct assignment will be String str =
"Computer";
(d) boolean p = false; This assignment is correct as false is a valid boolean literal and it is
assigned to a boolean variable.
(e) String b = "true"; This assignment is correct as "true" is a string literal not a boolean literal
as it is enclosed in double quotes. It is correctly assigned to a String variable.
(f) char ch = "apps"; This assignment is incorrect as "apps" is a string literal not a character
literal and it is assigned to a variable ch of char data type.
(g) String st= "Application";
This assignment is correct as "Application" is a string literal and it is correctly assigned to a
String variable.
(h) double n = 455.29044125;
This assignment is correct as 455.29044125 is a literal of double data type and it is correctly
assigned to a double variable.
Operators in Java
V.1. An operator is a symbol or sign used to specify an operation to be performed in Java
programming.
The three main types of operators are Arithmetical, Logical and Relational. An operator is a
symbol or sign used to specify an operation to be performed in Java programming.
The three main types of operators are Arithmetical, Logical and Relational.
2.An expression is a set of variables, constants and operators i.e. an expression is a combination of
operators and operands. When an expression is assigned to a variable, the complete set is referred
to as a statement.
3.a. Arithmetic operators are used to perform mathematical operations on its operands. Operands of
arithmetic operators must be of numeric type. A few arithmetic operators operate upon one
operand. They are called Unary Arithmetic operators. Other arithmetic operators operate upon two
operands. They are called Binary Arithmetic operators.
As an example, consider the below statement:
int a = 10 + 20;
Here, the addition arithmetic operator, represented by the symbol + will add 10 and 20. So variable
a will be 30.
b. Relational operators are used to determine the relationship between the operands. Relational
operators compare their operands to check if the operands are equal to ( == ), not equal to ( != ), less
than ( < ), less than equal to ( <= ), greater than ( > ), greater than equal to ( >= ) each other. The
result of an operation involving relation operators is a boolean value — true or false.
Example:
int a = 8;
int b = 10;
boolean c = a < b;
Here, as a is less than b so the result of a < b is true. Hence, boolean variable c becomes true.
c. Logical operators operate on boolean expressions to combine the results of these boolean
expression into a single boolean value.
Example:
int a = 7;
int b = 10;
boolean c = a < b && a % 2 == 0;
Here, the result of first boolean expression a < b is true and the result of second boolean expression
a % 2 is false. The logical AND operator ( && ) combines these true and false boolean values and
gives a resultant boolean value as false. So, boolean variable c becomes false.
d. condition ? expression 1 : expression 2
Ternary operator evaluates the condition. If the condition is true then result of ternary operator is
the value of expression 1. Otherwise, the result is the value of expression 2.
Example:
boolean isLeapYear = true;
int febDays = isLeapYear ? 29 : 28;
Here, the ternary operator checks if the value of boolean variable isLeapYear is true or false. As it
is true, expression 1, which in this example is the value 29, is the result of the ternary operator. So,
int variable febDays becomes 29.
4.a. d.
c. b.
5.a. b.
Introduction to Java
IV.4.a. A set of statements written in a High-Level programming language like Java, C++, Python,
etc. is called as Source Code.
b. Machine code is a low-level programming language. Instructions in machine code are
written as a sequence of 0s and 1s. It is directly understood and executed by the processor.
c. Java compiler converts Java source code into an intermediate binary code called Bytecode.
Bytecode can't be executed directly on the processor. It needs to be converted into Machine Code
first.
5. BlueJ is an integrated development environment for Java. It was created for teaching Object
Oriented programming to computer science students.
Features of BlueJ are:
1. Simple beginner friendly graphical user interface.
2. It allows creating objects of the class dynamically, invoking their methods and also supplying
data to the method arguments if present.
3. It supports syntax highlighting. (Syntax highlighting means showing the different tokens of the
program like keywords, variables, separators, etc. in different colours so that they show up more
clearly.)
4. It facilitates easier debugging as lines causing compilation errors are marked clearly and the
error is displayed at the bottom of the window.
5. It provides a code editor, compiler and debugger integrated into a single tool.
6. We commonly use two output statements in Java. There syntax are:
1. System.out.println(); — This statement prints data on the console. The data to be printed is
passed to the println method as a String. After printing the string, it places the cursor at the start
of the next line. So, the next printing happens at the start of the next line.
2. System.out.print(); — This statement also prints data on the console. The data to be printed is
passed to the print method as a String. After printing the string, the cursor remains on the same
line at the end of the printed string. So, the next printing starts in the same line just after the end
of the previous printed string.
As an example, the below statements will generate the following output:
System.out.println("JVM stands for");
System.out.print("Java ");
System.out.print("Virtual ");
System.out.print("Machine");
Output
JVM stands for
Java Virtual Machine
7. In Java, a reserved word is a word that has a predefined meaning in the language. Due to this,
reserved words can’t be used as names for variables, methods, classes or anya System.out.println()
System.out.print() It prints data to the console and places the cursor in the next line. It prints data
to the console but the cursor remains at the end of the data in the same line. Next printing takes
place from next line. Next printing takes place from the same line. ny other identifier. other
identifier. Reserved words are also known as keywords. Five commonly used Java reserved words
are:
1. public
2. class
3. int
4. double
5. char
9. Java compiler compiles Java source code to Bytecode. Bytecode cannot run on the processor
directly as processor only understands Machine Code. Java Virtual Machine (JVM) takes this
Bytecode as input and converts it into Machine Code line by line. So, JVM acts as an interpreter
for converting Bytecode to Machine Code. In this way, a Java program uses both a Compiler as well
as an Interpreter to get executed on the processor.
8.
Input in Java
IV.2. In Java, a package is used to group related classes. Packages are of 2 types:
1. Built-In packages — These are provided by Java API
2. User-Defined packages — These are created by the programmers to efficiently structure their
code.
java.util, java.lang are a couple of examples of built-in packages.
import keyword is used to import built-in and user-defined packages into our Java program.
4.Errors that occur during the execution of the program primarily due to the state of the program
which can only be resolved at runtime are called Runtime errors.
Consider the below example:
import java.util.Scanner;
class RunTimeError {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
int result = 100 / n;
System.out.println("Result = " + result);
}
}
This program will work fine for all nonzero values of n entered by the user. When the user enters
zero, a run-time error will occur as the program is trying to perform an illegal mathematical
operation of division by 0. When we are compiling the program, we cannot say if division by 0
error will occur or not. It entirely depends on the state of the program at run-time.
6.
Library Classes
VII.1. In Java, a package is used to group related classes. Packages are of 2 types:
1. Built-In packages — These are provided by Java API
2. User-Defined packages — These are created by the programmers to efficiently structure their
codes.
java.util.,java.lang are examples of built-in packages.
2.The asterisk(*) sign indicates that all the classes in the imported package can be used in the
program.
3. Wrapper classes wrap the value of a primitive type in an object. Wrapper classes are present in
java.lang package. The different wrapper classes provided by Java are Boolean, Byte, Integer,
Float, Character, Short, Long and Double.
4.a. b. c.
Arrays
VI.1. 6. 3.
2. 4. 8.
5. 7.
String Handling
VI.1. 2.
3. 4.
VII.1. An exception is an event, which occurs during the execution of a program, that disrupts the
normal flow of the program's instructions. Two exception handling blocks are try and catch.
2.a. It returns the index within the string of the first occurrence of the specified character or - 1 if
the character is not present. Its return type is int.
b. It compares two strings lexicographically. It results in the difference of the ASCII codes of the
corresponding characters. Its return type is int.
5. endsWith() tests if the string object ends with the string specified as its argument. startsWith()
tests if the string object starts with the string specified as its argument.
Consider the below example:
public class Example {
public static void main(String args[]) {
String str = "ICSE Computer Applications";
System.out.println("Does " + str + " starts with ICSE? " + str.startsWith("ICSE"));
System.out.println("Does " + str + " ends with tions? " + str.endsWith("tions"));
}
}
Here, both str.startsWith("ICSE") and str.endsWith("tions") returns true as str starts with "ICSE"
and ends with "tions".
VIII.1. It converts a string into upper case characters. If any character is already in uppercase or is
a special character then it will remain same.
Syntax:
String <variable-name>= <string-variable>.toUpperCase();
2. It removes all leading and trailing space from the string.
Syntax:
String <variable-name>= <string-variable>.trim();
3. It converts a string into lowercase characters. If any character is already in lowercase or is a
special character then it will remain same.
Syntax:
String <variable-name>= <string-variable>.toLowerCase();
4. It returns the length of the string i.e. the number of characters present in the string.
Syntax:
int <variable-name>= <string-variable>.length();
5. It replaces a character with another character or a substring with another substring at all its
occurrences in the given string.
Syntax:
String <variable-name>= <string-variable>.replace(, );
6. It compares two strings lexicographically. It results in the difference of the ASCII codes of the
corresponding characters. Its return type is int.
Syntax:
int <variable-name>= <string-variable>.compareTo();
7. It is a method of StringBuffer class and it is used to reverse the sequence of characters.
Syntax:
<StringBuffer-Variable>.reverse();
8. It returns the index of the first occurrence of the specified character within the string or -1 if
the character is not present.
Syntax:
int <variable-name> = <string-variable>.indexOf(<character>);
9. It tests if the string object starts with the string specified as its argument.
Syntax:
boolean <variable-name> = <string-variable>.startsWith(<string>);
10. It ignores the case of the characters and checks if the contents of two strings are same or not.
Syntax:
boolean <variable-name> = <string-variable>.equalsIgnoreCase(<string>);
18. A method can have multiple return statements but only one of them will be expected because
once a return statement is executed, the program control moves back to the caller method skipping
the remaining statements of the current method.
Constructors
V.1. A constructor is a member method having the same name as that of a class and is used to
initialise the instance variables of the objects. It is invoked at the time of creating any object of the
class. For example:
Employee emp = new Employee();
Here, Employee() is invoking a default constructor.
5. Constructors are special member methods of the class. Objects are non-primitive data types so
they are passed by reference and not by value to constructors. If objects were passed by value to a
constructor then to copy the objects from actual arguments to formal arguments, Java would again
invoke the constructor. This would lead to an endless circular loop of constructor calls.
10. The first statement abc p = new abc(); is calling a non-parameterised constructor to create and
initialize an object p of class abc. The second statement abc p = new abc(5,7,9); is calling a
parameterised constructor which accepts three arguments to create and initialize an object p of
class abc.
6. 8.