0% found this document useful (0 votes)
2 views38 pages

ICSE Class 9 Java Notes

The document is a comprehensive reference on Java programming, covering fundamental concepts of object-oriented programming, types of programming languages, and the principles of OOPs. It details Java's history, its features, and the compilation process, emphasizing Java's platform independence. Additionally, it introduces basic concepts of classes and objects, tokens, and operators in Java.

Uploaded by

kartikeyg8429
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)
2 views38 pages

ICSE Class 9 Java Notes

The document is a comprehensive reference on Java programming, covering fundamental concepts of object-oriented programming, types of programming languages, and the principles of OOPs. It details Java's history, its features, and the compilation process, emphasizing Java's platform independence. Additionally, it introduces basic concepts of classes and objects, tokens, and operators in Java.

Uploaded by

kartikeyg8429
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

1

The Complete Reference of Java


By: Sumit Verma
Published by Siya Valley
2
3

CHAPTER 1
Introduction to Object-Oriented Programming Concepts
Programming language: The sequences of set of instructions of any
programming language used to find the solution of the problem is known as
programming language or computer language.

Program: The set of instructions of any programming language given to a


computer to solve a problem is known as a program.

Programming: The procedure used to write the commands and statements of a


programming language to find the solution of the problem is known as
programming.

Programmer: A person who writes the program is referred to as a programmer.

There are two types of programming languages:


1) Low-Level Language (LLL)
2) High-Level Language (HLL)

Low- Level Language High- Level Language


Closer to machine code Farther from machine language
Provides direct access to hardware Abstracts away hardware details
resources
Platform-specific Platform-independent (with some
exceptions)
Examples: Assembly language, Examples: Python, Java,
Machine language JavaScript, etc.

Programming paradigm: The way of programming is known as


programming paradigm.
4

There are two types of programming paradigm:

1) Procedure-Oriented Programming (POPs)


2) Object-Oriented Programming (OOPs)

POPs OOPs
It divides the program into small It divides the program into
parts known as methods. objects.
It follows a top-down approach. It follows a bottom-up approach.
It deals with algorithm. It deals with data.
It is less secure. It is more secure.
Examples: C, Fortran Examples: C++, Java

Principles of the OOPs


There are mainly five principles of OOPs:
a) Abstraction (Data abstraction)
b) Encapsulation
c) Inheritance
d) Polymorphism
e) Modularity

Abstraction: The act of representing essential features without including


the background details is known as Abstraction.
Example: Switch board
Data hiding: The property which does not allow the data and its
functions to be used as public interface (or for the outer world) is known
as data hiding (or information hiding).

Encapsulation: The wrapping of data and functions into a single unit is


known as Encapsulation.
Example: Capsule
Encapsulation is a way to implement abstraction.

Inheritance: The act or process of acquiring the properties of existing


class (base class) by another class (derived class) is known as Inheritance.
5

Example: a child acquires some characteristics form grandparents /


parents.
Base class: The existing class whose properties are inherited by
some other class is known as base class or super class or parent class.
Derived class: The class that acquires the properties from existing
class is known as derived class or sub class or child class.

Polymorphism: The act or the process of defining an object in many


forms represented by a common name is known as Polymorphism.
(Polymorphism means many forms or one interface or multiple methods)
Example: an object Table may have different shapes but their names will
be table only.

Modularity: The division of program into a small module is known as


modularity.
Examples: our computer book are divided into several chapters for better
understanding.

❖ Java history
Java is a third-generation programming object-oriented
programming language with various features for writing codes or applets.
James Gosling, Patrick Naughton, Chris Warth, Ed Frank and Mike
Sheridan at Sun Microsystems, Inc., USA imagined the Java in 1991. It
was developed by the name ‘Oak’ and renamed as ‘Java’ in 1995.
Java was first known (internally) as GreenTalk, but it became
known as Oak before finally becoming Java.

❖ Types of Java Program


Data: The raw figures and facts used for processing is known as
data or data members or variables or instance variables.
Examples: a, b, T, Y, 9, 4.6, n3, etc.
Method (or function): The self-contained program structure that
uses data or variables of the class to produce the required output is known
as method or member method or function.
There are two types of Java program:
1) Applet Program
2) Application Program Interface (API) or Application Program
6

