Java Part 1
Java Part 1
4. It is secure.
5. It is Object Oriented.
C/C++ Java
Concept - 2 : COMPUTER
Computer :-
1. It is a Machine/System which is used to perform a Specific Task given by a User/Human.
2. A Computer can only understand the Electric Signals like High-Voltage(1) & Low-Voltage(0).
It is a medium which is used to communicate with the computer to perform some specific task is
known as a programming language.
OR
A language which is used to write a program and perform some specific task is known as
Programming language.
Examples : Java,C/C++,Python
Concept-4: LEVELS OF PROGRAMMING LANGUAGE ?
Eg: 100001111111111000000000000001111111
Drawback
This language is difficult for humans to read and understand , only machines can understand
• To overcome the drawback of Low level language we go for Assembly level language
• ALL consists of microcontroller instruction sets called MNEMONICS to perform
specific task.
• ALL is a Language which is readable and understandable by only humans.
• Now you can understand but which language can the machines understand ? BINARY
• So you need a translator to convert ALL to LLL. The translator used here is an
assembler.
ASSEMBLER
• Assembler is a software used to convert Assembly Level Language into Low Level
Language.
ADD
SUB MNEMONICS
MUL Pre-defined
instruction sets
DIV
MOV
ALL LLL
ASSEMBLER
It is understandable It is understandable
by humans by machines
DRAWBACKS
• It contains very few instruction sets and cannot perform complex functions like Simple
interest , Compound interest etc.
COMPILER
HLL LLL
COMPILER
It is understandable It is understandable
by humans by machines
Concept - 5 : What is Java ?
PROGRAMMING LANGUAGE
What is a language ?
It is a medium of communication.
Which language do you use to communicate with your friends, parents, teacher etc ?
English, Kannada,Hindi etc
Oh! Now I want to communicate with my computer ? I want to ask the computer to add 2
numbers.
Will I talk in English,Kannada,Hindi ?No computer will not understand.
Programming
Language
What is program ?
a) Primary Memory:-
b) Secondary Memory:-
Simple
It is easy to learn and write a program using java.
No prior knowledge required.
It has no complex features like pointers which is in C.
Platform Independent
An application developed on one platform can be executed on any other platform.
Object Oriented
Everything in java is written in terms of object and class.
Portable
WORA concept makes java portable.
Portable means migration of code from one system to the other
Robust
Strong memory management feature because of Garbage collector.
Garbage collector is used to destroy unused memory or objects.
Secure
Applications developed using java are secure i.e free from virus attacks etc.
Interpreted
Java programming language uses both compiler and an interpreter.
Multithreaded
windows os
32-bit architecture
linux os
32-bit architecture
Mac os
Platform :
Platform dependant
Platform independent
PLATFORM DEPENDANT
Eg : c / c++
HOW C/C++ is platform dependant ?
In c and c++
Compiler directly converts source file to executable file.
So it is platform dependant.
PLATFORM INDEPENDENT
An application developed on one platform can be executed or run on any other platform.
Eg: java/python
HOW JAVA IS PLATFORM INDEPENDENT ?
JVM
JVM
JVM
Extension is .java
Extension is .class
WHAT IS BYTECODE ?
It is generated by compiler
Intermediate language
Neither understandable by machines nor by humans
Understood only by JRE
JDK(JAVA DEVELOPMENT KIT) :-
JDK
JDK
compiler JRE
COMPILER
JRE
JRE
JVM
JIT
Interpreter
Interpreter:-
• It takes the Class File(.class file) as input and gives an Executable File(.exe file)
as an output.
• It converts Byte Code to LLL Line-By-Line if there are no errors.
• It checks for logical errors
JIT Compiler:-
JDK DIAGRAM
IMPORTANT QUESTION
It is a user interface
It is a terminal which is used to interact with the OS with the help of commands
Windows + R
Type cmd
to open command prompt.
Drive
Hard disk is divided into multiple partition and each partition is called as Drive.
Drive Name :
Eg: D:
C:
Commands
Eg : mkdir , cd , javac
What is directory ?
It is folder.
Basic commands
class ClassName
{
FileName.java
Execution of java program starts from main() method and it is declared as:
NOTE :
1. System.out.print(data);
Example:
System.out.print(“Hi”);
System.out.print(“Bye”);
Output : HiBye
2. System.out.println(data);
Example:
System.out.println (“Hi”);
System.out.println (“Bye”);
Output :
Hi
Bye
STEPS TO COMPILE AND EXECUTE A JAVA FILE
1. Compile
2. Run or Execute
Eg:
class Demo
javac Demo
Java Demo
Class Demo
{
System.out.println(“Hi”);
}
OUTPUT
Class Demo
{
public static void main(String[]args)
{
System.out.println(“Hi”);
}
}
OUTPUT
Hi
QUESTIONS ON STRUCTURE OF JAVA
TOKENS
Eg :
seats
wheels
TOKENS IN JAVA
Keywords
Identifiers
Literals
Separators
Comments
Operators
1. Keywords
List of components :
class
methods
variables
packages
interface
Rules of Identifiers:-
1. Identifiers should not start with number.
E.g. : class 9Program CTE Compile Time Error
class Program9 CTS Compile Time Successful
2. Identifiers should not have special character except '_' and '$'.
QUESTIONS
Answer : Digits
Which of the following can be used in a Java program as identifiers? Check all of the
identifiers that are legal.
1. sum_of_data
2. AnnualSalary
3. _average
4. 42isThesolution
5. B4
6. ABC
7. for
8. println
9. "hello"
10. first-name
Ans : 1,2,3,5,6,8
Of the below, what is an invalid variable name?
Lava_nya_230_111
2000_a_beautiful_story
Lavanya_ABCDEFG
_ s_ss_ssshhhhh
None of the above
Ans : 2000_a_beautiful_story
Ans : B,D
class _
{
class true
{
class false
{
primitive literals
non primitive literals
PRIMITIVE LITERALS
1. NUMBER
Note :
Empty single quote gives compile time error because minimum and
maximum length of char literal should be 1.In this case it is 0.
‘ab’ CTE
Length is 2
3. BOOLEAN
String
It is a non primitive literal
Any data enclosed in double quote (“ “) is called a String literal.
No defined or fixed length
NOTE :
4.COMMENTS
5. Separators
; - end of statements
DATATYPES
Datatype which is used to create a variable to store primitive value such as numbers,
characters, Boolean is known as primitive datatype.
Eg : String
1. Variable declaration.
2. Variable initialization.
3. Variable declaration and initialization.
Variable declaration:-
Syntax:-
datatype variableName ;
Eg : int age;
Variable initailzation:-
Syntax:-
variableName = value;
age 35
Eg : age=35;
Variable declaration and initialization:-
Syntax:-
NOTE :
age=20;//initialized
int height;//declaration
height=6;// initialization
System.out.println(name);
System.out.println(age);
System.out.println(height);
}
}
6 int age 20 ;
=
Or
Local Variable
Global Variable
1. Local Variable
2. GLOBAL VARIABLE
NOTE :
Local variables should always be intitialized (assigned with value ) before using it.
Otherwise we get Compile time error.
C:\java_selenium\part1\scope\A.java - EditPlus
File Edit View Search Document Project Tools Browser Emmet Window Help
G
3 x م да Hx W ?
Demo1.class
Demo1.java
Demo1.java.bak
Demo2.class
Demo2.java
A.java A1.java A2.java A3.java A4.java A5.java A6.java A7.java A8.java A9.java A10.java A11.java 8 A12.java Demo1.java Demo2.java
For Help, press F1 In 9 col 1 9 00 PC ANSI
Demo1.class
Demo1.java
Demo1.java.bak
Demo2.class
Demo2.java
A.java A1.java A2.java A3.java A4.java A5.java A6.java A7.java A8.java A9.java A10.java A11.java 8 A12.java Demo1.java Demo2.java
C:\java_selenium\part1\scope\A1.java In 9 col 1 9 00 PC ANSI
Π X م B A Hx W ?
A.java A1.java > A2.java A3.java A4.java A5.java A6.java A7.java A8.java A9.java A10.java A11.java A12.java Demo1.java Demo2.java
C:\java_selenium\part1\scope\A2.java In 10 col 1 10 00 PC ANSI
Demo1.class
Demo1.java
Demo1.java.bak
Demo2.class
Demo2.java
A.java A1.java A2.java A3.java A4.java A5.java A6.java A7.java A8.java A9.java A10.java A11.java 8 A12.java Demo1.java Demo2.java
C:\java_selenium\part1\scope\A3.java In 6 col 63 9 00 PC ANSI
[C:] 1 class A4
CA 2日
java_selenium 3 public static void main(String[] args)
part1 4日 {
variables 5
int void=10;//keywords used (CTE)
6
System.out.println(void);
7 }
8 }
9
Demo1.class
Demo1.java
Demo1.java.bak
Demo2.class
Demo2.java
A.java A1.java A2.java A3.java A4.java A5.java A6.java A7.java A8.java A9.java A10.java A11.java A12.java Demo1.java Demo2.java
C:\java_selenium\part1\scope\A5.java In 8 col 29 11 29 PC ANSI
File Edit View Search Document Project Tools Browser Emmet Window Help
G
單 X م да Hx W
[C:] 1 class A6
CA 2日
java_selenium 3 public static void main(String[] args)
{
part1 4日
variables
5 a=10;
6
System.out.println(a);//CTЕ
7 }
8
9
Demo1.class
Demo1.java
Demo1.java.bak
Demo2.class
Demo2.java
A.java A1.java A2.java A3.java A4.java A5.java A6.java A7.java A8.java A9.java A10.java A11.java 8 A12.java Demo1.java Demo2.java
C:\java_selenium\part1\scope\A6.java In 9 col 1 9 00 PC ANSI
A.java A1.java A2.java A3.java A4.java A5.java A6.java A7.java A8.java A9.java A10.java A11.java 8
A12.java Demo1.java Demo2.java
C:\java_selenium\part1\scope\A7.java In 11 col 1 11 00 PC ANSI
File Edit View Search Document Project Tools Browser Emmet Window Help
G
3 Π X م B да Hx W =
[C:] 1 class А8
CA 2日
java_selenium 3 public static void main(String[] args)
}
part1 4日
variables
5 int 42a=10;
6
System.out.println(42a);
7 }
8
9
Demo1.class
Demo1.java
Demo1.java.bak
Demo2.class
Demo2.java
L A.java A1.java A2.java A3.java A4.java A5.java A6.java A7.java A8.java A9.java A10.java A11.java A12.java Demo1.java >Demo2.java
L A.java A1.java A2.java A3.java A4.java A5.java A6.java A7.java A8.java A9.java A10.java A11.java 8 A12.java Demo1.java Demo2.java
For Help, press F1 In 12 col 1 12 00 PC ANSI
File Edit View Search Document Project Tools Browser Emmet Window Help ☑
G
Σ Π X م B B A Hx W = ?
A.java A1.java A2.java A3.java A4.java A5.java A6.java A7.java A8.java A9.java >A10.java A11.java A12.java Demo1.java Demo2.java
For Help, press F1 In 11 col 1 11 00 PC ANSI
A.java A1.java A2.java A3.java A4.java A5.java A6.java A7.java A8.java A9.java A10.java A11.java A12.java Demo1.java >Demo2.java
For Help, press F1 In 11 col 1 11 00 PC ANSI
L A.java A1.java A2.java A3.java A4.java A5.java A6.java A7.java A8.java A9.java A10.java A11.java A12.java انه Demo1.java Demo2.java
For Help, press F1 In 11 col 1 11 00 PC ANSI
CHAR IMPLICIT
INT
LONG
FLOAT
DOUBLE
It is classified into two types.
WIDENING
NARROWING
WIDENING
The process of converting the smaller range of
datatype to larger range of datatype is known as
WIDENING
Byte to int
It is the implicit process where the compiler allow
the programmer to perform widening implicitly.
The widening is also known as auto type casting
or auto type promotion.
Class demo
{
Public static void main (String[ ]args)
{
Byte b=12; //here directly we are converting
int i=b;
System.out.println(i); //12
}
}
OUTPUT :
12
SYNTAX:
(datatype to be converted)variable to be converted
Class demo
{
Public static void main (String[ ]args)
Int a=-129;
Byte b =( byte)a;
System.out.println(b);
}
}
OUTPUT:
127 // it will be convert to cycling method.
-128 -127 -126 to 125 126 127
-2^n-1 to 2^n-1 -1
1 byte = 8bits
Short = 2bytes
N=16
-2^8-1 to ( 2^8-1) -1 -2^16-1 TO (2^16-1)-1
-2^7 to ( 2^7 ) -1 -2^15 TO ( 2^15 )-1
-2*2*2*2*2*2*2 to 2*2*2*2*2*2*2 -1
-128 to 128-1
-128 to 127
OPERATORS:
Operators are predefined symbols which is used to
perform specific task.
For example:
OPERANDS
5 + 1= 6 RESULT(EXPRESSION)
OPERATORS
OPERANDS:
The values which are passed to the operator is
known as OPERANDS.
Any statements will gives result is known as
EXPRESSION
Based on operands it is classified into three types.
Unary operator
Binary operator
Ternary operator
Unary operator
The operator which can accept one operand is
known as Unary operator.
Binary operator
The operator which can accept two operands is
known as Binary operator.
Ternary operator
The operator which can accept three operands is
known as Ternary operator.
Based on functionalities it is classified into the
following types.
ARITHMETIC OPERATORS
ASSIGMENT OPERATORS
LOGICAL OPERATORS
RELATIONAL OPERATORS
BIT WISE OERATORS
CONDITIONAL OPERATORS
INCREMENT/DECREMENT OPERATORS
MISCELLANEOUS OPERATORS
TYPECAST OPERATORS
INSTANCE OF (KEYWORD)
ARITHMETIC OPERATORS
The operators which is used to perform arithmetic
operations like,
ADDITIONS
SUBTRACTIONS
MULTIPLICATIONS
DIVISIONS
MODULUS
Example:
Class Demo
{.
Public static void main (String[ ]args)
{
byte a=1;
byte b=7;
int c=a+b;
System.out.println(c);
/*
Output:
8
*/
char c1 =’a’;
char c2 =’A’
int result=c1+c2;
System.out.println(result);
/*
Output:
164 // As key value
*/
boolean b1=true;
// System.out.println(b1+false); // Compile time error
}
}
We can not perform arithmetic operations with boolean
data.
What are the functions we can use + operator?
It is used to perform two operations.
ARITHMETIC ADDITION
CONCATINATION
The joining of two strings is known as concatenation.
If the + operator has the string data either LHS or LHS
Or both LHS and RHS
In string we can do only addition we cannot perform
others like subtraction, multiplication.
EXAMPLE:
Class A2
{
Public static void main (String[ ]args)
{
String S1=”hello”;
String S2=”karthik”;
String S3=S1+S2;
System.out.println(S3); //
System.out.println(S3+TRUE); //
System.out.println(10+20+S3); // 30hellokarthik
System.out.println(S3+10+20); // hellokarthik1020
}
}
OUTPUT:
hello karthik
hello karthik true
30 hello karthik
hellokarthik 1020
EXAMPLE
Class A2
{
Public static void main (String[ ]args)
{
int a=5;
int b=2;
int result = a/b;
int result 1 = a%b;
System.out.println(result);
System.out.println(result 1);
}
}
OUTPUT
2
1
ASSIGNMENT OPERATOR( = )
The operator is used to assign or store the value
into the variable.
EXAMPLE:
int i;
i=10; (the values assigned right to left)
The assignment operator will work from right to left.
COMPOUND ASSIGNMENT OPERATOR
The combination of assignment operator and
arithmetic operator is known as compound assignment
operator.
It is used to update the existing container for the
given values.
OUTPUT;
120
122
120
240
48
8
RELATIONAL OPERATOR:
It is binary operator
It is used to compare the two values.
The return type of the relational operator is boolean.
OPERATIONS.
> Greater than 5 > 10 false
< Less than 5 < 10 true
>= Greater than or equal to 5>=10 false
<= Less than or equal to 5<=5 true
= = Equal to 5 = = 5 true
!= Not equal 5 != 5 false
Class demo
{
Public static void main (String[ ]args)
{
int a=5;
int b =10;
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a>=b);
System.out.println(a<=b);
System.out.println(a==b);
System.out.println(a!=b);
}
}
LOGICAL OPERATORS
This are the operators which perform a task on
logical values.
The logical operators can accept an expression
which gives the result as boolean data or it can accept
boolean values.
Operators Symbols
And &&
Or //
Not !
WORKING FLOW
CASE( 1)
If the first operand is true then the and(&&)
operator will check the operand two and returns either
true or false.
EXAMPLE:
1< 2 && 3>5 = FALSE
TRUE FALSE
CASE (2)
If the first operand is false the && operator return
false without checking the operand 2.
3>5 && 1<2 = FALSE
FALSE
EXAMPLE
Class demo
{
Public static void main (String[ ]args)
{
int a=13, b=54;
System.out.println(a>b && false);
System.out.println(true && b>a);
System.out.println(a<b && b<a);
}
}
OUTPUT
False
True
false
OR OPERATOR ( // )
It is the binary operator, if any one of the operand
is true the OR ( // ) operator returns true ,hence it
returns false.
WORKING FLOW
CASE ( 1 )
If the first operand is false then checks the second
operands and returns true or false.
1>2 // 2<5 = TRUE
FALSE TRUE
CASE ( 2 )
If the first operand is true it returns the true
without checking the second operand.
Class demo
{
Public static void main (String[ ]args)
{
Int x = 31 , y = 34;
System.out.println(x > y // y > (x + = 1);
System.out.println(x < y // y<x );
System.out.println( x );
System.out.println( y );
}
}
OUTPUT
TRUE
TRUE
32
34
NOT OPERATOR
It is unary operator
If the input is true it returns false
If the input is false it returns true
OPERAND RESULT
O ( FALSE) 1 ( TRUE )
1 ( TRUE ) O ( FALSE)
EXAMPLE
Class demo
{
Public static void main (String[ ]args)
{
int i,j;
i=12 , j =45;
System.out.println ( !( (i < 45 ) && (j - =10)>12));
System.out.println( ! (i<45 // (j*=2)<60));
System.out.println( i );
System.out.println( j );
}
}
OUTPUT
FALSE
FALSE
12
35
CONDITIONAL OPERATOR
PRE INCREMENT
It is denoted by ++ variable name.
The pre increment will works as follows.
Class demo
{
Public static void main (String[ ]args)
{
int i =12; 13
System.out.println( + + i );//
System.out.println(i);//
}
}
OUTPUT:
13
13
POST INCREMENT
It is denoted by variable name + +.
The post increment operator works as follows.
Substitute the value
Update the variable
Use the substituted value
EXAMPLE
Class demo
{
Public static void main (String[ ]args)
{
int i =12; 13
System.out.println( i+ +);//12
System.out.println( i);
} OUTPUT:
12
} 13
DECREMENT OPERATOR:
It is used to decrease the value of the variable by 1.
It is classified into two types
PRE DECREMENT
POST DECREMENT
PRE DECREMENT
It is denoted as - - variable name.
NOTE:
The working flow of pre decrement is same as
working flow of pre increment but the pre document
update the variable by decreasing 1.
EXAMPLE:
Class demo
{
Public static void main(String [ ] args)
{
Char a=’t’; S
--a;
System.out.println(a);
}
OUTPUT:
S
POST DECREMENT:
It is denoted by variable name --.
NOTE:
The working flow of post decrement is same as
working flow of post increment but the post decrement
operator will by decrease 1.
Class demo
{
Public static void main (string[ ]args)
{
Char a=’t’;
a--;
System.out.println(a);
System.out.println(a);
}
}
Output:
T
S
DYNAMIC READING:
The process of reading the data from the user
during the execution of the program is known as
dynamic reading.
We can read the data from the user with the help of
scanner class.
SCANNER CLASS
It is predefined class class which is defined in
java.util package.
User details
Name: Karthik Age :25
Location: Bangalore Contact number: 9750093856
CONTROL FLOW STATEMENTS
The statement of controlling the flow of execution
from the complier is known as control flow statement.
We can control the flow of execution with the help
of following statements.
They are classified into two types.
Decision making statements.
Looping statements.
DECISION MAKING STATEMENTS
It is used to skip block of instructions from the
executions based on the conditions.
We have the following decision making statements.
if statement(simple if statement)
if else statement
if else if statement
Switch statement
IF STATEMENT: (If it is one condition we can use if
statement)
The instructions written inside the if block get execute
only when the condition is satisfied else if block gets
skipped from the execution.
SYNTAX:
if (condition)
{
//statements;
}
NOTE: Here condition should be only true or false
WORKING FLOW:
COMPILER
IF
FALSE CONDITION TRUE
?
IF BLOCK
GET
EXECUTED
NORMAL FLOW
Class demo
{
Public static void main(String[ ]args)
{
System.out.println(“hello”);
if (true)
{
System.out.println(“hii”);
}
if (false)
{
System.out.println(“byee”);
}
System.out.println(“thanks”);
}
}
OUTPUT:
Hello Hi Thanks
NOTE:
If the programmer forget to add block the compiler
will add the block for only one statement.
IF ELSE STATEMENT (it is either or condition)
The instructions written inside if block get executed
only if the condition is satisfied else the instructions
written inside the else block gets executed.
NOTE:
When the programmer has either or option he will go
with if else statement.
SYNTAX:
if (condition)
{
//statements;
}
else
{
//statements;
}
WORKING FLOW
COMPILER
IF
FALSE CONDITION TRUE
?
IF BLOCK
ELSE GET
BLOCK GET EXECUTED
EXECUTED
NORMAL FLOW
Write a java program to check whether the given
number is odd or even
Class demo
{
Public static void main(String[ ]args)
{
int a=10;
if (a%2 = = 0)
{
System.out.println(“it is even number”);
}
else
{
System.out.println(“it is odd number”);
}
}
}
OUTPUT
It is odd number
IF ELSE IF LADDER (more than one condition we go
for if else if ladder)
The instructions written inside the if block get
executed only if the condition is satisfied else it
checks the condition of else if from top to bottom.
If any of the else if condition is satisfied the
block get execute if not the else blocks gets execute
SYNTAX:
if (condition)
{
//statements;
}
else if (condition)
{
//statements;
}
..
..
..
else
{
//statements; COMPILER
} TRUE
IF CONDITION ?
IF BLOCK GET
FALSE EXECUTED
ELSE IF TRUE
CONDITION ?
IF BLOCK GET
EXECUTED
FALSE
ELSE IF T TRUE
CONDITION ?
OUTPUT:
C is the largest number
switch
default
case all are keywords.
break
SYNTAX:
switch (value/variable/expression)
{
case(value/expression):
{
// statements;
}
break;
..
..
..
..
default:
{
// statements;
}
}
WORKING FLOW:
BREAK:
It is a keyword, it is used to transfer the control
out of the block.
It is also known as control transferred
statement.
We can use break only inside switch or
looping statements.
NOTE:
In switch if any of the case is not matching
with the given values default block get execute.
EXAMPLE:
Import java.util*;
Class S1
{
Public static void main(String[ ]args)
{
Scanner S = new Scanner(system.in);
System.out.println(“Enter the alphabet”);
char ch =s.next( ).CharAt(0);
switch(ch)
{
case’A’ :
{
System.out.println(“hello”);
}
break;
case’B’ :
{
System.out.println(“hii”);
}
break;
default :
{
System.out.println(“byee”);
}
}
}
}
OUTPUT :
hello
NOTE:
In switch duplicate case is not allowed if we try to give
we will get compile time error.
In switch we can not use boolean, float, double, long
datatypes.(if we use compile time error we get)
LOOPING STATEMENTS:
WHILE LOOP
When the programmer don’t know the no of iterations
we will go with while loop.
SYNTAX:
// initialization;
While (condition)
{
// statements;
// updation;
}
WORKING FLOW:
COMPILER
WHILE
FALSE CONDITION TRUE
?
WHILE
BLOCK GET
EXECUTE
NORMAL FLOW
Write a java program to print “Hello world” ten times.
Class demo
{
public static void main(String[ ]args)
{
int i=10; //initialization
while(i>=1) //condition
10,9,8,7,6,5,4,3,2,1,0
{
System.out.println(“Hello world”);
--i; // updation
}
}
OUTPUT
helloworld,
helloworld
helloworld
DO WHILE LOOP
When the programmer once to execute the set of
instructions atleast one time even when the condition is
false we will go with do while loop.
SYNTAX:
Do
{
//statements;
} while (condition)
WORKING FLOW:
In do while loop the control goes to do block and
execute the instructions without checking the condition.
After executing the do block control goes to while
condition, if the condition is true the control goes to do
block again else if the condition is false control comes
to normal flow.
COMPILER
DO
BLOCK GET
EXECUTE
WHILE
FALSE
CONDITION
TRUE
?
NORMAL FLOW
OUTPUT:
5
6
7
8
9
10
Difference between while and do while
WHILE DO WHILE
In while loop 1st check the In do while 1st execute the
condition, then it execute block then it checks the
the block. condition.
It is also known as entry It is also known as exit loop
loop
The minimum number of The minimum number of
iteration in while loop is iteration in do while loop is
zero (0). one (1).
Example: Example:
OUTPUT: OUTPUT:
No output. Hello
FOR LOOP:
When the programmer knows number of iteration
we can use for loop.
In for loop the three important steps will be given
in the same line.
(i.e)
Initialization , condition, updation
SYNTAX:
for(initialization; condition; updation)
{
//statements;
}
NOTE:
If we not given condition means it becomes infinite
loop.
WORKING FLOW:
COMPILER
INITIALIZATION
Updation
NORMAL WORK
FLOW
Class demo
{
public static void main(String[ ]args)
{
int n=7;
for(int d=1; d<=n; d++)
{
if(n%d = = 0)
{
System.out.println(d); //17
}
}
}
}
OUTPUT:
8%1= = 0 (TRUE)
8%2= = 0 (TRUE)
8%3= = 0 (FALSE) here comes 1
8%4= = 0 (TRUE)
8%5= = 0 (FALSE)
8%6= = 0 (FALSE)
8%7= = 0 (FALSE)
8%8= = 0 (TRUE)
1,2,4,8 are output
NESTED FOR LOOP:
The for loop inside another for loop is known as
nested for loop.
SYNTAX:
for(initialization;condition;updation)
{
for(initialization;condition;updation)
{
//statements;
}
}
WORKING FLOW
For each iteration of outer loop the inner loop will
execute for n number of times until the inner loop
condition get false.
If the inner loop condition is false then the outer
loop will get update.
The control comes to normal flow only when the
outer loop condition is not satisfied.(false)
COMPILER
INITIALIZATION
FALSE Increment/
CONDITION 1 decrement
FALSE TRUE
Increment/
CONDITION 2 decrement
TRUE
STATEMENTS
END FOR
END FOR
METHODS
Methods are the block of instructions or set of code
is used to perform a specific task based on programmer
instructions.
It is used to transfer the data from one method to
another method.
SYNTAX:
Public static void main(String[ ]args)
[Access Modifier] [Modifier] returntype methodname
([Formal arg])
{
//statements;
}
NOTE:
As a programmer we can create customized method by
following above syntax.
In class can have any number of methods.
TERMINOLOGIES OF METHODS:
Method signature
Method declaration
Method definition
METHOD SIGNATURE:
The combination of method name and formal
argument is known as method signature.
Method name ([Formal arg])
METHOD DECLARATION:
The combination of access modifier, modifier,
return type and method signature is known as method
declaration.
[Access Modifier] [Modifier] returntype methodname
([Formal arg])
METHOD DEFINITION:
The combination of method declaration and method
block is known as method definition.
[Access Modifier] [Modifier] returntype methodname
([Formal arg])
{
//statements;
}
NOTE:
In a class we can’t have the methods with same
signature.
We can’t create a method inside another method.
ACCESS MODIFIER:(OPTIONAL)
The access modifier is used to modify the accessibility
(visibility) of the method.
In java we have the following 4 levels of access
modifier.
Public
Protected
Default
Private
MODIFIER :(OPTIONAL)
The modifier are used to specify the characteristics
of the method.
EXAMPLE FOR MODIFIER:
Static
Final
Abstract
Transient
Synchronize, etc.
RETURN TYPE
Every method will return a data after performing the
task. It will be represented with the help of return type.
Return type is a datatype which is used to specify
what type of data will be returned by the method after
performing the task.
We can give primitive datatype, non primitive
datatype and void (null,nothing) as return type.
VOID
It is a keyword.
Void is a datatype which is used as a return type
when the method returns nothing.
LOOPING METHOD
It will be automatically Whenever wants we are
execute. using the method
Example:
Class demo
{
public static void Ayan( ) // method
{
System.out.println(“I am presenting”);
Return;
}
public static void main(String [ ]args)
{
trainer( );
System.out.println(“java class”);
trainer( );
trainer( );
System.out.println(“execution ends”);
}
public static void trainer( )
{
System.out.println(“good afternoon”);
Ayan( );
}
}
//output
Java class
Good afternoon
I am presenting
Good afternoon
I am presenting
Rules:
The return statement should return the data which is
same as type given in the return type.
If it is not same the compiler thrice to perform
implicit conversion, if is not possible we will get
compiler time error.
The return statement should be the last instructions
the method block if not we will get compile time error.
SYNTAX:
Return data/expression;
NOTE:
The return statement can return only one value.
Every method will have return statement.
A method can have only one return statement.
Working flow of return statement:
Whenever the return statement is executing the
control will be transferred from called method to
calling method.
Therefore return keyword is also known as
controlled transferred statement.
Example program.
Class demo
{
public static void main(String[ ]args)
{
System.out.println(“main starts”);
System.out.println(“good aftenun”);
M1( );
M2( );
M3( );
M4( );
}
public static int m1( )
{
System.out.println(“m1 is executing”);
int a= 2, b= 5;
System.out.println(b -a);
return 5;
}
public static boolean m2( )
{
System.out.println(“m2 is executing”);
int a=15, b= 5;
System.out.println(a-b);
return false;
}
public static string m3( )
{
System.out.println(“m3 is executing”);
int a=2,b=5;
System.out.println(a*b);
return a+”java”+12+b;
}
public static void m4( )
{
System.out.println(“m4 is executing”);
int a=2,b=15;
System.out.println(b/a);
Return;
}
}
OUTPUT:
Main starts
Good afternoon
M1 is executing
3
M2 is executing
10
M3 is executing
10
M4 is executing
7
NOTE:
If the method return type is other than void we can
call the method inside the print statement, decision
making statement, looping statement etc.
Class demo
{
Public static void main(String[ ]args)
{
System.out.println(“execution starts”);
M1( );
M2( );
M3( );
M4( );
System.out.println(“execution ends”);
}
Public static int m1( ) //method
{
System.out.println(“m1 is executing”);
System.out.println(m2( ));
return 8;
}
Public static long m2( ) //method
{
System.out.println(“m2 is executing”);
System.out.println(m3( ));
Return 1244563;
}
Public static float m3( )
{
System.out.println(“m3 is executing”);
System.out.println(m4( ));
Return 1.2f;
}
Public static char m4( )
{
Int a=97;
System.out.println(“m4 is executing”);
Return (char)a;
}
}
}
execution starts
m1 is executing
m2 is executing
m3 is executing
m4 is executing
a
1.2
1244563
m2 is executing
m3 is executing
m4 is executing
a
1.2
m3 is executing
m4 is executing
a
m4 is executing
execution ends
CALLING METHOD:
A method which is trying to call another method is
known as calling method.
CALLED METHOD
A method which is being called is known called
method.
TYPES OF METHOD:
Based on the arguments the methods are classified
into the two types.
NO ARGUMENT METHOD:
A method which is not having formal argument is
known as no argument method.
EXAMPLE:
public static void main m1( )
{
System.out.println(“no arg”);
}
PARAMETERIZED METHOD:
A method which is having formal argument is
known as parameterized method.
SYNTAX:
[Access Modifier] [Modifier] returntype methodname
([datatype variable])
{
//statements;
}
EXAMPLE:
public static void m1(int a, int b)
{
System.out.println(“parameterized method”);
}
METHOD CALL STATEMENT:
The statement which is used to call a method is
known as method call statement.
SYNTAX:
Methodname ([actual arguments]);
NOTE:
We can call the no argument method without passing
actual argument in the method call statement.
FORMAL ARGUMENTS:
A variables which are declared in the method
declaration is known as formal arguments.
ACTUAL ARGUMENTS:
The values which are passed in the method call
statement are known as actual arguments.
NOTE:
The formal arguments are local in nature (local
variables).
Class demo
{
public static void main(String [ ]args)
{
Test(false);// actual arguments
}
Public static void test (boolean a)//formal arguments
{
if(a)
{
System.out.println(“test is there”);
}
else
{
System.out.println(“test is not there”);
}
}
}
Method overloading:
In a class, methods having same name but different
formal arguments is known as method overloading.
The formal arguments may either differ in length of
formal arguments or type of formal arguments.
Example:
Class demo
{
public static void main(String[ ]args)
{
System.out.println(“main starts”);
System.out.println(“12.3”);
}
Public static void main(int a)
{
System.out.println(a);
}
Public static void main(boolean a)
{
System.out.println(a);
}
}
Advantages of method over loading
It improves readability of the program.
It add cleaniness to the program.
Method Recursion
The process of calling a method itself repeated is
known as method recursion.
EXAMPLE:
Class demo
{
public static void main(String[ ]args)
{
m1( );
}
public static void m1( )
{
System.out.println(“HELLO”);
m1( );
}