0% found this document useful (0 votes)
176 views18 pages

ICSE 10th Notes

Uploaded by

manjunath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views18 pages

ICSE 10th Notes

Uploaded by

manjunath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

24 Java Notes Class X

Function/Method
Function is a set of Java executable statements enclosed in a
function definition.

Built in methods

Methods
User 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

1. <access specifier> <modifier> <return type> <method name> (list of


parameters)
2. {
3. <statements>
4. }
5. //eg:
6. public static int(int a, int b){
7. int c=a+b;
8. return c;
9. }

Method call syntax:

1. <object name>.<method name>(arguments);


2. //Example:
3. a.example();

new keyword is used to dynamically allocate memory for the


object

24
25 Java Notes Class X

Static keyword is a modifier which identifies the method as a class


method. It means that the method is not called through an object

Access-specifier: Keywords (except friendly) that control the


visibility of a data member
Private: the data members or member methods which are
specified as private can be used only within the scope of the
class. These members are not accessed outside the class.
Public: The class members specified as public can be used even
outside the visibility of a class
Protected: the protected members are used in the class as private
members which can only be applied within the class but can be
inherited to another class

Return types indicate the type of outcome of a method to be


returned to its caller. Ex: int, double, float, char, short, byte,
Boolean, void
Method signature: Collection of data type and variable names
written inside a function definition.
Parameters: The value which is passed into the function to
instantiate

Parameters

Actual Formal
parameters parameters
In method calling In method definition

Actual parameters: The parameters that appear in method calling


Formal parameters: The parameters that appear in method
definition

25
26 Java Notes Class X

Actual Parameters Formal Parameters


Appear in method calling Appear in method definition
Original value Copied value of actual
parameters
Ways of Pass/call by value:
passing
values to Any change made in the formal parameters
a function will not reflect in the actual parameters

Pass/call by reference:
Any change made in the formal parameters
will reflect in the actual parameters

Call by value Call by reference


Changes made in the formal Changes made in the formal
parameters will not reflect in parameters will reflect in the
the actual parameters actual parameters
Primitive type data are passed Reference type data are
to the method using pass by passed to the method using
value pass by value
It is a pure function It is an impure function
Pure function/accessor method: A method that returns a value
but does not change the state of an object
Impure function/mutator: A method that may not return a value
but change the state of an object
Recursive Function: A function that calls itself / A function that
refers to itself for execution

26
27 Java Notes Class X

Method Overloading
Method overloading: Multiple functions sharing the same name
with different parameters/ method signature.
Example:

1. void Area(int side){


2. area=side*side;
3. }
4. void Area(int l, int b){
5. area=l*b; }

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

1. <data type> <array name>[]=new <data type>[<n>];

the array can


hold.
Assigning values for an Array
Ex:

1. int arr[]={1, 2, 3, 4}

.lenght function Tells the number of elements in an array


Ex:

1. int len=arr.length;

27
28 Java Notes Class X

Linear search: The search element is checked with all the


elements of the array. If the element is found, the flag variable is

Linear Search Binary Search


Can work with both sorted and Works only with sorted arrays.
unsorted arrays.
Reads and compares each Works by dividing the array in
element of the array one by two segments out of which only
one. one needs to be searched.

String Handling & Library Classes


In Java, String is an object which contains a sequence of
characters. String class is used to create and manipulate strings.
The String class is available in java.lang package.

Declaration and Assigning a String


