0% found this document useful (0 votes)
8 views142 pages

Java Part 1

The document provides an overview of Java, highlighting its simplicity, platform independence, security, and object-oriented nature. It contrasts Java with C/C++, discusses the concept of programming languages, and explains different levels of programming languages. Additionally, it covers Java's features, memory types, and the compilation and execution process, including the roles of JDK, JRE, and JVM.

Uploaded by

izelxoxo563
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)
8 views142 pages

Java Part 1

The document provides an overview of Java, highlighting its simplicity, platform independence, security, and object-oriented nature. It contrasts Java with C/C++, discusses the concept of programming languages, and explains different levels of programming languages. Additionally, it covers Java's features, memory types, and the compilation and execution process, including the roles of JDK, JRE, and JVM.

Uploaded by

izelxoxo563
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/ 142

Concept-1 : WHY JAVA ?

1. Simple and Easy.

2. Platform Independent Language.

3.Automatic garbage collector.

4. It is secure.

5. It is Object Oriented.

To overcome the drawbacks of C/C++ java was introduced.

Difference between C/C++ and Java

C/C++ Java

 Not flexible Flexible


 Platform dependant Platform independent
 Does not provide security Provides security
 Does not provide automatic Provides automatic
garbage collector garbage collector
 Does not support multithreading Supports multithreading

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

3. The Electric signals are known as 0,1 which are in Binary.

4. While giving Instructions to the Computer be Specific like What,Where,..etc.

Concept-3 : WHAT IS A PROGRAMMING LANGUAGE ?

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 ?

 Low level language/Machine level language/Binary language


 Assembly level language
 High level language

 LOW LEVEL LANGUAGE

• LLL is a Language which is readable and understandable by only machines.


• It is called low level language because it is low in readability and understand ability
for humans.
• It is also called machine language because it is readable and understandable by
machines.
• This language is called binary language because it can accept only two characters i.e
0 and 1

Eg: 100001111111111000000000000001111111

Drawback
This language is difficult for humans to read and understand , only machines can understand

 ASSEMBLY LEVEL LANGUAGE

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

 HIGH LEVEL LANGUAGE

• To overcome the drawback of ALL, we go for HLL


• Language which is readable and understandable by Humans.
• Now humans can understand this language
• What about my computer? It can understand only BINARY or LLL
• So, we need a translator to convert HLL to LLL
• Who is the translator here? COMPILER

COMPILER

• Compiler is the software which converts HLL TO LLL

HLL LLL

COMPILER

It is understandable It is understandable
by humans by machines
Concept - 5 : What is Java ?

Java is a High Level Programming language.

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

THE LANGUAGE WHICH IS USED TO COMMUNICATE WITH THE COMPUTER IS CALLED AS


PROGRAMMING LANGUAGE.

What is program ?

Can computer perform any task on it’s own ?NO

Program is a set of instructions given to perform a specific task

WHAT IS JAVA USED FOR ?

 Java is used in Desktop GUI Applications.

 It is used in Web Applications.

 It is also used in Mobile Applications.

 It is used in web applications

 It is used in Gamming, Robotics etc.


Types of Memory:-

There are Two Types in Memory. They are


a) Primary Memory b) Secondary Memory

a) Primary Memory:-

● It is also Known as Temporary Memory / Volatile Memory / Cache Memory.


● It is used to Execute/Run the Data
● All the Running apps are stored in Primary Memory.
● The data present in Primary Memory can be deleted/cleared if the Power supply is Cut-Off.
● RAM is the Best Example for Primary Memory.

b) Secondary Memory:-

● It is the Storage House of the Computer.


● Every Data is Stored in Secondary Memory Only.
● It is also known as ' Permanent Memory '. ● Eg: Pen Drives, Cloud, Hard Disk, CD,... etc
FEATURES OF JAVA

 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

Performing multiple operations at a time.

Eg: In vlc media player video,audio,subtitles,recording will take place simultaneously


PLATFORM
What is a Platform?

windows os

32-bit architecture

linux os

32-bit architecture

Mac os

64- bit architecture