Applet program: The program written in the Java programming


language that can be included in an HTML (Hyper Text Markup
Language) page is known as applet programming.
Example: Animated GIF’s, Simple Java Animation, Toy Demo Applets,
etc.

Application Programming Interface: The stand-alone Java program


that runs as a true application without a browser using JVM (Java Virtual
Machine) is known as application programming interface or application
program.
Examples: Mobile Applications, Web Applications, Web Server, GUI, etc.

Byte code: The platform independent code in which Java compiler


converts the Java source code into a platform independent code that can be
easily understood and executed by the computer is known as byte code.
Translator: The program or the application that translates a program into
machine language or low-level language is known as translator.
The translators are commonly divided into two parts:
1) Interpreter
2) Compiler

Interpreter: The program that translate the source program or source code
to machine language or low-level language step by step or line by line is known
as interpreter.

Source program in INTERPRETER Low Level


High-level (Does step by step translation)
Language
language

Compiler: The program that translate the source program or source code
to machine language or low-level language at once is known as compiler.
7

Source program in COMPILER High Level


High-level (Translates the program at once) language
language

❖ Types of compilation process

There are two types of compilation process:


1) Ordinary Compilation Process
2) Java Compilation Process
Ordinary Compilation Process:
Step 1: Java Source Code
Step 2: Compiler
Step 3: Machine language
Work on particular platform (e.g.: platform 2)

Java Compilation Process:


Step 1: Java Source code
Step 2: Java Compiler
Step 3: Java byte code
Step 4: JVM – the Java Interpreter
Step 5: Machine language
Works on all platform (because of platform independency)
8

Why Java is called platform independent?


Java is called platform independent because of its ability to run the
same compiled code on any operating system or hardware platform that
has a Java Virtual Machine (JVM).

Source Code: The sets of instructions and statements written by


computer programmer using a programming language to find the
solution of the problem is known as source code or program.
Object Code: The machine language program produced after the
compilation of the source code is known as object code or object
module or object program.
JVM: The Java run time system or virtual machine that translates
or interprets byte code into machine code is known as Java Virtual
Machine (JVM).

❖ Why Java is necessary?


• Simple
• Secure
• Portable
• Object-oriented
• Robust
9

• Multithread
• Architecture neutral
• Interpreted
• High performance
• Distributed
• Dynamic

❖ Features or characteristics of Java


• Platform Independent
• Write Once Run Anywhere
• Object Oriented Language
• Simple Coding
• User Friendly Environment
• Java in Robust
• Secure Environment

***
10

CHAPTER 2
Elementary concept of Classes and Objects
Object: An identifiable entity with some characteristics (or data), a
state and behavior (or function) is known as an object.
OR
An object is a self-contained entity which contains attributes and
behavior.
An object has three states:
i) Identity
ii) State
iii) Behavior
Example: Bike is an object
Characteristics– Black colour, 150cc engine, company
Bajaj
Identity- Bajaj Discover 10
Behavior- Starting the engine, changing the gear, using the
break etc.
Class: A class defines the common data and methods as single unit
that will be shared by a set of objects.
OR
A class is a blueprint that defines the variables and the methods
common to all objects.
OR
A class is a collection of objects of similar types.

Why class is called object factory?


Class is known as an object factory because it provides a way to create
multiple objects, each with its own characteristics and behaviors or
because it contains a group of objects with similar characteristics and
behaviors.
11

Why class is known as User-defined methods?


Class is known as User-defined data types because it allows developers
to create custom data types that are tailored for the specific needs.

Why class is called compositive data types?


Class is known as compositive data types because it allows you to
combine and multiple variables of different data types into a single unit.

***
12

CHAPTER 3
Class as Basis of all Computation
Character set is a set of valid characters that a language can recognize. A
character set represents any letters, digits or any other sign.
Java uses the Unicode character set.
Unicode character set: Unicode is a two-bytecode character code
set that has characters representing almost all characters in human’s
alphabets and writing systems around the world including Arabic,
Chinese, and many others languages of the world.
There is total 65,536 characters in the Unicode character set.
Characters ASCII values
A to Z 65 to 90
a to z 97 to 112
0 to 9 48 to 57
'' 32

