saravanan ganesh - techsparx icse/javanotes 2.0.pdftechsparx computer training center | 9880 205065...
TRANSCRIPT
13
Java Notes 10th ICSE
Saravanan Ganesh
TechSparx Computer Training Center | 9880 205065
1
Java Character Set
Character set is a set of valid characters that a language can recognise. A character represents any
letter, digit or any other sign.
Java uses the 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 systems around the world including English, Arabic, Chinese
and many more.
‘A’ - ‘\u0041’
‘B’ - ‘\u0042’
‘C’ – ‘\u0043’
The first 128 characters in the Unicode character set are identical to the common ASCII character
set.
The second 128 characters are identical to the upper 128 characters of the ISO Latin-1 extended
ASCII character set.
It’s the next 65280 characters that supports characters from other languages.
TechSparx Computer Training Center | 9880 205065
2
Java Token
Java Tokens are basic building block of a Java Program
There are five types of java tokens. They are:
1. Keyword
2. Identifier
3. Literal
4. Separator
5. Operator
Keyword Keyword is a reserved word which conveys a special meaning to the
language compiler.
Examples:
byte if break public
short else continue private
int switch void protected
long case final
float default static
double for volatile
char while class
Boolean do package
Note 1: all the keywords are in lower case.
Note 2: true, false and null are not keywords although they are reserved words.
TechSparx Computer Training Center | 9880 205065
3
Identifier An identifier is the name given to different parts of the program like
variable name, function name, class name, package name, array name
etc.
Rules to form a valid identifier:
1. An identifier should contain only alphabets, digits, underscore and dollar ($) sign. Valid: abc, abc123, abc123_, abc123_$, total_marks Invalid: abc*123, total marks,
2. An identifier should not begin with a digit Valid: abc123abc Invalid: 123abc
3. An identifier is case sensitive abc, Abc, ABc, ABC, aBC, abC
4. An identifier should not be a keyword, true, false or null literals. Valid: number, abc, abc1234, forfor, for123, $$for, PUBLIC Invalid: for, if, while,
5. An identifier can be of any length afjlkdjsalfjlsajflksjaflkjsalkfjdsljflsjalfkjalfwerqrewqrfdsgwsSFDAFDSAFSAwgsrewt Conventions:
1. A class name should be in TitleCase Example: HelloWorld, NumberProgram, Student, CarPool
2. A variable name should be in camelCase. Example: marks, totalMarks, totalMarksOfStudent
3. A function name should be in camelCase. Example: findSum, findDiff, findProduct, calculateAverage
4. Constants must be in upper case and separated by underscore Example: PI_VALUE, TAX_PER, MAX_MARKS Note: final keyword is used to declare a constant. A variable can be converted into a constant by using the final keyword. Ex: final double PI_VALUE = 22.0/7.0;
TechSparx Computer Training Center | 9880 205065
4
Literals
A literal is a constant data item which does not change.
Different kinds of literals
1. Integer Literal: A number with a positive or negative sign without a decimal point is called
an integer Literal
Example: 234, 23432, -324, +342
a. Decimal Integer Literal: An integer literal consisting of a sequence of digits is taken
to be decimal integer literal. Example: 234, 23432, -324, +342
b. Octal Integer Literal: A sequence of digits starting with 0 (digit zero) is taken to be an
octal integer. Example: 012, 077
c. Hexadecimal Integer Literal: A sequence of digits preceded by 0x or 0X is taken to be
an hexadecimal integer. Example: 0x41, 0xAB, 0x123
2. Floating Literal: A number with a decimal point having at least one digit before and after the
decimal point with a positive or negative sign is called as a floating literal.
Example:
Valid: 34.24, 3242.1323, +43.43, -22.53, 0.0
Invalid: .2342, 2342. , 0., .0
a. Fractional Form: A real literal in fractional form must have at least one digit before a
decimal point and at least one digit after the decimal point. It may also have either +
or – sign preceding it. A real literal with no sign is assumed to be positive.
b. Exponential Form: A real literal in exponent form consists of two parts: mantissa and
exponent.
For instance 5.8 * 10205 can be represented as 0.58E206 where 0.58 is mantissa and
206 is exponent.
3. Character Literal: One ASCII character enclosed within a pair of single quotes is called a
character literal.
Example:
Valid: ‘A’, ‘B’, ‘Z’, ‘a’, ‘z’, ‘ ’, ‘#’, ‘!’, ‘@’, ‘$’, ‘%’, ‘^’, ‘3’, ‘0’
Invalid: ‘AB’, ‘a@’, 2, ‘ “ ’, ‘ ’ ’, ‘’, ‘?’
Escape Sequence or backslash constant:
‘\’’, ‘\”’, ‘\?’, ‘\\’,
‘\n’ It’s a new line character – cursor moves to the beginning of next line
‘\t’ It’s a tab space character – cursor moves one tab space
TechSparx Computer Training Center | 9880 205065
5
‘\f’ form feed – cursor moves to the beginning of next page
‘\r’ carriage return – cursor moves to the beginning of the same line
4. String Literal: A group characters enclosed within a pair of double quotes is called as String
Literal
Example:
“Computer”, “Applications”, “”, “Computer Application”,
“[email protected]”, “Comp\tApp”
5. Boolean Literal: These are true or false
6. null literal: null is a literal which is used as default value of reference variable.
Separator
Separators are java tokens used to separate various tokens.
; , { } ( ) [ ]
TechSparx Computer Training Center | 9880 205065
6
Operators
Operators are symbols used to perform various operations with the help of operands.
There two ways Operators can be categorized.
1. Based on number of operands
2. Based on functionality
1. Based on number of Operands:
There are 3 different categories of operators based on the number of Operands. They are:-
a. Unary Operator: An operator which requires only one operand to perform its operation.
Example:
i. Unary Plus (+)
ii. Unary Minus (-)
iii. Increment Operator (++)
iv. Decrement Operator (--)
v. Logical Not operator ( ! )
vi. Bitwise Not operator ( ~ )
b. Binary Operator: An operator which requires two operands to perform its operation.
i. The operand before the operator is called as Left operand
ii. The operand after the operator is called as Right operand
Example: +, -, *, /, %, >, >=, <, <=, ==, !=, &&, ||, &, |, ^, >>, >>>, <<
c. Ternary Operator: An operator which requires three operands to perform its operation.
Example: Conditional Operator ( ? : )
2. Based on the operation:
There are six categories of operators based on the operation. They are:-
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Conditional Operators
5. Assignment Operators
6. Increment & Decrement Operators
7. Bitwise Operators
8. Shift Operators
TechSparx Computer Training Center | 9880 205065
7
Data Type
A data type indicates the nature of data that can be stored.
There are two kinds of Data Types:
1. Primitive Data Type
byte, short, int, long, float, double, char and boolean
2. Reference Data Type
classes, interfaces, arrays
1. Four different kinds of data types to store integer literal
Data Type Size Description Range
byte 1 byte Byte-length integer -128 to + 127
short 2 bytes Short integer -32768 to + 32767
int 4 bytes Integer around -2 billion to + 2 billion ( -231 to + 231 – 1)
long 8 bytes Long integer ( -263 to + 263 – 1)
2. Two different kinds of data types to store floating literal
Data Type Size Description Range Remarks
float 4 bytes Single-precision floating point
-3.4E38 to +3.4E38
Precision up to 6 digits. Examples: currency, temperature, percentage, length.
double 8 bytes Double-precision floating point
-1.7E308 to 1.7E308
Precision upt to 15 digits. Example: Large numbers or high precision, such as for astronomy or subatomic physics.
TechSparx Computer Training Center | 9880 205065
8
3. One data type to store a character literal
Data Type Size Description Range Remarks
char 2 bytes Single characters
0 to 65535 Unicode characters
4. One data type to store a Boolean literal
Data Type Size
Boolean 1 byte but uses only 1 bit
Note1: By default, any integer is of int data type, but if you want to make it a long integer then
postfix the number with L or l.
long a = 3242L;
Note2: By default, any floating-point literal is of double data type, but if you want to make it float,
then postfix the number with F or f.
float b = 3.14f;
Variable
A variable is a named memory location in which data can be
stored.
1. Declaration statement Syntax:
<data_type> <varialb_name> ;
Examples: int a; float b; char c; boolean d;
2. Initialization: Storing the data in a variable
a = 10; b = 5.432f; c = ‘A’; d = true;
TechSparx Computer Training Center | 9880 205065
9
Type Casting
The process of converting from one data type to another data type is
called as type casting.
There are two types of type casting:
1. Implicit Type casting 2. Explicit Type casting
Implicit Type Casting:
This is the process of converting from one data type to another data type
by the compiler without programmer’s intervention.
Also called as COERCION
Example: int a = 55; double b = 45.4; double sum = a + b;
Explicit Type Casting: This is the process of converting from one data type to another data type
by explicitly specifying the data type in the parenthesis just before the
variable or expression
Example:
int numOfBoys = 5; int numOfGirls = 2; double ratio = (double)numOfBoys / (double)numOfGirls
Implicit and Explicit Type Casting in the same expression
Example of both implicit and explicit type casting
int numOfBoys = 5; int numOfGirls = 2; double ratio = (double)numOfBoys/numOfGirls; int numOfBoys = 5; int numOfGirls = 2; double ratio = numOfBoys/(double)numOfGirls; char ch = 'A';
ch = (char)(ch + 1);
TechSparx Computer Training Center | 9880 205065
10
Static
Differences between static variable and non-static variables.
Note: Both static and non-static variables are MEMBER variables, which are defined outside all the
functions but inside the class.
static variable Non-static variables 1 Are also called as class variables Are also called as instance variables
2 A static keyword is used during declaration Example: static int numOfStudents;
A static keyword is NOT used during declaration. Example: int numOfStudents;
3 There will be one copy of a static variable per class.
There will be one copy of an instance variable for every object (or every instance) of the class.
4 static variables can be accessed directly using a class name. Example: Student.numOfStudents
non-static variables can be accessed only by an object of a class. Example: Student stu = new Student();
5 Example: public class Student { String name; int age; double marks; static int numOfStudents; } In the above example numOfStudents is a static variable
Example: public class Student { String name; int age; double marks; static int numOfStudents; } In the above example name, age, marks are non-static variables
TechSparx Computer Training Center | 9880 205065
11
Decision Making Statements – Theory
Syntax What if the body contains only one statement
if ( <expr> ) { st1; st2; … stn; }
if ( <expr> ) st1;
where, if -> is a keyword <expr> -> is a Boolean expression st1, st2 ... stn -> are executable statements
if ( <expr> ) { st1; st2; } else { st3; st4; }
if ( <expr> ) st1; else st2;
where, if, else -> are keywords <expr> -> is a Boolean expression st1, st2, st3, st4 -> are executable statements
TechSparx Computer Training Center | 9880 205065
12
if ( <expr1> ) { st1; st2; } else if ( <expr2 ) { st3; st4; } else if ( <expr3 ) { st5; st6; } else { st7; st8; }
if ( <expr1> ) st1; else if ( <expr2 ) st2; else if ( <expr3 ) st3; else st4;
where, if, else -> are keywords <expr> -> is a Boolean expression st1, st2, st3, st4 ... st8 -> are executable statements
switch ( <expr> ) { case const1 : case const2 : . . . case constn : default: }
Can’t write without curly braces
TechSparx Computer Training Center | 9880 205065
13
Difference between while and do while
while do while 1 Entry controlled loop Exit controlled loop
2 There is no semi colon (;) at the end of while There is a semi-colon (;) at the end of do while
3 The body of the while loop may or may not be executed even once
The body of the do while loop will be executed at least once
4 Syntax: while( <condition> ) { st1; st2; . . st n; }
Syntax: do { st1; st2; . . st n; } while( <condition> );
TechSparx Computer Training Center | 9880 205065
14
ASCII Table
TechSparx Computer Training Center | 9880 205065
15
Range for unsigned numbers
Number of bits(n)
Possible values (Binary)
Possible values (Decimal)
Range General Formulae
1 0 1
0 1
0 to 1 0 to 2n-1
2 00 01 10 11
0 1 2 3
0 to 3 0 to 2n-1
3 000 001 010 011 100 101 110 111
0 1 2 3 4 5 6 7
0 to 7 0 to 2n-1
4 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 to 15 0 to 2n-1
5 00000 . . 11111
0 . . 31
0 to 31 0 to 25-1
6 000000 . . 111111
0 . . 63
0 to 63 0 to 26-1
7 0000000 . . 1111111
0 . . 127
0 to 127 0 to 27-1
TechSparx Computer Training Center | 9880 205065
16
8 00000000 . . 11111111
0 . . 255
0 to 255 0 to 28-1
16 0000000000000000 . . . 1111111111111111
0 . . . 65535
0 to 65535 0 to 216-1
Range for Signed Number
byte 8 -128 to 127 -2n-1 to +2n-1-1 -28-1 to +28-1-1 -27 to +27-1
short 16 -32768 to +32767 -216-1 to +216-1-1 -215 to +215-1
int 32 (around) -2 billion to + 2 billion
-232-1 to +232-1-1 -231 to +231-1
long 64 -10E18 to +10E18 -264-1 to +264-1-1 -263 to +263-1
TechSparx Computer Training Center | 9880 205065
17
Expressions
An expression can be either a Pure Expression, mixed Expression or Boolean Expression.
1. Pure Expression: When all the operands and the constants in an expression are of same data
type, i.e., either integers or floating-point literals then that kind of expression is called as
pure Expression.
int a, b, c;
c = a + b;
2. Mixed expression: When an expression contains operands and constants of various data
types, then they are called mixed expression.
int a, b;
double area;
area = a * b;
3. Boolean Expression: The expressions that result into false or true are called Boolean
expressions. The Boolean expressions are combination of constants, variables, logical and
relational operators.
(a + b) > c && (c+ d) > a