WHAT IS A PLATFORM ?

 It is a place where your operating system runs.


 It is a combination of hardware and software.

Platform :

 Platform dependant
 Platform independent

PLATFORM DEPENDANT

An application developed on one platform can be run on the same platform.

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 ?

Explain WORA ? Write once run anywhere .

JVM

JVM

JVM

 In java compiler will not directly convert source file to LLL.


 In the first step : Compiler will convert source file to class file which contains
bytecode
 Bytecode is neither understood by humans or machine
 Only System installed with JRE will understand bytecode
 Interpreter which is present inside JVM will convert bytecode to
Native machine understandable language.

THIS MAKES JAVA PLATFORM INDEPENDENT LANGUAGE.

Note : Java is platform independent but JRE dependent.


DIFFERENCE BETWEEN SOURCE FILE , CLASS FILE & EXECUTABLE
FILE

WHAT IS SOURCE FILE ?

It is a file in which instructions are written in High Level Language.

Extension is .java

WHAT IS CLASS FILE ?

It is a file which contains bytecode.

Extension is .class

WHAT IS EXECUTABLE FILE ?

It is a file which contains instructions in Low Level Language.

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

 It stands for Java Development Kit


 It contains Compiler , JRE
 It contains all the development tools required for developing and running
a java application

JDK

compiler JRE
COMPILER

• A java compiler is a software used for first compilation in java.


• Java compiler will convert a Source File into a class file by taking a
Sourcefile(.java file) as an input and giving a Class File(.class file) as output.
• It reads the Entire Source Code Once and generates a Class File. If there is
any syntax error it throws a Compile Time Error(CTE) and it will not generate
a Class File.

JRE

 Java Runtime Environment


 It contains JVM and class loader
 It contains inbuilt libraries
 Provides environment only to run or execute a java file

JRE

compi ler JVM Class


loader
Class Loader:-

 It loads all the java classes into the JVM


 It is used to load a Class File from Secondary Memory to JRE.

JVM

 It stands for Java Virtual Machine


 It consists of Interpreter & JIT Complier
 It is responsible for execution of program Line by line

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

• It is used to increase the Efficiency of Interpreter/Execution by saving time.


• It is also for Memory Management.

JDK DIAGRAM

IMPORTANT QUESTION

1. Difference between compiler and interpreter

2.Difference between JDK,JRE,JVM


Command Prompt

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

Syntax to represent drive :

Drive Name :
Eg: D:

C:

Commands

Programs designed to perform a specific task.

Eg : mkdir , cd , javac

What is directory ?

It is folder.
Basic commands

 mkdir--> make directory-->used to create a


directory(folder)

 cd--> change directory---> used to change the directory

 ..--->It will take back one directory

 mkdir -->make directory-->used to create a directory

 dir-->directory-->it is used to list all the directories

 cls--->clear screen--->used to clear the screen

 rmdir-->remove directory--->used to delete directory


from current working directory

 ren-->rename-->used to rename the folders


Example Using commands
Structure of Java Program