TOKEN: The smallest individual unit in a program is known as Token.


Types of Tokens:
There are five types of tokens:
1) Keywords
2) Identifiers
3) Operators
4) Literals
5) Punctuators / Separators

Keyword: The words that convey specific meaning to a java


compiler and performs some specific tasks are known as Keywords.
There are a total 67 keywords.
true, false and null are reserved word but they are not keywords.
const and goto are keyword but they are not currently used.
13
abstract assert boolean break byte case
catch char class const continue default
do double else enum exports extends
final finally float for goto if
implements import instanceof int interface long
module native new open opens package
private protected provides public requires return
short static strictfp super switch synchronized
this throw throws to transient transitive
try uses void volatile while with

Identifiers: Identifiers are the name given by the programmer to


variables, classes, objects, functions, array etc.
The following rules are used for naming a identifier:
• It can contain alphabets (A to Z or a to z), digits, and underscore
and dollar sign characters.
• It must not be keyword or boolean literal or null literal.
• It must not start with the digits.
• It can be of any length.
• Uppercase and Lowercase characters are treated differently.
(Java is case sensitive)

Valid identifiers
Myfile
isPrime
var
n1
mum_1

Invalid identifiers
DATA-REC
27AC
break
[Link]
14

Operators: Operators are the symbols that is used to performs


operation on the operands.
Types of Operators:
i) On the basis of the number of operands
ii) On the basis of the operations
On the basis of number of operands:
i) Unary operator
a) Increment operator
i. Prefix increment operator
ii. Postfix increment operator
b) Decrement operator
i. Prefix decrement operator
ii. Postfix decrement operator
ii) Binary operator
iii) Ternary operator

Unary operators: A unary operator is an operator that operates on


only one operand.
Example: a++, a--, ++a, --a
Prefix increment operator: The operator that used to first
increase the value by 1 and then use it.
Note: First change, then use.
Example: int x=7;
m=++x;
[Link](m); // Answer is: 8
Postfix increment operator: The operator that used to first
use the value and then increase it by 1.
Note: First use, then change.
Example: int x=7;
m=x++;
[Link](m); // Answer is: 7
15

Prefix decrement operator: The operator that used to first


decrease the value by 1 and then use it.
Note: First change, then use.
Example: int x=7;
m=--x;
[Link](m); // Answer is: 6
Postfix decrement operator: The operator that used to first
use the value and then decrease it by 1.
Note: First use, then change.
Example: int x=7;
m=x--;
[Link](m); // Answer is: 7
Binary operator: A binary operator is an operator that operate on
two operands.
Example: a+b, a-b, etc.
Ternary operator: A ternary operator is an operator that operates on
three operands.
Syntax of ternary operator: Expression1? Expression2:Expression3
(If expression 1 is true, then expression 2 will evaluates and
expression 1 is false, then expression 3 will evaluates)
Example: int a = (5>7)? 5: 7; // Answer is: 7

On the basis of number of operations:


i) Arithmetic operators
ii) Relational operators
iii) Logical operators
iv) Others operator
i. Conditional operator
ii. dot operator
iii. new operator
iv. Bitwise operator
16

v. Shift operator
vi. Java shorthand operator
Arithmetic operators: The operators that is used to perform
arithmetic operations are called arithmetic operators.
Operator symbols: +, -, *, /, %
Given a=5 and b=3: -
Examples: a+b // Answer: 8
a-b //Answer: 2
a*b //Answer: 15
a/b //Answer: 1
Note: In the modulo (%) operator, there are two cases:
Case 1: If a>b, then:
the answer will always be remainder.
Example: a%b //Answer: 2
(Given a=5 and b=3)
Case 2: If a<b, then:
the answer will always be a.
Example: a%b //Answer: 3
(Given a=3 and b=5)

Relational operators: The operators that is used to show


comparisons between java expressions is called relational operator.
Operator symbols: <, <=, >, >=, ==, !=
Return type: boolean
Given a=5 and b=3: -
Examples: a>b // Answer: true
a>=b //Answer: true
a<b // Answer: false
17

