ICSE-JavaNotes-V4.7.2
ICSE-JavaNotes-V4.7.2
JAVA NOTES
Revision of Class IX Syllabus
Programming paradigms are a way to classify programming
languages based on their features. Paradigm means organizing
principle of a program. It is an approach to programming.
Procedure Oriented Programming: A Procedure oriented
programming approach allows the users to develop their logic by
using a number of functions that would enhance the program’s
productivity.
1
2 Java Notes Class X
Characteristics of Java:
Java libraries:
2
3 Java Notes Class X
The (*) sign denotes that all the classes of the concerning
package will be made available for use in your program.
Keywords or Reserved words:
Java reserved words or the keywords are the words which
carry special meaning to the system compiler. Such words
cannot be used for naming a variable in the program.
case , switch, else, break , static, do, const, throws, float , char,
try, int, double, void, goto, for, while, new, import , boolean,
long, if, byte , package, private, catch, short, public, class ,
default.
white space 32
Token: The smallest individual unit in a program is known as a
Token
Types:
3
4 Java Notes Class X
f) Null literal has one value, the null reference. A null literal is
always of the null type.
4
5 Java Notes Class X
Data types:
Data Types are means to identify the type of data and
associated operations of handling it.
Two types:
1. Primitive or fundamental or Instrinsic ( predefined)
5
6 Java Notes Class X
6
7 Java Notes Class X
Types:
7
8 Java Notes Class X
Unary operators:
8
9 Java Notes Class X
max = (a > b) ? a : b ;
2) salary = 15000
bonus = ( salary> 10000) ? salary*15/100 : salary*
5/100 ;
9
10 Java Notes Class X
Special operators:
new: dynamically allocate the memory for the object.
Example: example e = new example( );
Operator precedence:
Operator precedence determines the order in which
the operators in an expression are evaluated.
Object is an instance of a class. It is an identifiable entity with
some characteristics and behavior.
10
11 Java Notes Class X
11
12 Java Notes Class X
12
13 Java Notes Class X
Variables
A variable is defined as a location in the memory of the computer
where the values are stored.
13
14 Java Notes Class X
{
a=4;
b=0;
}
14
15 Java Notes Class X
for is the easiest to understand of the Java loops. All its loop
control elements are gathered in one place(on the top of the
loop)
15
16 Java Notes Class X
=&==
= = =
= is an assignment operator, = = is a relational operator,
used to assign values to a used to check for equality
variable
16
17 Java Notes Class X
17
18 Java Notes Class X
18
19 Java Notes Class X
/ and %
/ %
Returns the quotient Returns the remainder
E.g.: int c=7/2 answer =3 E.g.: int c=7%2
float c=7/2 answer = 3.5 Answer =1
19
20 Java Notes Class X
20
21 Java Notes Class X
Operator Precedence
Precedence Operator Type Associativity
15 () Parentheses Left to Right
[] Array subscript
· Member selection
14 ++ Unary post-increment Right to left
-- Unary post-decrement
13 ++ Unary pre-increment Right to left
-- Unary pre-decrement
+ Unary plus
- Unary minus
! Unary logical negation
~ Unary bitwise complement
(type) Unary type cast
12 * Multiplication Left to right
/ Division
% Modulus
11 + Addition Left to right
- Subtraction
10 << Bitwise left shift Left to right
>> Bitwise right shift with sign
>>> extension
Bitwise right shift with zero
extension
9 < Relational less than Left to right
<= Relational less than or equal
> Relational greater than
>= Relational greater than or equal
instanceof Type comparison (objects only)
8 == Relational is equal to Left to right
!= Relational is not equal to
7 & Bitwise AND Left to right
6 ^ Bitwise exclusive OR Left to right
5 | Bitwise inclusive OR Left to right
4 && Logical AND Left to right
3 || Logical OR Left to right
2 ?: Ternary conditional Right to left
1 = Assignment
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
21
22 Java Notes Class X
Constructor
Constructor is a special method which is used to initialize
instance/non-static variable.
Properties of constructors
A constructor has the same name as the class
It does not have a return type, not even void
It doesn’t have a calling statement
The constructor gets called when an object is created
Types of constructors
Parameterised
Constructor
Non Parameterised
22
23 Java Notes Class X
Examples:
Parameterized: Non-Parameterized:
1. Int num1, num2, sum; 1. Int num1, num2, sum;
2. Sum(int num1, num2){ 2. Sum(){
3. num1=num1; 3. Sum=0;
4. num2=num2; 4. }
5. }
23
24 Java Notes Class X
Function/Method
Function is a set of Java executable statements enclosed in a
function definition.
Methods
defined
methods
Advantages of methods:
Reusability
Manage complex programs into smaller parts
Hide details
Method definition/prototype refers to the first line of a method
which contains the access specifier, modifier, return type, method
name and the method signature.
Syntax
24
25 Java Notes Class X
Parameters
Actual Formal
parameters parameters
In method calling In method definition
25
26 Java Notes Class X
Pass/call by reference:
Any change made in the formal parameters
will reflect in the actual parameters
26
27 Java Notes Class X
Method Overloading
Method overloading: Multiple functions sharing the same name
with different parameters/ method signature.
Example:
Arrays
Array: An Array is a set of like variables which are referred to by a
common name. A number called subscript/index is used to
distinguish one from the other.
Syntax
‘n’ denotes the maximum number of elements that the array can
hold.
Assigning values for an Array
Ex:
1. int arr[]={1, 2, 3, 4}
1. int len=arr.length;
27
28 Java Notes Class X
Input a String
1. //For a string without any space (For a single word):
2. <variable>=<Scanner object>.next();
3. Str=sc.next();
4. //For a String with spaces (For a sentence):
5. <variable>=<Scanner object>.nextLine();
6. Str=sc.nextLine();
28
29 Java Notes Class X
String Functions
For all the below examples, str=”COMPUTER”; Output will be displayed as a single line
comment (//).
Quick
.length() (int)
This function is used to return the length of the string.
.charAt() (char)
This function returns the character from the given index.
29
30 Java Notes Class X
.indexOf() (int)
This function returns the index of first occurrence of a
character.
30
31 Java Notes Class X
.toLowerCase() (String)
This function is used to convert a given String to lowercase
letters (entire string).
Syntax with example:
.toUpperCase() (String)
This function is used to convert a given String to uppercase
letters (entire string).
Syntax with example:
31
32 Java Notes Class X
32
33 Java Notes Class X
.trim() (String)
This function removes spaces at the start and end of the
String. (NOTE: This function does not remove spaces in-between
characters)
Syntax with example:
33
34 Java Notes Class X
.equals() .compareTo()
Returns a Boolean value Returns a an int value
It checks for equality between It checks if a String is equal,
two Strings bigger or smaller than the
other.
Difference Between equals() and compareTo() functions
34
35 Java Notes Class X
Library Classes
JDK (Java Development Kit) V1.5 and above contains Java Class
Library (JCL) which contains various packages. Each package
contains various classes containing different built-in functions.
JDK
JCL
Classes
Functions
Wrapper Class
Wrapper Classes are a part of java.lang (A Library Class Package).
Wrapper classes contain primitive data values in terms of objects/
Wrapper Class wraps a primitive data type to an object. There are
8 wrapper classes in Java. Ex: Integer, Byte, Double
(NOTE: Wrapper Classes always start with an uppercase letter
Ex: Integer, Boolean, Float)
35
36 Java Notes Class X
36
37 Java Notes Class X
Unboxing
Conversion of an object to primitive type data.
Syntax with example:
Autoboxing
Boxing is the mechanism and autoboxing is the feature of
the compiler which generates the boxing code.
Syntax with example:
37
38 Java Notes Class X
Character
Character is defined as a letter, a digit or any special
symbol/UNICODE enclosed within single quotes. Ex: ‘@’, ‘s’, ‘5’
Assigning a character
A Character is declared under char data type.
Syntax with example:
Input a character
A Character is declared under char data type.
Syntax with example:
Character Functions
Character.isLetter() (boolean)
This function is used to check if a given argument is a letter or
not.
Syntax with example:
Character.isDigit() (boolean)
This function is used to check if a given argument is a digit or
not.
Syntax with example:
38
39 Java Notes Class X
Character.isLetterOrDigit() (boolean)
This function is used to check if a given argument is either a
letter or a digit or none of these.
Syntax with example:
Character.isWhitespace() (boolean)
This function is used to check if a given argument is a
blank/gap/space or not.
Syntax with example:
Character.isUpperCase() (boolean)
This function is used to check if a given argument is an
uppercase letter or not.
Syntax with example:
Character.isLowerCase() (boolean)
This function is used to check if a given argument is a or not.
Syntax with example:
Character.toUpperCase() (char)
This function is used to convert/returns a given
argument/character/letter to/in uppercase character/letter.
Syntax with example:
39
40 Java Notes Class X
Character.toLowerCase() (char)
This function is used to convert/returns a given
argument/character/letter to/in lowercase character/letter.
Syntax with example:
40
41 Java Notes Class X
41
42 Java Notes Class X
Note: All classes in the package have access to the member defined friendly. The
classes outside the package doesn’t have access to the member.
Errors
Runtime Logical
Error Error Error
Types of Errors (Icons are for illustration purposes only. FrozenNotes cannot be held liable for incorrect representation)
1.
Errors which prevent a code from execution due to
are called Compile Time Errors. These
may include mistakes like missing semicolon, missing
brackets, grammatical mistakes and more…
42
43 Java Notes Class X
== equals()
== is a relational operator equals() is a String class function
== can be used for all type of data Equals() can only be used for Strings
except Strings
43