Java Icse Notes For Classes 9th and 10th
Java Icse Notes For Classes 9th and 10th
5,6
{
Output 5+6
}
For now , objects can be considered as an entity with some data and some certain actions
to be performed on data !
Objects are blueprint of Class !
i.e class is the layout of an object !
Classes :-
It is the entity that binds the data and functions performed on data in a single
independent entity !
Objects:-
They are the real entities and are exact copy of the class .It is through objects that we
manipulate the data/functions of the class.
1.Encapsulation
The binding and wrapping up of data and the function performed on data in a single unit called class is
known as encapsulation.
It is enabled by the keyword 'class'
2.Abstraction
The act of representing only essential features without including the background detials
3.Inheritence
When properties of 1 class are taken (inherited) from another class
The class that takes the properties are called as child/derived/sub - class
The class that gives the properties are called as parent/base/super - class
4.Polymorphism
Single entity, many forms.
One function behaves differently for different objects
Compiler
It executes the whole program at once
Interpreter
It executes the whole program line by line
Multi-threading:-
Program is partitioned into modules which reduces the complexity of program
and creates a well defined boundey for the programs.
Secure
There is no direct access to the ram , in between lies the JVM
Typed-language
Each variable is specified with the type of data it will hold
1.Keywords
What are tokens ?
These are words that convey special meaning to the compiler
These are 57 in number
The smallest individual unit in a java program.
They include true , false , null.
They are of 5 types :-
1.Keyword
2.Identifier
3.Literals 2.Identifiers & Literals
4.Punctuators
5.Operators Identifiers are named memory location used to store temporary
constants.(literals)
3.Punctuators
4.Operators
Character Set
A set of graphical and textual symbol each of which is mapped to a positive integer.
A -> 65
Character Set in Java
Set of alphabets , letters (including special characters)that are valid in Java language.
Base 10 / decimal
A character preceded by \ (backslash) is known as escape sequence and hold a special meaning to the compiler.
Java has a total of 8 escape sequences.
They are used to print non graphical characters.
\\ -> \
\\ -> \
Questions:-
Output when :-
\\\" --> \"
\" --> "
\a --> a
Data Types
Integer float double Char Boolean Note: they are referential data type i.e
instead of storing the value of the literal
they store the location of the literal.
Write a program(WAP) to assign two numbers and then calculate their sum,difference,product,division in sepeate
variables and then print them .
datatype variable=value;
Each line is terminated by a semicolon
Name of the class should start with a alphabet, can be folllowed by number
Assignment vs declaration.
class mathoperation
{
public static void main(String at[]) PUBLIC STATIC VOID MAIN
{
int num1=5,num2=6;
Int num1=5;
int sum=num1 + num2; 11
int num1; --> declaration
int diff=num1 - num2; num1=5; --> assignment
double div=num1/num2;
"" -> string -> sequence charatcers
System.out.print("Sum= "+sum); "HELLO WORLD"
System.out.println("Difference= "+diff); 'a' -> character
System.out.println("Product= "+product);
System.out.println("Division= "+div);
}
}
1.Arithmetic Operators
2.Assingment Operators
3.Unary Operators They also have a special type called as short-hand operators
4.Comparison/Relational Operators
5.Logical Operators
6.Bitwise Operators
7.Miscleneous Operators
1.Arithemetic Operators
4.Comparison/Relational Operators
3.Unary operators
Number % 2 = 1
False || true
Used to determine logic between variable/values. Only conditional operator that takes 3 operands.
Its and replacement for if-then-else
Symbol--> ? :
Egs:-
num1 = 10;
num2 = 20;
res=(num1>num2) ? (num1+num2):(num1-num2)
Since num1<num2,
the second operation is performed
Certain operators have higher precedence than others and therefore precedence of operator decides how an expression
should be evaluated.
ICSE 2015
b + c -> 5 6 + -- postfix
Here b and c are the operands and + is the operator.
+ 5 6 -- prefix
Now we can put this + operator in 3 ways
1. +bc
2. b+c
3. bc+
Noticed how the operator was put at different locations but all means the same.
a=5; //5
a=a++;
A //6
v=2 + 0 + 3 + 4; v=9
Implicit
byte -> short -> char -> int -> long -> float -> double
Double a=45;
Explicit
a=66; a=45.0
double -> float -> long -> int -> char -> short -> byte
char ch=(char)a;
Double a=45.5;
Egs:-
Int num=(int)a;
double a=45.5;
Num=45;
a= int b=(int)a; --> 45
double a=45;
Int a='A';
int a='A';
Int a=66;
char ch=(char)a;
Ch-> B
4.2 -> 4
4.6->5
Math.rint(5.5) -> 6.0 QUES 2. WAP TO ASSIGN TWO NUMBERS AND SWAP THEM USING A THIRD VARIABLE !
QUES3. WAP TO ASSIGN TWO NUMBERS AND PRINT THE GREATER AND SMALLER OUT OF THEM WITH
56.99 APPROPRIATE MESSAGE !
Floor->56.0
Ceil->57.0
1.Sequential
They are of 3 types
Program is execute line by line i.e a
1. Sequential
sequence is followed.
2. Selection
3. Iteration
2.Selection
Different part of the code are
executed on different condition
3.Iteration
A specific part of code is run again
and again till a condition is
satisfied.
If you want to interept the flow of program and terminate it then you can use System.exit(0)
If only one statement has to be written in the if block then curly braces are optional.
i.e If curly braces are not their then if only consider its next statement under it's block.
We can add an else block to the if statement that runs if the if statement is false.
Dangling if problem
The break statements break the flow of switch block and shifts the
control to the line following the switch statement.
Default can be written anywhere within the switch case i.e it's not
compulsory to write it at last.
Use of break :-
If we don't put break statement after every case then all the cases
following the matched case will be executed unless break statement is
not found or switch case ends.this is also called as fall-through
conditon in switch
They are also called Menu driven as out of several options only one is
selected and executed.
Streams means flow of data Buffer memory : a temporary memory where data is stored before sending to ram
They are of 3 types Java uses 2 types of stream for input/output operations.
1. Input Stream :- flow of data ready for input
2. Output Stream :- flow of data ready for output
3. Error Stream :- flow of data ready to throw error Character Stream Byte Stream
Performs operation on unicode system Performs operation on 8-bit system
Conversion of data is not required Conversion of data is required
Does not requires buffer memory Requires buffer memory
Egs:- Egs:-
DataInputStream InputStreamReader , BufferedReader
Notice the util class and no throws exception after the main function
An operation performed by the user that results in abnormal working of the program RUN TIME ERROR
They are of 3 types Errors that occur during the execution of the program.
1. Run time error Egs:- Math.sqrt(-4);
2. Syntax error
3. Logical error SYNTAX ERROR
Errors that occur during the compilation of the program
Egs:- int x;y;
Logical error
Error that produces different output / unexpected output.
Egs: using a > b instead of b >a
Phase 1 Phase 2
Break Continue
They are of 2 types It terminates the current It skips the current
1. Break iteration of the loop and iteration of the loop
2. Continue shifts the control to the and shifts the control
statement following the to the next iteration
loop.
Infite loop
String Functions
A sequence of character is called as string.
Egs:- "SKY IS BLUE" String is a class so it contains many functions to perform common operations on string
It's denoted by "". String always start from 0
It's default value is null
.length() .charAt(index)
It is used to compute the length of the string. It is used to read and return character at a
Egs:- position
Int l=s.length(); Egs:-
l = 11 char c=s.charAt(5);
Length is the only string function that starts c='S'
string from 1.
.indexOf(character/string) .lastIndexOf(character)
It is used to find the first position of the It is used to find the last position of
character/string from the start.If the the character/string.
character is not found it returns -1 If character not found it returns -1.
Egs:- Egs:-
int l=s.indexOf('S'); int l=s.lastIndexOf('S');
l=0 l=5
int p=s.indexOf('A');
p=-1
int q=s.indexOf("IS");
q=3
k=s.substring(5);
k="S BLUE"
k=s.substring(5,10);
k="S BLU"
k=s.toLowerCase();
k="sky is blue";
.equals(String)
Checks wether 2 strings are equal or not .isEmpty()
.equalsIgnoreCase(String) To check wether a string is empty or not
Checks wether 2 strings are equal or not ignoring the case boolean b=s.isEmpty();
b=false
.contains(String)
To check wether a string contains a substring or not =null;
="";
.concat(String)
To add 2 strings
k=s.concat("2nd String");
k=SKY IS BLUE2nd String
A alternate to s+"2nd String"
.valueOf(any type)
Returns the data In string form
When any string is passed to the string tokenizer , it breaks the string into tokens , each token is a word of the string.
It has 3 functions
1. st.countTokens() :- to count the number of tokens
2. st..nextToken() :- to extract the next token from the string tokenizer
3. .hasMoreTokens() :- to check if next token is available or not
Strings are immutable that is they can't be changes once they are created so to make them mutable we use StringBuffer
Printing a 2d array
We use methods to make our code modular (i.e divide the code into pieces) and for reusability.
Output:
Static variable/method is a
variable/method that belongs to the
class (not to any particular object)
It is shared across all the objects
Pure methods :
Those who don't change the state of the variable
Impure Methods :
Those who change the state of the variable.
Default constructor
It is a constructor that is created automatically by the compiler and it initialises the values to the data
members , if we have not created a constructor.
Parametersied Constructor
A constructors with parameters.
Non-paramterseid Constructor
A constructor with no
paramaters
Wrapper class is a class whose object wraps the primitive data type.
Value of primitive type in object form
Boxing / Unboxing
The conversion of wrapper class back to it's primitive form is known as unboxing / boxing.
Autoboxing
The automatic converison the Java compiler makes from the primitive type to their respective wrapper class
In this we are creating an object of type Integer and passing 5 as primtive data type
Interger number=new Integer(5);
In this case 5 would automatically be coverted into Integer obejct, this is called as autobxing
Converts from String to respective primtive data type Each of the wrapper class contains an constant named MAX_VALUE and MIN_VALUE
They store the maximum and shortest range of that data type
Egs:-
Double.MAX_VALUE
.toString()
Method in character type
Converts an object to string
.valueOf()
Converts an string s into the respective wrapper class.( not primitive type as in case of .parse