a<=b //Answer: false


a==b // Answer: false
a!=b //Answer: true
Logical operators: The operators that is used to show comparisons
between two or more relational expressions is called logical operators.
Operator symbols: &&, ||, !
Return type: boolean

&&
Expression1 Expression2 Result
false false false
false true false
true false false
true true true

||
Expression1 Expression2 Result
false false false
false true true
true false true
true true true

!
Expression1 Result
false true
true false

Given a=5, b=7 and c=3:


Examples: (a>b) && (a>c) // Answer: false
(a>b) || (a>c) // Answer: true
(!(a>b)) // Answer: true
18

Conditional operators: The operators that evaluate boolean


expressions and decide the flow of execution based on conditions known
as conditional operators.
It is also called ternary operator.
Operator symbol: ? :
Syntax: Expression1? Expression2 : Expression 3
If expression 1 is true, then expression 2 will evaluate and if
expression 1 is false, then expression 3 will evaluate.
Given: a=5 and b=7
Example: (a<=b) ? “I love Computer Science” : “I love
Mathematics” // Answer: “I love Computer Science”

Dot operators: The operator that connects an object or class with its
properties (variables) or behaviours methods) is called dot operators.
Examples: [Link]();
(obj is object name and display is function name)
new operators: The new operator is used to create (instantiate)
objects of a class or arrays and also allocates memory on the heap for the
object or array and returns a reference to it.
Examples: Scanner sc =new Scanner ([Link]);
int arr[]=new int [10];
Java shorthand operators: Java shorthand operators (also called
compound assignment operators) allow you to combine an operation and
assignment in a single statement.
Examples: a+=5 represents a=a+5
a-=5 represents a=a-5
a*=5 represents a=a*5
a/=5 represents a=a/5
a%=5 represents a=a%5
Bitwise and shift operators is not in class 10 syllabus.
19

Precedence of operators:
Order Operator(s)
1 ( ), [ ]
2 ++, --, ~, !
3 *, /, %
4 +, -
5 >>, >>>, <<
6 >, >=, <, <=
7 ==, !=
8 &
9 ^
10 |
11 &&
12 ||
13 ?:
14 =

Evaluate the following:


i) 7/3+5*8-2%7
Solutions:
Step 1: Find the order of precedence
(*, / and % and high precedence than + and -)
Step 2: If two or more operators have same precedence than solve from
left to right.
2+40-2 // Question get reduce
Step 3: Repeat step 2 until the final answer come.
Answer: 42
20

Literals: Literals are the constants values whose value does not
change during the execution of the program.
Types of literals:
There are six types of literals:
i) Integer literal
ii) Floating/real literal
iii) Character literal
iv) String literal
v) Boolean literal
vi) null literal

Integer literal: An integer literal is a constant whole number


(without a fractional part) that is directly written in the code.
Example: -123, 015,0XA etc
Types of integer literal
There are three types of integer literals:
i) Octal integer literal
ii) Decimal integer literal
iii) Hexadecimal integer literal
Binary integer literal:
➢ Base 2
➢ Starts with 0b or 0B
➢ Consists of 0 and 1
Examples: 0b0101, 0B101, etc.
Octal integer literal:
➢ Base 8
➢ Starts with 0 (zero)
➢ Consists of 0 to 7
Examples: 015, 065, etc.
Decimal integer literal:
➢ Base 10
➢ Consists of 0 to 9
Examples: 18, 515, etc.
21

Hexadecimal integer literal:


➢ Base 16
➢ Starts with 0x or 0X
➢ Consists of 0 to 9 and A to F
Examples: 0xA, 0X15, etc.
Floating literal: A floating literal is a constant whole number (with
a fractional part) that is directly written in the code
Examples: 5.165,4.24, etc.
Character literal: Any single character enclosed within single
inverted commas is known as Character literals.
Examples: ‘a’, ‘9’, ‘$’, etc.
Escape sequence: An escape sequence is a special character that is
used in a string or character literal, starting with a backslash (\).
They are used to represent characters that cannot be typed directly
or have a special meaning in Java (like newline, tab, etc.).
Escape sequences
'\n ' new line character
'\a ' appears a beep sound
'\b ' backspace
'\t ' horizontal tab
'\0 ' null character used as string
terminator
'\r ' carriage return
'\f ' form feed
'\v ' vertical tab
'\\ ' backslash
'\' ' single quote
'\ ' ' ' double quote
'\? ' question mark
'\0n ' octal number
'\xHn ' hexadecimal number
'\uHn ' Unicode character represented
through its hexadecimal code
Hn.
22