1. //Declaration:
2. String <variable>;
3. //Ex:
4. String str;
5. //Assigning:
6. <variable>=<String literal>;
7. //Ex:
8. str Hello World

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, Output will be displayed as a single line
comment (//).

Quick Tip

1. Function names start with lowercase and then the second


word starts with uppercase letters. Eg: indexOf();
2. Topics asked in board questions are marked with

.length() (int)
This function is used to return the length of the string.

Syntax with example:

1. <int variable>=<string var>.length();


2. int Len=str.length();
3. //8

.charAt() (char)
This function returns the character from the given index.

Syntax with example:

1. <char variable>=<string var>.charAt(<index>);


2. char ch=str.charAt(2);
3. //O

29
30 Java Notes Class X

.indexOf() (int)
This function returns the index of first occurrence of a
character.

Syntax with example:

1. <int variable>=<string var>.indexOf(<character>);


2. int idx=str.indexOf( M );
3. //2

.indexOf(char ch, int start_index) (int)


This function returns the index of a given character from the
given index.
Syntax with example:

1. <int var>=<String var>.indexOf(<char var>,<int var>);


2.
3. int ind=str.indexOf(ch, 1);
4. //2

.lastIndexOf(char ch) (int)


This function returns the index of the last occurrence of a
given character.
Syntax with example:

1. <int var>=<String var>.lastIndexOf(char ch);


2. int ind=str.lastIndexOf( );
3. //6

.substring(int start_index, int last_index) (String)


This function is used to extract a set of characters
simultaneously from a given index upto the end of the String or till
a given index.
Syntax with example:

1. <String var>=<String var>.substring(<int var>,<int var>);


2. String ext=str.substring(3);
3. //PUTER

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:

1. <String var>=<String var>.toLowerCase();


2. String lc=str.toLowerCase();
3. //computer

.toUpperCase() (String)
This function is used to convert a given String to uppercase
letters (entire string).
Syntax with example:

1. <String var>=<String var>.toUpperCase();


2. String uc=str.toUpperCase(ind);
3. //COMPUTER

.replace(char old, char new) (String)


This function is used to replace a character or a sequence of
characters in a String with a new character or sequence of
characters. (NOTE: This does not work with int values)
Syntax with example:

1. <String var>=<String var>.replace(<char var>,<char var>);


2. String rep=str. ;
3. //COMPUTE

.concat(String second) (String)


This function is used to concatenate/join two Strings together.
(NOTE: This does not add any spaces in-between)
Syntax with example:

1. <String var>=<String var>.concat(s);


2.
3. String con=str.(s);
4. //COMPUTERSTUDENT

31
32 Java Notes Class X

.equals(String srt) (boolean)


This function is used to check for equality between two
Strings. (NOTE: This function returns a boolean value. This function
cannot be used for characters. //You can simply use == for
characters. This can be used in if statements)
Syntax with example:

1. <boolean var>=<String var>.equals(<String var>);


2.
3. boolean chk=str.equals(s);
4. //true

. equalsIgnoreCase(String str) (boolean)


This function does the same function of .equals() function.
The only difference is that it does not care about the case (It
ignores the case).
Syntax with example:

1. <boolean var>=<String var>.equalsIgnoreCase(<String var>);


2. boolean chk=str.equalsIgnoreCase( ); //true

.compareTo(String str) (int)


This function is used to compare two Strings. It also checks
whether a String is bigger or smaller than the other and returns a
suitable int value. It returns 0 if both are equal. A positive value
when the first is bigger than the second and a negative value
when the second String is bigger than the first. It returns the no. of
additional characters first sequence of
characters are equal but the other has additional characters.
Syntax with example:

1. <int var>=<String var>.compareTo(<String var>);


2.
3. int cmp=str.compareTo(s);
4. //A, B, C, (C is the 3rd letter in the Alphabet and S is the 19 th)
5. //the value of cmp will be-16 because (3-19=-16)

32
33 Java Notes Class X

.compareToIgnoreCase(String str) (int)


This function does the same function as .compareTo but it
ignores the case.
Syntax with example:

1. <int var>=<String var>.compareToIgnoreCase(<String var>);


2.
3. //0

.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:

1. <String var>=<String var>.trim();


2.
3. String trm=str.trim();
4. //He llo World!

.startsWith(String str) (boolean)


This function is used to check if the given String is a prefix to
the other.
Syntax with example:

1. <boolean var>=<String var>.startsWith(<String var>);


2.
3. boolean chk=str.startsWith(pfx);
4. //true

.endsWith(String str) (boolean)


This function is used to check if a given String has a specified
suffix.
Syntax with example:

1. <boolean var>=<String var>.ends with(<String var>);


2.

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

Library Classes & Wrapper Classes


For better understanding:

Before we get into Library


important to know what is a primitive and composite data types.
Primitive Data Type: These are fundamental built-in data types of
fixed sizes. Ex: int, long, float
Composite/Reference/User-Defined Data Type: These are
data types created by the user. The availability of these data
types depends upon their scope and sizes depend upon
their constituent members. Ex: array, class, object

Primitive data type Composite data type


These are built in data types Created by user
The sizes of these objects are The sizes of these data types
fixed depend upon their constituent
members
These data types are available The availability of these data
in all parts of a java program types depends upon their
scope
Difference between primitive and composite data type.

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

Packages

Classes

Functions

Ex: java.lang, java.math

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)

Need for Wrapper Classes


To store primitive values in the objects
To convert a string data into other primitive types and vice-
versa

35
36 Java Notes Class X

Wrapper Class Primitive Type


Byte Byte
Short short
Integer int
Long long
Float float
Double double
Character char
Boolean boolean
Wrapper Classes and their primitive types

Functions/Methods of Wrapper Classes


Conversion from String to Primitive types
For converting String to any primitive data type, Wrapper
Class functions can be used. For any primitive data Wrapper Class,
the parse<prm data type>(<String arg>) (or) valueOf(<String arg>)
functions can be used.
Eg: int i=Integer.parseInt(s); int j=Integer.valueOf(s);
For better understanding:

1. <prm data type var>=<prm data type Wrapper Class>.parse<prm data


type name>(<String arg>);
2. <prm data type var>=<prm data type wrapper class>.valueOf(<String
arg>);
3.
4. //Examples:
5. int a=
6.
7.
8.

Examples of each <> (In the above syntax):


prm data type: int a | double b prm data type name: Int | Long | Double
prm data wrapper class: Integer | Double String arg:

36
37 Java Notes Class X