class ClassName
{

public static void main(String[]args)


Class
block {
Main block
// Statements

FileName.java

 Every program in java will start with “class”

 Every class has a ClassName followed by a class block

 Execution of java program starts from main() method and it is declared as:

public static void main(String[]args)

Who calls main method ? JVM

NOTE :

 File name should be same as ClassName


PRINT STATEMENTS:

We can print the data with the help of print statements.

1. System.out.print(data);

 It is used to print data only.

 We cannot use print statement without passing data.

System.out.print( ); Compile Time Error

Example:

System.out.print(“Hi”);

System.out.print(“Bye”);

Output : HiBye

2. System.out.println(data);

 It is used to print data and create new line

 We can use println without passing data

System.out.println( ); Compile time successful

Example:

System.out.println (“Hi”);

System.out.println (“Bye”);

Output :

Hi

Bye
STEPS TO COMPILE AND EXECUTE A JAVA FILE

1. Compile

Command : javac FileName.java

2. Run or Execute

Command : java ClassName

Note : FileName and ClassName should be same to avoid confusion.

Important Interview Questions

 Can we compile a class without main method ?


YES .

 Can we run or execute a class without main method ?


NO

Eg:

class Demo

javac Demo

Compile Time Successful

Java Demo

Run Time Error


 Can FileName and ClassName be different ?
YES it can be different

But to compile we should use FileName


Javac FileName.java

To run we should use ClassName


Java ClassName

 Class Demo
{
System.out.println(“Hi”);
}

OUTPUT

Compile Time Error

Why ? No main method

 Class Demo
{
public static void main(String[]args)
{
System.out.println(“Hi”);

}
}

OUTPUT
Hi
QUESTIONS ON STRUCTURE OF JAVA
TOKENS

Smallest unit of programming language which are used to build a program.

Eg :

Build a bike Similarly to build java program

Engine Tokens (small units of a


Brakes program)

tyres small components of bike

seats
wheels

TOKENS IN JAVA

 Keywords
 Identifiers
 Literals
 Separators
 Comments
 Operators
1. Keywords

 Predefined words or compiler aware words


 Each word will have a specific meaning
 Programmers cannot change the meaning of those words
 In current version there are 50 keywords in java

Eg: public static void class

NOTE : KEYWORDS ARE ALWAYS WRITTEN IN lowercase

Can you change the meaning of cloud or sun or moon ?


No. Similary keywords are some predefined words whose
meaning cannot be changed.
NOTE : ***

true , false and null are not keywords


2. Identifiers: -

The name given to the components of java by the programmer is known


as identifiers.

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 '$'.

E.g. : class Program% CTE


class _Program CTS
class Program$ CTS

3. character space is not allowed in an identifier.

E.g. : class Logical Program CTE


class LogicalProgram CTS
4. we cannot use keyword as an identifier.

E.g. : class void CTE


class Public CTS (Public is not keyword.It is not in lowercase)

QUESTIONS

Identifiers can not start with?


Characters
Digits
underscore ( _ ) symbols
None of the above

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

Which of the following are valid identifiers?


Please select all the correct answers.
A. ident-fier
B. $identifier
C. 2identifier
D. a_long_identifier_59

Ans : B,D

class _
{

OUTPUT : CTE ( _ is a reserved word and cannot be used as identifiers)


class
{

OUTPUT : Compile time successfull

class true
{

Ans : true is reserved word and cannot be used as identifiers)

class false
{

Ans : false is reserved word and cannot be used as identifiers)


3. LITERALS

The value or data used in the program are called literals.


It is of 2 types :

 primitive literals
 non primitive literals

PRIMITIVE LITERALS

Number Character Boolean

1. NUMBER

INTEGER FLOATING POINT (decimal)

Positive Negative Positive Negative


Eg : 9 , 10, 5 -10 , -8 , -4 6.5 , 8.9 ,4.4 -7.5 , -6.8 ,-100
2. CHARACTER LITERAL

 Any data enclosed in single quote(‘ ‘) is called a character literal


 The maximum and minimum length is 1
Eg : ‘a’ ‘@’ ‘j’ ’v’

Note :

‘’ (length=0) compile time error

Empty single quote gives compile time error because minimum and
maximum length of char literal should be 1.In this case it is 0.

‘‘ (length=1)Compile Time Successful


Here space is also a character so it is compile time success

‘ab’ CTE
Length is 2

3. BOOLEAN

Java supports Boolean data to achieve logical operations


Logical high : 1 : true
Logical low : 0 : false
NON PRIMITIVE LITERALS

 String
 It is a non primitive literal
 Any data enclosed in double quote (“ “) is called a String literal.
 No defined or fixed length

Eg: “Lavanya” “” “abc” “j” “ “

NOTE :

“” length=0 Compile time successful


Because in String the length can be anything

4.COMMENTS

 Information about the program


 Compiler and Interpreter will ignore these statements
 They are of two types

Single line comments

//This is single line comment

Mult i line comments


/*This is multi
line comment */

5. Separators

Anything which is used to differentiate one instruction from another instruction or


end of a statement or one block from another block is called separator.

 They are also called punctuators

{ } - denotes start and end of blocks

; - end of statements

( ) - used to call or declare a method

, - used to separate values

. Takes you to particular address

[ ] –used to define array elements


VARIABLES

Variable is a container which is used to store data.


Definition : Variable is a named block of memory which is used to
store data.

age=20; age 20 here 20 is the value


It is integer

height=5.7 height 5.7 here 5.7 is the value


It is decimal

We know that 20 is integer and 5.7 is decimal.


Will the compiler know ?
NO
We need to tell the compiler what type of data you are going
to store in the variable.
How to tell ? We will make use of datatypes

DATATYPES

Datatypes are used to indicate the type of data stored into a


variable.
Datatypes also specifies the size of data.

They are of 2 types :


 Primitive datatype
 Non primitive datatype
Primitive datatype:-

Datatype which is used to create a variable to store primitive value such as numbers,
characters, Boolean is known as primitive datatype.

Note:- All primitive datatypes are keywords in java.

There are 8 types of primitive data types:

Non primitive datatype :

Eg : String

Note : Every class in java is an example for non primitive datatype.


STEPS TO CREATE A VARIABLE

1. Variable declaration.
2. Variable initialization.
3. Variable declaration and initialization.

Variable declaration:-

Declaring a variable and not initializing a value to it is called as variable declaration.

Syntax:-

datatype variableName ;

Eg : int age;

Variable initailzation:-

Initializing a declared variable is known as variable initialization.

Syntax:-

variableName = value;
age 35
Eg : age=35;
Variable declaration and initialization:-

Declaring a variable and initializing a value to it is called as variable declaration and


initialization.

Syntax:-

datatype variableName = value ;

Eg: double height = 5.8 ; height 5.8

char gender = ‘F’ ; gender F

NOTE :

For long datatype always mention l / L

For float datatype always mention f/F


Programs on Variables and datypes
class Demo2 . Command Prompt
B{
public static void main(String[] args) C:\WECO23\Variables>javac Demo2.java
日{
System.out.println("Details of Saurabh"); C:\WECO23\Variables>java Demo2
Details of Saurabh
String name;// declartion Saurabh
20
name="Saurabh";//initialization 6

int age;// declaration C:\WECO23\Variables>

age=20;//initialized

int height;//declaration

height=6;// initialization

//print the details

System.out.println(name);
System.out.println(age);
System.out.println(height);

}
}

1 2 +---3 -4+ 5 6 +--7


1 class Detail
2日
3 public static void main (String [] args)
4日 {
5 String name
= Rahul";
"

6 int age 20 ;
=

7 double height = 5.6;


8 char gender = 'M' ;
9
0 System.out.println(name) 3
1 System.out.println(age) ;
2 System.out.println(height) ;
3 System.out.println(gender) ;
4
5 /*
6 Rahul
7 20
8 5.6
9 M
0 */
1 }
2
SCOPE OF VARIABLE

The visibility of variable is called as scope of variable.

Or

Region from which we can access the variable

Variables are of 2 types based on visibility :

 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 ?

Directory Cliptex -1 -2- ---3----+----4----+ -5 -6--+----7---+--8---+---9--+-0----1--+-


[C:] 1 class А
C 2日 {
java_selenium 3 public static void main(String[] args)
{
part1 4日
variables
5 int a=10;//local variable
6
System.out.println(a);
7 }
8 }
9

Demo1.class
Demo1.java
Demo1.java.bak
Demo2.class
Demo2.java

All Files (*.*

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

21°C Mostly cl... ENG 19:28


미 ☑
C:\java_selenium\part1\scope\A1.java - EditPlus
File Edit View Search Document Project Tools Browser Emmet Window Help x
G
3 單 Π X ‫م‬ A Hx ?

Directory Cliptex -1 -2- -+----3--+----4----+----5 -----6-+-7-+-8-+----9-+-+-1


[C:] 1 class A1
CA 2日
java_selenium 3 public static void main(String[] args)
part1 4日 {
variables
5 int a;//cannot use local variable without initialization
6 System.out.println(a);
7 }
8 }
9

Demo1.class
Demo1.java
Demo1.java.bak
Demo2.class
Demo2.java

All Files (*.* <

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

21°C Mostly cl... ENG 19:28


C:\java_selenium\part1\scope\A2.java EditPlus
-

File Edit View Search Document Project Tools Browser Emmet Window Help ☑

Π X ‫م‬ B A Hx W ?

Directory Cliptex -1----+- -2- ---3----+- -4- 5- --6----+----7----+----8----+----9----+----0----+----1----+


[C:] 1 class А2
CA 2日
java_selenium 3 public static void main(String[] args)
part1 4日 {
variables
5 int a;
6 int b=10;
7 System.out.println(b);
8 }
}
10
Demo1.class
Demo1.java
Demo1.java.bak
Demo2.class
Demo2.java

All Files (*.*) <

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

21°C Mostly cl... ENG 19:28



C:\java_selenium\part1\scope\A3.java - EditPlus
File Edit View Search Document Project Tools Browser Emmet Window Help x
G
Π x ‫م‬ B A Hx W

Directory Cliptex -1- ----2- ---+----3----+----4---- -5 -6-+-7-+----8-+-9----+---+-1


[C:] 1 class АЗ
CA 2日 {
java_selenium 3 public static void main(String[] args)
{
part1 4日
variables
5
System.out.println(int a=10);
6 //cannot declare and initialize inside print statement
7
8 }

Demo1.class
Demo1.java
Demo1.java.bak
Demo2.class
Demo2.java

All Files (*.*)

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

21°C Mostly cl... ENG 19:28 Π


C:\java_selenium\part1\scope\A4.java - EditPlus
File Edit View Search Document Project Tools Browser Emmet Window Help
G
3 單 Π X ‫م‬ B A Hx W =

Directory Cliptex -1 -2- 3----+--- -4----+ -5 -+-6789-+-+1

[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

All Files (*.*

A.java A1.java A2.java A3.java A4.java A5.java A6.java A7.java 8


A8.java A9.java A10.java A11.java 8 A12.java Demo1.java Demo2.java
C:\java_selenium\part1\scope\A4.java In 9 col 1 9 00 PC ANSI

21°C Mostly cl... ENG 19:28


C:\java_selenium\part1\scope\A5.java - EditPlus 미
File Edit View Search Document Project Tools Browser Emmet Window Help ☑
G
X ‫م‬ дa Hx W ?

Directory Cliptex -1 -2- -4- -5- --6-+--7----+--8---+--9--+----0------1---+


[C:] 1 class A5
CA 2日
java_selenium 3 public static void main(String[] args)
part1 4日 {
variables
5 int a=10;
6 a=20;
7 a=30;
8
System.out.println(a);
9
10 }
Demo1.class 11
Demo1.java
Demo1.java.bak
Demo2.class
Demo2.java

All Files (*.*)

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

21°C Mostly cl... ENG 19:28


C:\java_selenium\part1\scope\A6.java EditPlus
-

File Edit View Search Document Project Tools Browser Emmet Window Help
G
單 X ‫م‬ да Hx W

Directory Cliptex -1- -2- ---+----3-+----4--- -5 --+-6-+-7-+-8-9-+-+-1-

[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

All Files (*.* <

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

21°C Mostly cl... ENG 19:28


C:\java_selenium\part1\scope\A7.java - EditPlus
File Edit View Search Document Project Tools Browser Emmet Window Help
Σ G
X ‫م‬ B да Hx W ?

Directory Cliptex -1- -2- -+----3----+- -4- -5- +----6----+----7----+----8----+----9----+----0----+----1----+


[C:] 1 class А7
CA 2日
java_selenium 3 public static void main(String[] args)
part1 4日 {
variables
5 int a=10;
6 int a=20;//cannot declare variable with same name (СТЕ)
7 System.out.println(a);
8 System.out.println(a);
9 }
10 }
Demo1.class 11
Demo1.java
Demo1.java.bak
Demo2.class
Demo2.java

All Files (*.*)

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

21°C Mostly cl... ENG 19:28


C:\java_selenium\part1\scope\A8.java EditPlus
-

File Edit View Search Document Project Tools Browser Emmet Window Help
G
3 Π X ‫م‬ B да Hx W =

Directory Cliptex -1- -2- -+----3--+----4- -5 ------6-7-+-8-9-+-+-1-

[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

All Files (*.* <

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 9 col 1 9 00 PC ANSI

21°C Mostly cl... ENG 19:28


C:\java_selenium\part1\scope\A9.java - EditPlus 미
File Edit View Search Document Project Tools Browser Emmet Window Help ☑
G
‫م‬ B д Нх W ?

Directory Cliptex -1- -2- -3----+- -4- 5----+----6----+----7----+----8----+----9----+----0----+----1----+


[C:] 1 class A9
C 2日
java_selenium 3 static int a=10;//global variable
part1
variables
4 public static void main(String[] args)
5日 {
6 int a=20;//local variable
7 System.out.println(a);
8
9 }
10
Demo1.class 11 }
Demo1.java 12
Demo1.java.bak
Demo2.class
Demo2.java

All Files (*.*

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

G 21°C Mostly cl... ENG 19:28


C:\java_selenium\part1\scope\A10.java EditPlus
-

File Edit View Search Document Project Tools Browser Emmet Window Help ☑
G
Σ Π X ‫م‬ B B A Hx W = ?

Directory Cliptex -1- -2----+----3---- -4--- -5- +----6----+----7----+----8----+----9----+----0----+----1----+


[C:] 1 class A10
C 2日
java_selenium 3 static int a=100;
part1 4
variables
public static void main(String[] args)
5日 {
6 int a=200;
7 System.out.println(a);
8 System.out.println(A10.a);//by using class name
9 }
10 }
Demo1.class 11
Demo1.java
Demo1.java.bak
Demo2.class
Demo2.java

All Files (*.* <

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

G 21°C Mostly cl... 요 ENG 19:28 Π


C:java_selenium\part1\scope\A11.java - EditPlus 미
File Edit View Search Document Project Tools Browser Emmet Window Help ☑
G
Σ 單 Π X ‫م‬ B Q да Hx W = ?

Directory Cliptex -1- -2- -+----3--+ -4---+- 5678-+-9-1+


[C:] 1 class A11
CA 2日
java_selenium 3 static int a=10;
part1 4 public static void main(String[] args)
variables
5日 {
6 System.out.println(a);
7 int a=20;
8 System.out.println(a);
9 }
10 }
Demo1.class 11
Demo1.java
Demo1.java.bak
Demo2.class
Demo2.java

All Files (*.* <

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

G 21°C Mostly cl... ENG 19:28


C:\java_selenium\part1\scope\A12.java EditPlus
-

File Edit View Search Document Project Tools Browser Emmet Window Help ☑
G
Π X ‫م‬ B A Hx ?

Directory Cliptex ---1- -2- -+----3----+- -4----+- -5- +----6----+----7----+----8----+----9----+----0----+----1----+


[C:] 1 class A12
C 2日
java_selenium 3 static int a;
part1 4 static double b;
variables
5 public static void main(String[] args)
6日 {
7 System.out.println(a);
8 System.out.println(b);
9 }
10 }
Demo1.class 11
Demo1.java
Demo1.java.bak
Demo2.class
Demo2.java

All Files (*.* <

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

21°C Mostly cl... ENG 19:28


TYPE CASTING:
The process of converting from one datatype to
another datatype is known as type casting.
It is classified into two types.
PRIMITIVE TYPE CASTING
NON PRIMITIVE TYPE CASTING
PRIMITIVE TYPE CASTING
The process of converting from one primitive
datatype to another primitive datatype is known as
PRIMITIVE TYPE CASTING
BYTE
SHORT

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

NARROWING (here data will be loss)


The process of converting from larger range of
data to smaller range of data is known as narrowing.
It is explicit process, where the complier will not
Allow the programmer to perform narrowing implicitly.
Therefore the program has to perform narrowing
explicitly by using type cast operator

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

DATA TYPE DATA TYPE RESULT


1 2
byte byte int
short short int
int int int
char char int
long long long
float float float
double double double
NOTE:
If we perform arithmetic operations for two
operation for two different datatypes the result type will
be based on largest datatype.

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

Difference between modulus and divided


DIVIDE MODULUS
The divide operator will The modulus operator will
return quotient return remainder

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.

OPERATOR EXAMPLE EXPRESSION


+= a+=2 a= a+2
-= a -=1 a = a-1
*= a*=3 a=a*3
/= a/=2 a=a/2
%= a%=2 a=a%2
EXAMPLE.
Class demo
{
Public static void main (String[ ]args)
{
int i =120;
System.out.println(i); //120
i+=2; //i=i+2
System.out.println(i); //122
i-=2;
System.out.println(i); //120
i*=2;
System.out.println(i); // 240
i/=5;
System.out.println(i); // 48
i%=10;
System.out.println(i); //8
}
}

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.

It is classified into 3 types.

Operators Symbols
And &&
Or //
Not !

AND OPERATOR( && )


The and operator is the binary operator
It returns true, only when both the operands are
true.
Hence it returns false
OPERAND 1 OPERAND 2 RESULT

1(TRUE) 1(TRUE) 1(TRUE)


1(TRUE) 0(FALSE) 0(FALSE)
0(FALSE) 1(TRUE) 0(FALSE)
0(FALSE) 0(FALSE) 0(FALSE)

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.

OPERAND 1 OPERAND 1 RESULT

1(TRUE) 1(TRUE) 1(TRUE)


1(TRUE) 0(FALSE) 1(TRUE)
0(FALSE) 1(TRUE) 1(TRUE)
0(FALSE) 0(FALSE) 0(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.

1<2 // 2>5 = TRUE


TRUE

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

? and : are the two conditional


operator.
It is a ternary operator.
It is used to return the data based on conditions.
SYNTAX:
OPERAND 1 ? OPERAND 2 : OPERAND 3
TRUE / FALSE

OPERAND 1 = BOOLEAN (TRUE OR FALSE)


OPERAND 2 = Any type of data(ie, string,int)
OPERAND 3 = Any type of data
Write a java program to check whether the given
number is odd or even.
Class demo
{
Public static void main (String[ ]args)
{
int n =19;

String b = n%2= =0 ? “ it is even number “ :


“it is odd number” ;
System.out.println(b);
}
}
OUTPUT:
It is odd number
INCREMENT AND DECREMENT OPERATOR
It is an Unary operator
It is used to update the value of existing
container(variable ) by 1.
INCREMENT OPERATOR:
It is used to increase the value of existing container
by 1.
It is classified into two types.
 PRE INCREMENT
 POST INCREMENT

PRE INCREMENT
It is denoted by ++ variable name.
The pre increment will works as follows.

Update the variable by 1


Substitute the updated value
Use the substituted value.
EXAMPLE

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.

STEPS TO USE SCANNER CLASS:


1.import the scanner class from java.util package.
Import java.util.scanner ; (for single or particular class
we can use this one)
Import java.util.* ; (here we can access all class )
NOTE : The import statement should be the first
instruction in the java file.
2. Create object for the scanner class
Scanner scn = new Scanner(System.in)

Object (we can give any name)


3. Call the methods of scanner class based on the type
of data.
1. byte= scn.nextByte( );
2. short= scn.nextShort( );
3. int= scn.nextInt( );
4. long= scn.nextLong( );
5. float= scn.nextFloat( );
6. double= scn.nextDouble( );
7. boolean= scn.nextBoolean( );
8. char= scn.next( ).CharAt(0);
9. string= scn.next( ); -------for single word
scn.nextLine( ); ----for multiple
word
Read the username, contact number, age location,
pincode.
import java.util.*;
class user
{
Public static void main (string[ ]args)
{
Scanner abc = new Scanner (System.in);
System.out.println(“Enter the name”);
String name = abc.nextLine( );
System.out.println(“Enter your contact number”);
long contact number = abc.nextLong( );
System.out.println(“Enter your age”);
int age = abc.nextInt( );
System.out.println(“Enter your location”);
String location = abc.next( );
System.out.println(“user details”);
System.out.println(“name : “ +name);
System.out.println(“contact number : “ +contact
number);
System.out.println(“age : “ +age);
System.out.println(“location : “ +location);
}
}
OUTPUT:
Enter the name
Karthik
Enter the contact number
9750093856
Enter the age
25
Enter the location
Bangalore

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 ?

FALSE IF BLOCK GET


EXECUTED
ELSE
BLOCK GET
EXECUTED

NORMAL WORK FLOW


Write the java program to find the largest of 3 numbers
using if else if statements
Class demo
{
Public static void main(String[ ]args)
{
int a=20,b=100,c=200;
if(a>b && a>c)
{
System.out.println(a + “is the largest number”);
}
else if (b>c)
{
System.out.println(b + “is the largest number”);
}
else
{
System.out.println(c + “is the largest number”);
}
}
}

OUTPUT:
C is the largest number

SWITCH STATEMENT :(matching the same value like


pattern matching)
User given value Case
A C
B A
C B

Switch statement is used for pattern matching.

switch
default
case all are keywords.
break
SYNTAX:

switch (value/variable/expression)
{
case(value/expression):
{
// statements;
}
break;
..
..
..
..
default:
{
// statements;
}
}

WORKING FLOW:

The (value/variable/expression) given to the


switch get compared with the value or expression
given to the case.
If any of the case is matching with the value
the case block get execute and the control is
transferred to the below cases to stop the flows of
execution between the cases we can use break.

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:

The looping statements are used to execute the


block of instructions repeadtly. (again and again)

We have looping statements like


While loop
Do while loop
For loop
Nested for loop

NOTE: To use looping statements are have to follow


the 3 important steps.
Initialization
Condition
Updation

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

Write the program to print the numbers in the given


range.
Class demo
{
public static void main (String[ ]args)
{
int start =5; // 6,7,8,9,10
int end =10;
do
{
System.out.println(start);
Start ++;
}
while (start<=end)
}
}

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:

boolen b =false; boolen b =false;


while (b) do
{ {
System.out.println(“Hello”); System.out.println(“Hello”);
} } while (b)

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

FALSE CONDITION TRUE


?

For block get


execute

Updation

NORMAL WORK
FLOW

In for loop the control goes to segment 1 (initialization),


then it checks the condition.
If the condition is true the for block get executed, after
executing the control goes to the updation.
After updation again it checks the condition, if the
condition is true the process will be continued else it
comes to the normal flow (false).

NOTE: In for loop by default the condition will be true.


if the programmer for to give condition for compile
time success and it becomes infinite loop.
Example:
Class demo
{
public static void main(string[ ]args)
{
for( ; ; ) default value is true
{
System.out.println(“Hello”);
}
}
}
OUTPUT:
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
.
.
.
It goes n number of times. (multiple looping)
Write a java program to print the factors of the given
number.
Factors means: if we get remainder as zero(0), that
we called as factors of any numbers(for example =7)

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

Return statement :(we can use return statement for what


kind of datatype to be used).
The return statement is used to return the value from
one method to another method(calling method to called
method).
We can return with the help of return keyword.
Return
It is a keyword and it is used to return the value
which is specified in the return type.

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.


Parameterized method//Argument method

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.

WORKING FLOW OF METHOD CALL


STATEMENT:
The execution of calling method will be paused.
The control is given to the called method.
The called method begins the execution.
Once the execution of called method completed
the control will be transferred back to the calling
method.
The execution of calling method resumes.
EXAMPLE:
public static void main(String[ ]args)
{
System.out.println(“main starts”);
m1(true/false);
}
public static void m1(boolean a)
{
System.out.println(“parameterized method”);
}

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( );
}

The above example the m1 method is calling itself at


same point memory get fill because of infinite method
call .Therefore we will get run time error(stack over
flow).

You might also like