String literal: One or more than one characters enclosed within


double inverted commas is known as string literal.
Example: “Siya Valley”, “Ada Lovelace”, etc.
Boolean literal: true and false are called boolean literals.
null literal: The null literal represents the absence of any value or
reference.
Punctuators/Separators: They are the specific symbols used to
indicate how group of codes are divided and arranged.
There are nine punctuators:

Comment: Comment are the part of the program that does not
execute during the execution of the program.
Type of comments:
There are two types of comments:
i) Single line comments
ii) Multiline comments

Single line comments: The comments that is used to ignore


everything after \\ in single line.
Symbols: \\
Examples: \\ I am a programmer
23

Multi line comments: The comments that is used to ignore


everything after \* in one or more than one line until */ is not
found.
Symbol: \*…*\
Examples: \* I am a programmer *\
Whitespace: Whitespace is defined as the ASCII space character,
tab character, form feed character and line terminator character.
Note: - In java, whitespace is a space, tab, newline or form feed.
Data types: To identify the type of data invoked in an operation is
known as data type.
Data types

Primitive data
types
Non-primitive
data types

Primitive (or Pre-defined or Intrinsic or Fundamental) data types: The


data types which automatically loaded in the memory as soon as Java
program runs are known as primitive data types.
There are eight primitive data types.
Non-primitive (or derived or reference) data types: The data types
formed with the help of primitive data types is known as non-primitive
data types.
It is used to store the memory address of the object.
24

Integer (byte,
short, int, long)
Floating point
(float, double)
Primitive data types
Character (char)

Boolean (boolean)
Data types
Classes

Non-primitive data
Interfaces
types

Arrays

Size of primitive data types

byte 1 byte

short 2 bytes

int 4 bytes

long 8 bytes

float 4 bytes

double 8 bytes

char 2 bytes

boolean 1 byte (used 1


bits)
25

Precision

float 12 digits

double 15 digits

Range of data types

byte 8 bits −27 to 27 − 1

short 16 bits −215 to 215 − 1

int 32 bits −231 to 231 − 1

long 64 bits −263 to 263 − 1

float 32 bits -3,40292347E+38 to +3,40292347E+38

double 64 bits -1.79769313486231570E+308 to


+1.79769313486231570E+308

char 16 bits ‘\u0000’ to ‘\uFFFF’

boolean 8 bits (used 1 bits) true, false

Formula for finding range

−2𝑛−1 to 2𝑛−1 − 1
26

Bi-default values of primitive data types:

Bi-default values

byte 0

short 0

int 0

long 0

float 0.0

double 0.0

char null or ‘\0’

boolean false

Type conversion: To convert one primitive data type to another


primitive data types are known as type conversion.
Type conversion

Implicit type
conversion

Explicit type
conversion
27

Implicit type conversion: The conversion automatically performed by


the compiler on the basic of largest data type is known as implicit type
conversion.
It is also known as type promotion or automatic type casting.
Examples: int a = 7;
long b = a;
Explicit type conversion: The process of changing one predefined
data type to another user-defined data types force fully is known as
explicit type conversion.
It is also known as type casting or user-defined type conversion.
Examples: double a = 7.8;
int b = (int)a;

From (data types) To (data types)

byte short, char, int, long, float,


double

short int, long, float, double

int long, float, double

long float, double

float double

char int, long, float, double

***
28

CHAPTER 4
MATH CLASS
Introduction to math class:
➢ It belongs to the [Link] package
➢ [Link] is a bi-default package (import of [Link] package is
not required)
➢ It has many functions
Example:
[Link](argument);
[Link](argument);
[Link](argument1, argument2);
[Link]();
[Link]

1) [Link](argument);
This function returns absolute value of a numeric data by ignoring its sign
(+ or -).
[Link](-73); Answer: 73
[Link](+73); Answer: 73
[Link](-73.25); Answer: 73.25