Conversion from primitive type data to String


For converting a primitive type data to a String, the toString()
Wrapper Class function can be used.
Ex: Integer.toString() | Double.toString()

1. <String var>=<Wrapper Class>.toString(<prm data arg>);


2. String cnv=Integer.toString(38);
3. String dbl=Double.toString(94.53);

Boxing, Unboxing & Autoboxing


Boxing
Conversion of primitive type data to an object.
Syntax with example:

1. <wrapper class> <object name>=new <wrapper class>(<prm type arg>);


2. int a=239;
3. Integer x=new Integer(a);

Unboxing
Conversion of an object to primitive type data.
Syntax with example:

1. <int var>=<wrapper class obj>


2. int b=x;

Autoboxing
Boxing is the mechanism and autoboxing is the feature of
the compiler which generates the boxing code.
Syntax with example:

1. <wrapper class> <object name>=new <wrapper class>(<prm type arg>);


2. int a=239;
3. Integer x=new Integer(a);

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:

Assigning a character
A Character is declared under char data type.
Syntax with example:

1.
2. ;

Input a character
A Character is declared under char data type.
Syntax with example:

1. <char var>=<Scanner obj>.next().charAt(0);


2. ch=sc.next().charAt(0);

Character Functions
Character.isLetter() (boolean)
This function is used to check if a given argument is a letter or
not.
Syntax with example:

1. <boolean var>=Character.isLetter(<char arg>);


2.

Character.isDigit() (boolean)
This function is used to check if a given argument is a digit or
not.
Syntax with example:

1. <boolean var>=Character.isDigit(<char arg>);


2.

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:

1. <boolean var>=Character.is(<char arg>);


2.

Character.isWhitespace() (boolean)
This function is used to check if a given argument is a
blank/gap/space or not.
Syntax with example:

1. <boolean var>=Character.is(<char arg>);


2.

Character.isUpperCase() (boolean)
This function is used to check if a given argument is an
uppercase letter or not.
Syntax with example:

1. <boolean var>=Character.is(<char arg>);


2. boolea

Character.isLowerCase() (boolean)
This function is used to check if a given argument is a or not.
Syntax with example:

1. <boolean var>=Character.is(<char arg>);


2.

Character.toUpperCase() (char)
This function is used to convert/returns a given
argument/character/letter to/in uppercase character/letter.
Syntax with example:

1. <char var>=Character.toUpperCase(<char arg>);


2. char uc=Character.toUpperCase a //A

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:

1. <char var>=Character.toLowerCase(<char arg>);


2. char lc=

Differentiate between the following


Unary & Binary operator (ICSE 2019)
Unary Binary
Works on single operand Works on 2 operands
E.g., ++, -- E.g., +, -, *, /

If else if and switch case (ICSE 2019 Marking Scheme)


If else if Switch case
Range if values are checked Only one value is compared
Supports both primitive and Supports only integer, char
composite data type
Relational/Logical expression can Only equality can be checked
be checked

Linear search and Binary search (ICSE 2019 Marking Scheme)


Linear Search Binary Search
Array need not be sorted Array must be sorted
Checks each item Does not check each item
Checks the search value from the 0 th Checks the search value from the
index middle value of the array
It checks in a sequential order It checks by dividing the array into
two halves

Call by value and Call by reference (ICSE 2019 Marking Scheme)


Call by value Call by reference
It works with primitive data type It works with reference data
type
The original value of variable The original value of variable
remains unchanged changes
Operation is performed on Operation is performed on
duplicate value of variables original values of variables.
It is also called as pure function. It is also called as Impure
function.

40
41 Java Notes Class X

Searching and sorting (ICSE 2018 Marking Scheme)


Searching Sorting
To find an element in an array is To arrange the elements in an order
called searching is called sorting
Eg: Linear Search Eg: Bubble sort

isUpperCase and toUpperCase (ICSE 2018 Marking Scheme)


isUpperCase toUpperCase
CHECKS whether a given character CONVERTS the character to its upper
is an uppercase letter or not case
Output of this function is boolean Output of this function is character

While loop and do while loop (ICSE 2018 Marking Scheme)


While loop Do while loop
Entry controlled loop Exit controlled loop
If condition is false in the beginning, Loop will be execute at least once
loop never executes even if the condition is false
Minimum reputation is 0 Minimum reputation is 1

Constructor and function (ICSE 2017)


Constructor Function
The constructor ahs the same name A function has a different name
as the class than that of the class
It does not have a return type, not It must have a return type
even void

Break and Continue


Break Continue
Used to terminate a block in which it Used to transfer the control to the
exists next iteration
Break statement can be used in It cannot be used it a stitch case
switch case as well as loop
statements

Unboxing and Boxing (ICSE Prelim)


Unboxing Boxing
Conversion of an object to primitive Conversion of primitive type data to
data type an object
Passed as an argument to a function Passed as an argument to a function
which is expecting a primitive data which is expecting a wrapper class
type variable. object.

41

You might also like