You are on page 1of 18

Java Notes

10th ICSE
Saravanan Ganesh

13
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


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


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


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:

‘\’’, ‘\”’, ‘\?’, ‘\\’,


4
‘\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


‘\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”,
“tech253545@gmail.com”, “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


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


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- -3.4E38 to Precision up to
precision +3.4E38 6 digits.
floating point Examples:
currency,
temperature,
percentage,
length.
double 8 bytes Double- -1.7E308 to Precision upt
precision 1.7E308 to 15 digits.
floating point Example:
Large numbers
or high
precision, such
as for
astronomy or
subatomic
physics.
7

TechSparx Computer Training Center | 9880 205065


3. One data type to store a character literal

Data Type Size Description Range Remarks


char 2 bytes Single 0 to 65535 Unicode
characters 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;
8

TechSparx Computer Training Center | 9880 205065


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';
9 ch = (char)(ch + 1);

TechSparx Computer Training Center | 9880 205065


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 A static keyword is NOT used during declaration.
Example: Example:
static int numOfStudents; int numOfStudents;

3 There will be one copy of a static variable There will be one copy of an instance variable for
per class. every object (or every instance) of the class.
4 static variables can be accessed directly non-static variables can be accessed only by an
using a class name. object of a class.
Example: Example:
Student.numOfStudents Student stu = new Student();

5 Example: Example:

public class Student public class Student


{ {
String name; String name;
int age; int age;
double marks; double marks;
static int numOfStudents; static int numOfStudents;
} }

In the above example numOfStudents is a In the above example name, age, marks are non-
static variable static variables

10

TechSparx Computer Training Center | 9880 205065


Decision Making Statements – Theory

Syntax What if the body contains only


one statement

if ( <expr> ) if ( <expr> ) where,


{ st1; if -> is a keyword
st1; <expr> -> is a Boolean expression
st2; st1, st2 ... stn -> are executable
… statements
stn;
}

if ( <expr> ) if ( <expr> ) where,


{ st1; if, else -> are keywords
st1; else <expr> -> is a Boolean expression
st2; st2; st1, st2, st3, st4 -> are executable
} statements
else
{
st3;
st4;
}

11

TechSparx Computer Training Center | 9880 205065


if ( <expr1> ) if ( <expr1> ) where,
{ st1; if, else -> are keywords
st1; else if ( <expr2 ) <expr> -> is a Boolean expression
st2; st2; st1, st2, st3, st4 ... st8 -> are
} else if ( <expr3 ) executable statements
else if ( <expr2 ) st3;
{ else
st3; st4;
st4;
}
else if ( <expr3 )
{
st5;
st6;
}
else
{
st7;
st8;
}

switch ( <expr> ) Can’t write without curly braces


{
case const1 :

case const2 :
.
.
.
case constn :

default:
}

12

TechSparx Computer Training Center | 9880 205065


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 The body of the do while loop
executed even once will be executed at least once
4 Syntax: Syntax:
while( <condition> ) do
{ {
st1; st1;
st2; st2;
. .
. .
st n; st n;
} } while( <condition> );

13

TechSparx Computer Training Center | 9880 205065


ASCII Table

14

TechSparx Computer Training Center | 9880 205065


Range for unsigned numbers

Number of Possible values Possible Range General Formulae


bits(n) (Binary) values
(Decimal)
1 0 0 0 to 1 0 to 2n-1
1 1
2 00 0 0 to 3 0 to 2n-1
01 1
10 2
11 3
3 000 0 0 to 7 0 to 2n-1
001 1
010 2
011 3
100 4
101 5
110 6
111 7
4 0000 0 0 to 15 0 to 2n-1
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 10
1011 11
1100 12
1101 13
1110 14
1111 15
5 00000 0 0 to 31 0 to 25-1
. .
. .
11111 31
6 000000 0 0 to 63 0 to 26-1
. .
. .
111111 63
15 7 0000000 0 0 to 127 0 to 27-1
. .
. .
1111111 127

TechSparx Computer Training Center | 9880 205065


8 00000000 0 0 to 255 0 to 28-1
. .
. .
11111111 255
16 0000000000000000 0 0 to 65535 0 to 216-1
. .
. .
. .
1111111111111111 65535

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 + -232-1 to +232-1-1


2 billion -231 to +231-1

long 64 -10E18 to +10E18 -264-1 to +264-1-1


-263 to +263-1

16

TechSparx Computer Training Center | 9880 205065


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

17

TechSparx Computer Training Center | 9880 205065

You might also like