2) [Link](argument);
This function returns the square root of a positive number.
[Link](81); Answer: 9.0
[Link](-81); Answer: no output
[Link](81.9); Answer: 9.049861877399014

3) [Link](argument1, argument2);
This function is used to find the power of numeric data.
[Link](2,3); Answer: 8.0
[Link](-2,3); Answer: -8.0
[Link](2.5,2); Answer: 6.25
29

4) [Link](argument1, argument2);
This function returns largest from two arguments.
[Link](15,20); Answer: 20
[Link](-1.5,1.5); Answer: 1.5

5) [Link](argument1, argument2);
This function returns smallest from two arguments.
[Link](15,20); Answer: 15
[Link](-1.5,1.5); Answer: -1.5

6) [Link](argument);
This function returns the cube root of the method argument.
[Link](125); Answer: 5.0
[Link](-27.0); Answer: -3.0
[Link](10 ); Answer:2.154434690031884

7) [Link](argument);
This function returns the smallest whole number greater than or equal to
the number by rounding up i.e. irrespective of the fractional part.
[Link](15.3); Answer: 16.0
[Link](15.52); Answer: 16.0
[Link](9.01); Answer: 10.0
[Link](-9.01); Answer: -9.0
[Link](10); Answer: 10.0
[Link](-0.03); Answer: -0.0

8) [Link](argument);
This function returns the largest whole number less than or equal to the
number by rounding up i.e. irrespective of the fractional part.
[Link](15.3); Answer: 15.0
[Link](15.52); Answer: 15.0
[Link](9.01); Answer:9.0
[Link](-9.01); Answer: -10.0
[Link](10); Answer: 10.0
[Link](-0.03); Answer: -1.0
30

9) [Link]( );
This function is used to generate random numbers between 0.0 and 1.0 .
Note: - The results of random will vary on every execution.
[Link]( ); Answer: 0.653248765892756
Answer: 0.345798972137946
10) [Link](argument);
This function adds 0.5 to the argument and returns the closest integer
value.
If the argument is not a number, then its returns zero.
If the argument is negative, infinity or less than the MIN_VALUE for
the data type , it returns the MIN_VALUE.
If the argument is positive, infinity or greater than the MAX_VALUE
for the data type , it returns the MAX_VALUE.
Example :-
[Link](1.5); Answer: 2
[Link](-1.5); Answer: -1
[Link]([Link]); Answer: 0
[Link](Float.NEGATIVE_INFINITY);
Answer: -2147483648
[Link](Double.POSITIVE_INFINITY);
Answer: 9223372036854775807

11) [Link](argument);
This function returns truncated value of an argument by rounding off
by 0.5 .
[Link](25.3); Answer: 25.0
[Link](25.5); Answer: 26.0
[Link](30.35); Answer: 30.0
[Link](15.99); Answer: 16.0

12) [Link]
This function returns the value of pie.
[Link]
Answer: 3.141592653589793

Difference between [Link](argument) and [Link](argument): -

[Link] (argument) returns the integer value.


[Link] (argument) returns the double value.
***
Functions or Methods
**********************************************************
A method is compound set of executable statements to carry out some
specific operation, which is executed, when it is called from another section of
code.
Example:
println();
main();
[Link](argument);
Why Functions is necessary:
i) Simple code
ii) Reusability of code
iii) Abstraction
iv) Modularity

Method definition:
The compound body defined by user’s given name consists of a valid set of
statements (Java statements, operators, methods, etc.) within the curly braces to
perform some tasks known as method definition.
<access specifier> <modifier> Function_data_types Function_name (list of
parameters)

Example:
public static void main (int a, int b)
public void main ( )
public int getSum (int n)
 Access Specifier:
There are five types of access specifiers:
i. public - access from everywhere
ii. private - access within the same class
iii. protected - access from same package or subclasses
iv. private protected - access only in subclasses
v. default - access only in same package
private has least accessibility and public has most accessibility.

 Modifier:
The modifier may be static or final or native or volatile or synchronized.

The static keyword makes a method (or function) as a class function (also
known as static method), these means the function can be called or invoked
or executed through the same class and not through the object of the class.

But the final keyword changes the function into final or constant, thus the
statements of the functions cannot be modified as they become constant.

To write modifier is optional.

 Function data type:


It tells us that function is return or non-return type.
→ If void is used as data type, it means that function do not return a value
i.e., non-return type.
→ If byte, short, int, long, float, double, char, boolean, String etc. is used as
data type, it means function return a value i.e., return type.

The return keyword is used to return a value from the function definition to
function statement.
OR
In other words, the return statement transfers the control back to function
call statement.
Example of non-return type function:

➢ Write a program to find area of rectangle.


class Rectangle
{
public void calc (int l, int b)
{
int area=l*b;
[Link]("Area : " + area);
}
}

Example of return type function:

➢ Write a program to find area of rectangle and return it.


class Rectangle
{
public int calc (int l, int b)
{
int area=l*b;
return area;
}
}
Note: It does not matter how many return type functions are there, but
program return only one values that is invoked first.

• Invoking a Function
To execute all the statements given within the function definition
body by providing actual values to the parameters (if parameters are
given within the brackets) is known as calling function or invoking
function or executing function or accessing function.
Syntax:
object_name.function_name( );
[Link]( );

 Actual parameters:
Those parameters which appears in the function call statement are known as
actual parameters.

Example 1:
product (223, 70, 80);

Example 2:
int cd=223, float pr=70, qty=80;
product (cd, pr, qty);

These arguments/actual values are passed to the variables/ arguments/


parameters given in function definition.

 Formal parameters:
Those parameters which appears in the function definition statement are
known as formal parameters.

Example:
public static void product (int code, int float price)
{
set of statements;
}

These arguments receive actual values from function call statement.


 Function call by value

• In this method the actual parameters are passed to formal arguments


that are copied to formal arguments and works with them as needed.
• The changes made in the formal parameters (if any) do not reflect to
actual parameters.
• In call by value method fundamental or primitive data types are
passed (e.g. int, float, long, char, double etc.)

public int process (int m)


{
m=m/10;
}
public void run ( )
{
int z=30;
process(z);
[Link]("The result = " + z);
}

The output of the program is:


The result = 30

 Function call by reference

• In this method the actual parameters are passed to reference that


are copied to formal arguments and works with them as needed.
• The changes made in the formal parameters (if any) are reflected in
the actual parameters.
• In call by reference method only reference type arguments
(objects, arrays) or non-primitive data types are passed.
public void process (Sample T)
{
T.z=T.z/10;
}
public void run ( )
{
Sample obj=new Sample ( );
obj.z=30;
process (obj);
[Link]("The result = " + obj.z);
}
The output of the program is:
The result = 3

***
Scanner Class
************************************************************
Introduction to scanner class:
➢ It belongs to the [Link] package
It has many functions:
Let discuss each function with an example:
1. nextByte()
➢ It is a function of byte data type.
2. nextShort()
➢ It is a function of short data type.
3. nextInt()
➢ It is a function of int data type.
4. nextLong()
➢ It is a function of long data type.
5. nextFloat()
➢ It is a function of float data type.
6. nextDouble()
➢ It is a function of double data type.
7. next().charAt(0)
➢ It is a function of char data type.
8. nextBoolean()
➢ It is a function of boolean data type.
9. next()
➢ It is a function of string data type.
➢ It is used to accept string values without space.
[Link]()
➢ It is also a function of string data type.
➢ It is used to accept string values with spaces.
❖ Syntax of object creation:
<class_name> <object_name> = new <class_name>();
Example:
Demo d =new Demo();
Scanner sc=new Scanner([Link]);

❖ Syntax of object calling:


<object_name>.<function_name>();
Example: [Link](); [Link]();
 Write a program to input your name, class and admission number.
import [Link].*;
class Introduction
{
public static void main(String [] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter your name : ");
String name=[Link]();
[Link]("Enter your class : ");
int cl=[Link]();
[Link]("Enter your admission number : ");
int adm_no = [Link]();
[Link]("Name : " + name);
[Link]("Class : " + cl);
[Link]("Admission number : " + adm_no);
}
}

You might also like