Vous êtes sur la page 1sur 114

www.besthinditutorials.

com

Java in Hindi
Third Edition
By Prof. V.K. Sharma

2015

www.besthinditutorials.com

You can download this EBook from www.besthinditutorials.com

Java in Hindi
Third Edition
V.K. SHARMA

www.besthinditutorials.com

You can download this EBook from www.besthinditutorials.com

Copyright 2015 by www.besthinditutorials.com


This EBook may not be duplicated in any way without the express written consent of the
publisher, except in the form of brief excerpts or quotations for the purpose of review. The
information contained herein is for the personal use of reader and may not be incorporated
in any commercial programs, other books, databases, or any kind of software without written
consent of the publisher. Making copies of this EBook or any portion thereof for any purpose
other than your own is violation of copyright laws.

This edition has been published by www.besthinditutorials.com

Limits of Liability/disclaimer of Warranty: The author and publisher have used their best
efforts in preparing this EBook. www.besthinditutorials.com makes no representation or
warranties with respect to the accuracy or completeness of the contents of this book, and
specifically disclaims any implied warranties which extend beyond the descriptions contained
in this paragraph. No warranty may be created or extended by sales representatives or
written sales materials. The accuracy and completeness of the information provided herein
and the opinions stated herein are not guaranteed or warranted to produce any particular
results and the advice and strategies contained herein may not be suitable for every
individual. www.besthinditutorials.com shall not be liable for any loss of profit or any other
commercial damages, including but not limited to special, incidental, consequential, or other
damages.

Trademarks: All brand names and product names used in this EBook are trademarks,
registered trademarks, or trade names of their respective holders.
www.besthinditutorials.com is not associated with any product or vendor mentioned in this
EBook.

Reprint Edition: 2013

Published By: www.besthinditutorials.com

You can download this EBook from www.besthinditutorials.com

Contents
1.

2.

3.

4.

5.

6.

7.

8.

9.

INTRODUCTION TO JAVA
1.1. History of java
1.2. Features of java
1.3. OO principles of java
1.4. Java vs. c and c++
INSTALLING JAVA
2.1. Java development tools
2.2. Downloading java
2.3. Installing java and setting environment variables
2.4. Download eclipse
2.5. Installing eclipse
FIRST JAVA PROGRAM
3.1. Running a java program
3.2. Hello world program
DATA TYPES
4.1. What are data types?
4.2. Integer
4.3. Float
4.4. Character
4.5. Boolean
VARIABLES
5.1. What are variables?
5.2. Types of variables
5.3. Creating variables
STRINGS
6.1. What are strings?
6.2. Operations on strings
ARRAYS
7.1. What are arrays?
7.2. Creating an array
7.3. Initializing an array
7.4. Displaying an array
OPERATORS
8.1. What are operators?
8.2. Types of operators
Control Statements
9.1. What are control statements?
9.2. Types of control statements

You can download this EBook from www.besthinditutorials.com

10. CLASSES
10.1. What are classes?
10.2. Advantages of classes
10.3. Creating a class
11. OBJECTS
11.1. What are Objects
11.2. Steps to create an object
12. CONSTRUCTORS
12.1. What are constructors?
12.2. Rules for creating constructors
12.3. Types of constructors
13. METHODS
13.1. What are methods?
13.2. Structure of method definition and method call
14. INHERITANCE
14.1. What is inheritance?
14.2. Types of inheritance
14.3. Inheriting a class
14.4. Example
15. Exception Handling
15.1. What are exceptions?
15.2. Keywords(try, throw, catch)
15.3. Example
16. Interfaces and packages
16.1. What are interfaces?
16.2. Example
16.3. What are packages?
16.4. Creating a package
16.5. Using a package
16.6. Some common java packages
17. Multithreading
17.1. What is multithreading?
17.2. Thread class
17.3. Runnable interface
17.4. Synchronization
18. File I/O
18.1. What are streams?
18.2. Byte streams
18.3. Character streams
19. Serialization
19.1. What is serialization?

You can download this EBook from www.besthinditutorials.com

20.

21.

22.

23.

24.

25.

26.

27.

19.2. Serializable interface


Collections Framework
20.1. Collection interface
20.2. Other interfaces
Lambda Expressions
21.1. What is functional interface?
21.2. What is lambda expression?
Generics
22.1. What are generics?
22.2. Generic classes
22.3. Generic methods
Applets
23.1. What are applets?
23.2. Applets life cycle methods
23.3. Example
AWT(Abstract Windows Toolkit)
24.1. What is AWT?
24.2. Container classes
24.3. Component classes
24.4. Frame class
Servlets
25.1. Architecture of servlets
25.2. Life cycle of servlets
25.3. Example
Swing
26.1. What is swing?
26.2. Features of swing
26.3. Swing classes
26.4. Example
Index

You can download this EBook from www.besthinditutorials.com

Preface
The idea of writing a book about Java Development in Hindi can be somewhat futile at times,
considering the pace at which Java continues to expand, morph and change. What started
out as a book project two years ago has now become a series of updates to the original work
with the page count nearly double the original project and that after making hard decisions
about what to leave out of the book to make sure it gets liked.
This update to Java in Hindi represents our latest effort to provide coverage on important
Java Development topics, namely the expansion into the Swing.
Like many things in life, the only constant is change, but by now we are somewhat
accustomed to the rapid-fire environment of Java Development. To the end, we have
ensured that all the programs work with eclipse as well as without eclipse.
The third edition is written by Vipin Kumar Sharma. Thank you for learning and collaborating
with us.

VIPIN KUMAR SHARMA

You can download this EBook from www.besthinditutorials.com

Acknowledgement
Writing a third edition of Java in Hindi feels somewhat like the old saying about weddings:
Something old, something new The deadline for the third edition did not become any
easier as at last count there are still only 24 hours in the day. And as for something new it
seems as though Javas pace of innovation is continuing to match its adoption rate by users
around the globe.

In particular, wed like to acknowledge and thank everyone at www.besthinditutorials.com.


Firstly I would like to thank our EBook Designers and editors, who have been involved in
every aspect of now three editions of this project. I would also like to thank our publisher to
bring this masterpiece in EBook format.

You can download this EBook from www.besthinditutorials.com

About this book


Java in Hindi, Third Edition is a revision and update of the second edition published in 2013.
This third edition adds new content related to java 8. Like its predecessors, this book covers
important beginner topics such as What is Java? and installing and using the development
environment. We than advance to practical working examples of core programming topics
any developer will be happy to have at the ready on the reference shelf. The remaining
chapters present detailed example applications covering advanced topics, including AWT
and Swing.
Although you can read the book from start to finish, you can also consider it a few books in
one. If you are new to Java focus first on beginning chapters. With the foundation you can
work your way up to last chapter.

Who should read this book?


We wrote this book for professional programmers and hobbyists alike. Many of the concepts
can absorbed without specific Java language knowledge. If you have c, c++ or c#
knowledge, you will be able to follow the examples.
Prior eclipse experience is helpful, but not required. A number of good resources are
available on java and eclipse to augment the content of this book.
And lastly the person who is willing to learn java in his mother tongue Hindi should read this
book.

Road map
1. Basic first 10 chapters contain basic information regarding java programming.
2. Intermediate next 10 chapters are somewhat of intermediate level and necessary
java topics.
3. Advanced remaining chapters covers some advanced topics of java language.

You can download this EBook from www.besthinditutorials.com

Chapter 1
Introduction
This chapter covers

History of Java
Features of Java
OO Principles
Java vs. c and c++

History of Java
Java general purpose object oriented

. Sun Microsystems

. Java

U.S
develop

. Java

Java James Gosling


Java

oak

java

develop

java platform independent

execute

java 1991

. Java

. Java

java

java

Features of Java
java

java

java

1. Compiled and Interpreted


compiled
approaches combine
compile

interpreted

two

generate

instructions
generate

java
.

java

directly execute

You can download this EBook from www.besthinditutorials.com


2. Platform Independent
Java

JVM

generate

generate

execute

JVM

machines

JVM

. Java

compile

java

JVM (java virtual machine)


JVM

JVM

generate

generate

.
.

3. Object Oriented
Java true

. Java

. Java

4. Robust and Secure


Java robust

java

technologies

crash

reliable

. Java

JVM

Java

environments

invalid combinations

generate

JVM

5. Distributed
Java

distributed applications

networks

. Distributed applications

task

applications

RMI

. Java

6. Simple and Familiar


Java

. Java

c c++

java easy to

declarations, control statements, methods declarations


understand

variables

java c c++

c c++

java

familiar
java

7. Multi-threaded and interactive


Java multi-threaded
java

8. High Performance

java
.

You can download this EBook from www.besthinditutorials.com


Java performance
c c++

impressive . Java

reason

java architecture

. Java

java

9. Dynamic and Extensible


Java dynamic

. Java

dynamic

Java

java extensible

10. Ease of Development


develop

Java

. Java

.
.

Object oriented principles of java

java

. Java

principles

1. Encapsulation
Encapsulation data hiding

(variables)

. Encapsulation
.

bind

(Methods)

. Encapsulation

. Java 3

hiding

Public -

Private -

Protected -

2. Inheritance

You can download this EBook from www.besthinditutorials.com

. Java

allow

java

3. Polymorphism
Polymorphism

"

according

". Polymorphism

actions

4. Abstraction
Abstraction java

concept .
same

Java vs. c and c++


Java vs C
object oriented programming language

1.

program objects

C procedural language .
C

functions

.
interpreted compiled language

2.

language

directly
JVM

convert

4.

convert

convert

high level language

3.

C compiled language . C

C low

.
language . C language machine

.
bottom up approach

C top down approach

.
pointers

5.

pointers handle

.
memory automatically

6.

8.
error

. C overloading

error

automatic garbage collection

overloading support

7.

exception handling

.
C

You can download this EBook from www.besthinditutorials.com

Java Vs C++
1. C++ structures, unions, templates, operator overloading pointers support

2. C++

collection

3.

threads

destructors

built in support

. C++

C++

automatic garbage

threads third party

scope resolution operator(::)

resolution operators

5. C++

goto statement

6. C++

method overloading

method overloading

default arguments

arguments

support

libraries
4. C++

8. C++ compiled language

goto statement

operator overloading support

compiled

default

.
.

operator overloading

7. C++

platform independent

interpreted

You can download this EBook from www.besthinditutorials.com

Chapter 2
Installing Java
This chapter covers

Java development tools


Installing java and eclipse

Java Development Tools

JDK(Java Development Kit)


JDK

. JDK

JRE

execution

execute

JRE(Java Runtime Environment)


execution

JRE (Java Runtime Environment )

. JRE

execute

JRE 2
1.

2.

execution

JVM(Java Virtual Machine)

Machine ) environment
JRE

compile

convert

. JVM (Java Virtual

convert

You can download this EBook from www.besthinditutorials.com

Installing java and eclipse


Java

command prompt

command prompt version

java

official

editor

IDE

environment
NetBeans IDE milega

Eclipse

Java command prompt platform NetBeans IDE

http://www.oracle.com/technetwork/java/javase/downloads/index.html

Eclipse IDE


http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/marsr

You can download this EBook from www.besthinditutorials.com

Chapter 3
First Java Program
This chapter covers

Running a java program


Hello world program

Running a java program

notepad

console

console

IDE use

java

1. Through IDE (Integrated Development Environment)


2. Through Console
Through IDE
IDE

IDE

IDE

Eclipse, NetBeans
IDE Java Compiler execute
.

You can download this EBook from www.besthinditutorials.com

Through Console
Console
1. Run

program
cmd

2. Command prompt

steps

command prompt

drive

install

bin folder locate

3.

enter

image

javac

You can download this EBook from www.besthinditutorials.com

4.
MyProgram.java.

prompt

.java extension

javac MyProgram.java

errors
executable file

javac command
enter

You can download this EBook from www.besthinditutorials.com


java

5.

java MyProgram.

Hello world program


simple

copy

lecturer

board

simple

You can download this EBook from www.besthinditutorials.com

notepad

java program

chapter 2

inverted commas (" ")

message

main

HelloJava

println

description

public - public access modifier . Access modifiers 3


protected)

members

(public, private,

tutorials

class -

important concept

static

HelloJava keyword

method

main

program execute

access

main -

main

System.out.println () command prompt screen

java run time environment

execution

string array command line arguments

execute

. Class

String args [] -

argument

system package , out

, println
.

You can download this EBook from www.besthinditutorials.com

Chapter 4
Data Types
This chapter covers

What are data types?


Integer
Float
Character
Boolean

What are data types?


Data, data data.

available

according

???

.
operations

operations

perform

operations

and Division)

(Addition, Subtraction ,Multiplication

perform

categories

Integer
Integers

(
4

) include
.

Byte

Example - int age = 40;

. -127

128

.
.

You can download this EBook from www.besthinditutorials.com

Short

16

32,767

-32,768

.
Example - short sal = 25,000
Int
Int

32

commonly

2,147,483,747

. Int

-2,147,483,648

Example - int population = 2,00,000,000;


Long

64

Example - Long WorldPopulation = 8,00,00,00,00,00,00;

Floating-Point Numbers
numbers

)
.

Float

3.4e+038

32

1.4e-045

.
Example - float temp=34.4;
Double

1.8e+308

Example - double pi = 3.1416;

64

4.9e-324

You can download this EBook from www.besthinditutorials.com

Character

char

16

Example - char FavLang = c;

Boolean

decision
true

false.

Example - boolean isFivemorethanOne = true;

You can download this EBook from www.besthinditutorials.com

Chapter 5
Variables
This chapter covers

What are variables?


Types of variables
Creating variables

What are variables?

information

changeable

. Variable

constant

create

. information

error generate

information

change

Information

data type

You can download this EBook from www.besthinditutorials.com

Types of Variables in Java

Java

scope

Local Variables

.
.

Global Variables

variable

variable

global variables

scope

Creating Variables

Java

create

stages

Variable Declaration

Example - int PersonAge;


Variable Initialization

Example - PersonAge = 24;

Example - int PersonAge = 24.

initialize

declare

You can download this EBook from www.besthinditutorials.com

argument

(System.in)

nextInt(), nextLine(),nextFloat

Scanner

Example -

int PersonAge; // variable declaration


Scanner s = new Scanner(System.in); // Scanner class object
PersonAge = s.nextInt(); // reading from keyboard

You can download this EBook from www.besthinditutorials.com

Chapter 6
Strings
This chapter covers

What are strings?


Operations on strings

What are strings?


characters sequence
implement

compare

. String

strings objects

"I am fan of java". Java

operations
.

normal variables

object

string

Example

String variable

MyString

object

"this is a string "

You can download this EBook from www.besthinditutorials.com

string variable

methods

provide

modification

string object

generate

string object

Operations on Strings

String Length

string

length( )

string length

characters

length( ) method call

Example

String Concatenation

Example

strings

string concatenation

You can download this EBook from www.besthinditutorials.com


concat ( )

Example

charAt( ) method
method
array
Example

indexing

character extract

character extract

string

You can download this EBook from www.besthinditutorials.com

Equals( ) method
compare

false

equal

compare

argument

true

Example

indexOf( ) method

string

index

sub-string extract

sub-string

Example

substring( ) method

index end index argument


string

.
start index

start
end index

You can download this EBook from www.besthinditutorials.com

Example

You can download this EBook from www.besthinditutorials.com

Chapter 7
Arrays
This chapter covers

What are arrays?


Creating an array
Initializing an array
Displaying an array

What are arrays?

Array

syllabus

array

. Arrays programming

Array similar

need

variables
array

. Array

employees salary array

.
topic

word

programming

similar
employees

int, float, char

You can download this EBook from www.besthinditutorials.com


array

variables

size

index

BCA

access

array

array index

Arrays index

. Array

classmate

array

arrays

element

first

lecturer

array

.
.

zero

array

array

element (n -1)

array size 5

array

array index

element 22 4th

Creating an array
Java

array

type array_Name = new type[sizeofarray];


array

type array_Name new =

array

keyword

size of array -

array memory allocate


array

Example

int salary = new int[10];

array

You can download this EBook from www.besthinditutorials.com

Initializing an array
Arrays

initialize

array

particular index

Example

salary [4 ] = 10000;

array

array

Example

Scanner s = new Scanner(System.in)


for(int i=0; i<10; i++)
{
salary[i] = s.nextInt();
}

Displaying an array

array values

Example

System.out.println(salary[4]);

Example

for(int i=0; i<10; i++)


{
System.out.println(salary[i]);

You can download this EBook from www.besthinditutorials.com


}

Multidimensional arrays

dimensions

array example

one dimensional array .

two dimensional array


array

row column

Creating two dimensional arrays


Example

int TwoDiem[ ] [ ] = new int [3] [4];

Initializing two dimensional arrays


Example

Scanner s = new Scanner(System.in);


System.out.println("Enter the array elements");
for(int i=0; i<3; i++)
{
for(int j=0; j<4; j++)
{
TwoDiem[ ][ ] = s.nextInt();
}
}
Displaying two dimensional arrays
Example

for(int i =0; i<3; i++)


{
for(int j=0; j<4; j++)
{
System.out.println(TwoDiem[i][j]);
}
}

array

. two dimensional

You can download this EBook from www.besthinditutorials.com

Chapter 8
Operators
This chapter covers

What are operators?


Types of operators

What are operators?

operators

operations perform

operations

.
2

operands

unary binary .
operand

. Unary

apply

2 operands

6
1.
2.
3.
4.
5.
6.

Arithmetic Operators
Logical Operators
Bitwise Operators
Relational Operators
Assignment Operators
Conditional Operator

Types of Operators
Arithmetic Operators

Arithmetic
5

You can download this EBook from www.besthinditutorials.com


Addition
Addition

a=5, b=3;

Example

c = a+b;
Subtraction

Subtraction

subtract

a =10, b=9;

Example

c = a-b;
Multiplication

multiply

a =3, b=3;

Example

c = a*b;
Division

a=10, b =2;

Example

c = a/b;
Modulus

a= 10, b=3;

Example

c = a%b;

Logical Operators
operators

operations

true

Logical AND (&&)

true

false

true
.

true

3
false.

You can download this EBook from www.besthinditutorials.com


a = true, b = flase;

Example

c = a&&b ; // c

false

Logical OR (||)

true

true

a= true, b = false;

Example

c = a&&b; // c

true

Logical NOT (!)


unary

Bitwise Operators

by

Bitwise AND (&)


2

.
Bitwise OR (|)

Left Shift (<<)

Right Shift (>>)

right

. Right

You can download this EBook from www.besthinditutorials.com


Relational Operators
2

compute

IF

Equal to (==)

true return

equal

Not equal to (!=)

equal

true return

Greater than ( >)

true return

Less than (<)


true

Greater than equal to (>=)

true return

Less than equal to (<=)

Assignment Operators

Simple Assignment (=)

left

Plus and Assignment (+)

Minus and Assignment (-=)

subtract

You can download this EBook from www.besthinditutorials.com


Multiply and Assignment (*=)

multiply

Divide and Assignment (/=)

Conditional Operator (?:)


according

Example

a = 5, b = 3;
a

c = a>b ? a : b //

You can download this EBook from www.besthinditutorials.com

Chapter 9
Control Statements
This chapter covers

What are control statements?


Types of control statements

What are control statements?

even

execution

execute

. Iteration statements

statements

. Jump
.

execution (

according

odd.

email

even

email id

email

according

. Java

execute

You can download this EBook from www.besthinditutorials.com

Types of Control Statements


Selection Statements

execute
execute

execute

false
3

IF Statement

if ( condition )
{

//

execute

execute

condition

condition

execute

Example

int a = 5, b = 3;
if ( a > b)
{
System.out.println("A is bigger than B");
}

IF Else Statement

execute

if ( condition )
{
// when condition true
}
else

false
-

You can download this EBook from www.besthinditutorials.com


{
//when condition is false
}

false

else

. If

execute

execute

condition

Example

int a = 5, b = 3;
if (a > b)
{
System.out.println("A is bigger than B");
}
else
{
System.out.println("B is bigger than A");
}

Nested IF Statements
if

condition

Example

int a = 4;
if (a > 0)
{
if( a /2 == 0)
{
System.out.prinln("A is Even" );
}
}

execute

You can download this EBook from www.besthinditutorials.com

execute

Switch Statements

according execute

according
.

Example

int num = 3;
switch (num)
{
case 1 :
System.out.println("f number is 1");
break;
case 2 :
System.out.println("if number is 2");
break;

case 3 :
System.out.println("if number is 3");
break;

defalut:
System.out.println("if number does not match any
condition");
break;
}

You can download this EBook from www.besthinditutorials.com

case

example

execute

variable

execute

execute

execute

Iteration Statements

execute

iteration

execute

statement

execute
.

false

execution

execute

. Iteration

looping

For Loop

Example

for(initialization; condition; increment) ;


{
// statements
}

Initialization
initialization

Example - for(int i = 0; condition; increment);

You can download this EBook from www.besthinditutorials.com

Condition

execution

execute

?.

execute

initialized

Example - for(int i = 0; i <10; increment) ;

Increment

initialized

execute
?

Example for(int i =0; i<10; i++)


{
//
}

Example

class IterSta
{
public static void main(String args[])
{
for(int i =1; i<=10; i++)
{
System.out.println("Numbers from 1 to 10");
System.out.println(i);
}
}
}

You can download this EBook from www.besthinditutorials.com


While Loop
While

. While

initialization

initialization;

while (condition)
{
// statements

increment;
}

Example

class WhileStat
{
public static void main(String args[])
{
int i =1; // initialization not compulsory
while( i<=10) // condition
{
System.out.println(i);
i++; // increment
}

While

. While

You can download this EBook from www.besthinditutorials.com


Do-While Loop
while

execute

execute

execute

do-while

execute

execute

execution

.
Example

class DoWhileStat
{
public static void main(String args[])
{
int i=0; //initialization
do
{
System.out.println(i);
i++; // increment
}while(i<=10) // condition
}
}

Jump Statements

Break statement

cases

execution

execute

You can download this EBook from www.besthinditutorials.com


Example

break;

Continue statement

divide

100

iteration

iteration

iteration execute

100

Example

continue;

Return statement

Return

return

.
.

Example

return;

return

You can download this EBook from www.besthinditutorials.com

Chapter 10
Classes
This chapter covers

What are classes?


Advantages of classes
Creating a class

What are classes?

??

???

.
entity

attributes

entity

, ,

java

execution

Advantages of Classes

advantages

You can download this EBook from www.besthinditutorials.com

Security

.
Reduce Complexity

complexity

Separation

(methods)

(methods)

Purchases

Sales

Focus on Data

Creating a Class

Example

class Person
{
String Name="Vipin";
int age=24;

You can download this EBook from www.besthinditutorials.com


public static void display()
{
System.out.println("Name is "+Name+ "Age is"+age);
}

public static void main(String args[])


{
display();

age .

name

execution main

Main Method

execution

static

environment

execute

static

. Static

environment

You can download this EBook from www.besthinditutorials.com

Instance Variables and Methods

.
Static Variables and Methods

Command line argument

array

according

You can download this EBook from www.besthinditutorials.com

Chapter 11
Objects
This chapter covers

What are objects?


Creating objects

What are objects?

new

.
5

Steps to create an object

1.

2.

.
3.

equals (=)

4.

new

5. New
.
Example

class Person
{

You can download this EBook from www.besthinditutorials.com


String Name="Vipin";
int age=24;
public static void display()
{
System.out.println("Name is "+Name+ "Age is"+age);
}

class PersonObj
{
public static void main(String args[])
{
Person p1 = new Person();
p1.display();
}
}

example

execution

PersonObj

()

class

constructor

create
initialize

constructor

(= new Person (); )

? Constructor

(.)

constructor

You can download this EBook from www.besthinditutorials.com


Example

Person p1;
p1 = new Person();

objects
.

create

Example

Person p1:

initialize

reference variables

You can download this EBook from www.besthinditutorials.com

Chapter 12
Constructors
This chapter covers

What are constructors?


Rules for creating constructors
Types of constructors

What are constructors?

initialize

memory

constructor

initialize

. Constructor
Constructor

return

,
.

1. Constructor

2. Constructor

return

same

problem

Rules for Creating Constructors


Constructor

allocate

automatically

default constructor

allocate

alloacate

according

initialize
initialize

You can download this EBook from www.besthinditutorials.com

Types of Constructors
4

constructors

explain

Default Constructor

constructor

Constructor

constructor

Normal Constructor

constructor

Example

class Person
{
String Name;
int age;

Person() //Constructor
{
Name = "Vipin"
age = 24;
}

public void display()


{
System.out.println("Name is : "+Name+" Age is : "+age);
}
}

class NorCon
{
public static void main (String args[])

You can download this EBook from www.besthinditutorials.com


{
Person p1 = new Person(); // calling constructor and initializing object
p1.display();
}
}

example

automatically

constructor

Parameterized Constructor
constructors

parameters

variables

assign

Example

class Person
{
String Name;
int age;

Person(String n , int a ) // Parameterized Constructor


{
Name = n ;
age = a ;
}

public void display()


{
System.out.println("Name is : "+Name+" Age is : "+age);
}
}

You can download this EBook from www.besthinditutorials.com

class NorCon
{
public static void main (String args[])
{
Person p1 = new Person("Vipin",24 ); // calling constructor and
initializing object
p1.display();
}
}

example

initialize

create

Copy Constructor

constructor

same

constructor

argument

Example

class Person
{
String Name;
int age;

Person(String n , int a ) // Parameterized Constructor for p1 object


{
Name = n ;
age = a ;
}

You can download this EBook from www.besthinditutorials.com

Person(Person p ) // Copy Constructor for p2 object


{
Name = p.Name;
age = p.age;
}

public void display()


{
System.out.println("Name is : "+Name+" Age is : "+age);
}
}

class CopyCon
{
public static void main (String args[])
{
Person p1 = new Person("Vipin",age);
Person p2 = new Person(p1); // calling constructor copying object
p1.display();
p2.display();
}
}

same

example
.

You can download this EBook from www.besthinditutorials.com

Chapter 13
Methods
This chapter covers

What are methods?


Structure of method definition
Structure of method call

What are methods?

perform

??.

text

println() method.

separate
-

You can download this EBook from www.besthinditutorials.com

Structure of method definition


access_specifier return_type method_name (argument list)
{
// statements to be executed
}

Access specifier

public

private

static.

Return type
execution
int

return

string.

Method name
method

Argument list

variables

return

Structure of method call


If in the same class
method_Name ();

If in another class
object_Name.method_Name();

Example

class PersonsInfo
{

addition

return

You can download this EBook from www.besthinditutorials.com


Public static void Separator()// our method which separates information of persons
{
System.out.println("-----------------------------------------------------------------------------");
}

public static void main(String args[])


{
System.out.println("Name : Depika Padukaun Age : 23");
Separator();
System.out.println("Name : Vipin

Age:24");

Separator();
System.out.println("Name : Radha Age : 23");
Separator();
System.out.println("Name : Krishn

Age:24");

}
}

Output

Name : Sita Age : 23


------------------------------------------------------------------------------Name : Ram Age : 24
-------------------------------------------------------------------------------Name : Radha Age : 23
---------------------------------------------------------------------------------Name : Krishn Age : 24

print

method
persons

You can download this EBook from www.besthinditutorials.com


Void
Void

return type

void

You can download this EBook from www.besthinditutorials.com

Chapter 14
Inheritance
This chapter covers

What is inheritance
Types of inheritance
Inheriting a class
Example

What is inheritance?

class

class

class

code

re-usability
class

class

extend

class

extends
extend

class

Final

.
.

class

implement

super

class extend
subclass

reuse

. Inheritance

multiple inheritance

allow

class
Final

inheritance

class

class extend
Inheritance

class features

class

You can download this EBook from www.besthinditutorials.com

subclass

Types of Inheritance

Single inheritance

extend

Class A
|
extends
|
Class B

Multilevel inheritance

extend

extend

Class A
|
extends
|
Class B
|
extends
|
Class C

Hierarchical inheritance

Class A
/
extends
/
Class B

\
extends
\
Class C

extend

You can download this EBook from www.besthinditutorials.com

Inheriting a class

inherit

extend

class otherclass // class you want to inherit


{

}
class yrclass extends otherclass
{
// now you can access other class public methods and variables
}

Example from oracle.com.

public class Bicycle


{
// the Bicycle class has three fields
public int cadence;
public int gear;
public int speed;

// the Bicycle class has one constructor


public Bicycle(int startCadence, int startSpeed, int startGear)
{
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}

// the Bicycle class has four methods

extends

You can download this EBook from www.besthinditutorials.com


public void setCadence(int newValue)
{
cadence = newValue;
}

public void setGear(int newValue)


{
gear = newValue;
}

public void applyBrake(int decrement)


{
speed -= decrement;
}

public void speedUp(int increment)


{
speed += increment;
}

}
public class MountainBike extends Bicycle
{
// the MountainBike subclass adds one field
public int seatHeight;

// the MountainBike subclass has one constructor


public MountainBike(int startHeight,

You can download this EBook from www.besthinditutorials.com


int startCadence,
int startSpeed,
int startGear)
{
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}

// the MountainBike subclass adds one method


public void setHeight(int newValue)
{
seatHeight = newValue;
}
}

You can download this EBook from www.besthinditutorials.com

Chapter 15
Exception Handling
This chapter covers

What are excetions


Keywords (try, throw, catch)
Steps to handle exceptions
Some common exceptions
Example

What are exceptions?


Exceptions

. Exception

error

compile

out of memory

execute

exception

execute

memory

execution

situations

error

execute

Keywords

Java

structure

keywords

easy .

You can download this EBook from www.besthinditutorials.com


Try

try

Catch

try

generate

catch

Throw

java

exceptions

exceptions

define

throw

java

Throws

method

throw

exceptions

generate

throws

exceptions

Finally

Exceptions

execute

execute

terminate

Steps to Handle Exceptions


1.

2. Try

try

catch

exception generate

exception

generate

Example

catch(ArithmeticException e)
3. Catch

4. Exception

exception

situation

execute

Some Common Exceptions

exceptions

exceptions

message

. Exceptions

java

exceptions

You can download this EBook from www.besthinditutorials.com

skill improve

exceptions

java

Arithmetic Exception

arithmetic error

divide

ArithmeticException generate

.
Class Cast Exception

sub

ClassCastException generate

Array Store Exception


array

ArrayStoreException generate

string

Array-Index-Out-Of-Bounds-Exception
array

10

11th

11

ArrayIndexOutOfBoundsException generate

Illegal Argument Exception

IllegalArgumentException generate

int

string

Null Pointer Exception

Java

reference null

assign

NullPointerException generate

Number Format Exception

string

number

NumberFormatException generate

null
.

You can download this EBook from www.besthinditutorials.com


Example

You can download this EBook from www.besthinditutorials.com

Chapter 16
Interfaces & Packages
This chapter covers

What are interfaces?


Example
What are packages?
Creating packages
Using packages
Some common packages

What are interfaces?


allow

. Interfaces

interfaces

Interfaces

object

create

. Method

extend

methods

interface

Interfaces
interfaces

variables static final

Interfaces
fruit


Is-a relation

interfaces

establish

Is-a
mango

Is-a

mango

fruit

Mango is a fruit.

interfaces

You can download this EBook from www.besthinditutorials.com


Interfaces

public
public

interface keyword

public

Example

public interface Fruit


{
void DisName();
}

class Mango implements Fruit


{
void DisName()
{
System.out.println("Mango Is-a Fruit");
}
}

example

Fruit

Fruit interface DisName()

Mango

example

Mango

What are packages?

???

problem

??.

error show

same
.

execute

possibility

.
problem

java

You can download this EBook from www.besthinditutorials.com

error generate

packages

same

. Packages

programmer productivity

category classes

classes interfaces categories

Java packages

error

error

same

Package

package

maintain

packages

production

sales package

related classes

production package

Java packages

classes interfaces access protection provide

class

packages classes access

access modifier

packages level protection provide

same

classes

package-private modifier apply


.

java packages

related classes

package

packages

java packages

amazing

Creating packages

Package

:
Example package newPackage;

Using packages

You can download this EBook from www.besthinditutorials.com


Example import newPackage;

package

directly

package member access

access

import packageName.className;
Example import newPackage.myClass;

Some common packages


main package

Java

Java API Package

package

packages

IO package AWT package .

packages

java

tasks

common

packages

java.language
package

java

java.util

package

vectors hash tables

java.io

package

java.AWT

package

java.net

package

.
java.applet
package

You can download this EBook from www.besthinditutorials.com

Chapter 17
Multithreading
This chapter covers

What is multithreading?
Thread class
Runnable interface
Synchronization

What is multithreading?

JAVA

JAVA

Multitasking

Multi-threading

. multi-threading

Thread

run()

Runnable

thread

run()

Thread class

Thread

Thread
run()

Thread class
Runnable Interface

JAVA

You can download this EBook from www.besthinditutorials.com


Thread t = new Thread;

thread

thread

start()

start()

t.start();

start()

public void run()


{
//

1. getName
2. getPriority
3. isAlive
4. join
5. run
6. sleep
7. start

thread

. run()

execute

You can download this EBook from www.besthinditutorials.com


Example

Thread

.
example

Thread
start() run()

Runnable interface

Runnable
Thread

start()

Runnable

argument

You can download this EBook from www.besthinditutorials.com


Example

This

.
run()

This

start()

Synchronization
2

conflict

thread

resource

technique

.
.

technique

Synchronization concept

access

conflict

synchronization

thread

monitor
synchronized

You can download this EBook from www.besthinditutorials.com

lock

threads

waiting

suspend

Java

Synchronization 2

:-

1. Synchronized method
2. Synchronized statement

Synchronized method

synchronized

execute

Synchronized

Example:

class bank
{
synchronized void withdraw(int amount)
{

}
}

Synchronized statement

class

synchronized

synchronized

synchronized

Example:
Synchronized (s1)
{
//
}

synchronize

You can download this EBook from www.besthinditutorials.com

Chapter 18
File I/O
This chapter covers

What are streams?


Byte streams
Character streams

input
console

.
enter

software

read

output
.

output

input

output

solution
file

??

read

input console
console

human

output

situation

provide

file

input

classes

output

console (command prompt)

text file

file

. Java

output

store

file

You can download this EBook from www.besthinditutorials.com

What are Streams?


Data sequence stream

streams 2

Input Stream
Input stream

input

Output Stream
Output stream

Input stream Output stream

output

byte stream character stream.

Byte Streams
Byte stream

bytes

input output

2 classes

InputStream OutputStream.
InputStream class
abstract

class

byte input stream class . FileInputStream class


methods

FileInputstream class

extend

Methods of InputStream Class

OutputStream Class
abstract

byte output stream class . FileOutputStream class


methods

FileOutputStream

extend

You can download this EBook from www.besthinditutorials.com


Methods of Output Stream class

FileInputStream Class
FileInputStream byte stream input
stream

object

call bytes

read( )

byte input
.

Example

FileOutputStream Class
FileOutputStream byte stream output
output stream
write
Example

.
write ( )


bytes

byte

You can download this EBook from www.besthinditutorials.com

example

byte stream

Character Stream
Character stream

bytes

input output

2 classes

character

. Character stream

Reader Writer .

Reader Class
Reader abstract
extend

character input stream

. FileReader Class

Methods of Reader Class

Writer Class
abstract

character output stream

FileWriter

FileWriter Class

extend
.

You can download this EBook from www.besthinditutorials.com


Methods of Writer class

FileReader Class
FileReader character stream input classes .
character input stream

create

read ( )

data read

object

Example

FileWriter Class
FileWriter character stream output class .
output stream create
Example

object

write ( )

character

data write

You can download this EBook from www.besthinditutorials.com

You can download this EBook from www.besthinditutorials.com

Chapter 19
Serialization
This chapter covers

What is serialization?
Serializable interface

What is serialization?

process serialization
store

members values file

Object

object serialize

EmployeeName

process De-serialization

serialize

object restore
byte stream

state save

object current state

??.

.
save

stages

??.
store

employee

store

object De serialize

value memory

file

load

Serializable Interface

object serialize

interface implement
objects

serialize

Serializable

serializable interface implement

You can download this EBook from www.besthinditutorials.com


Example

ObjectOutputStream Class

object file

store

FileOutputStream

ObjectOutputStream
serialize
Example

ObjectOutputStream
object
writeObject ( )

argument

object create

You can download this EBook from www.besthinditutorials.com


ObjectInputStream Class

object

ObjectInputStream

Example

memory
.

ObjectInputStream

FileInputStream Class object

object

readObject ( )

You can download this EBook from www.besthinditutorials.com


A Complete Example of Serialization

You can download this EBook from www.besthinditutorials.com

Chapter 20
Collections Framework
This chapter covers

What are collections?


Other interfaces

What are collections?


java collections

data structures

objects

. Java

structures(set, list, queue, array )

objects

) perform

Collections framework

classes

collection
.

collection

interfaces implement

. Collections framework

data

objects organize

object

Collections Framework

normal

extend

operations (Searching, Sorting, Insertion, Deletion

classes provide

structure

data structures collection

organize

interfaces

data structure

interfaces classes generic


.

interfaces classes

Collection Interface

collection define

Collection interface

collection interface implement

methods .

interface methods

collections

interface implement
-

methods
Collection

You can download this EBook from www.besthinditutorials.com

List Interface
ArrayList LinkedList

objects

list interface implement

objects

store

. list collection

duplicate

ArrayList Class

LinkedList Class

Set Interface
HashSet TreeSet

set interface implement

. Set collection

. Set collection

duplicate
set collection

objects

You can download this EBook from www.besthinditutorials.com


HashSet Class

collection objects

hash table

TreeSet Class

collection

objects tree form

Queue Interface
queue interface implement

PriorityQueue

First-In-First-Out form

queue collection

PriorityQueue Class

Map Interface
HashMap TreeMap class Map interface
value pair

store

. Map collection

elements unique

. Map collection

store access
.

data key
key

You can download this EBook from www.besthinditutorials.com


HashMap Class

TreeMap Class

Iterator interface
interface iterator ( ) method provide
return

collection first element index

iterator interface

iterator interface hasNext ( )


next ( )

call

collection elements

create

through

You can download this EBook from www.besthinditutorials.com

Chapter 21
Lambda Expressions
This chapter covers

What is functional interface?


What is lambda expression?

What is functional interface?


Lambda expressions

interface
interfaces

abstract

method

default abstract .

functional

. functional
Runnable

.
hello()

Welcome.

message

by

,Example:-

Example

interface Welcome
{
String hello(String name);
}

What is lambda expression?


Lambda expression

abstract

functional

. Lambda expression

conditions

execute

abstract

available

string
define

variable initialization,

You can download this EBook from www.besthinditutorials.com


return statement method argument

lambda

available
expression

return type

body

lambda

(->)

(right)

available

(left)

conditions

body

lambda

class

lambda

Lambda expressions

conditions

lambda

functional interface object

Example

Welcome wel;
String name = "Vipin";
wel = (name)-> {
System.out.println("Hello"+name);
};

lambda expression

expression

lambda

string

functional

hello()

apply

lambda

You can download this EBook from www.besthinditutorials.com

Chapter 22
Generics
This chapter covers

What are generics?


Example

What are generics?



.

addition method

data type

float

java

generics

execute

classes, interfaces constructors

method

classes, interfaces constructors


generic .

possible ???

data type

execute

readability

methods

Java

Generics
.

data type

integers

double

generic

. Java

You can download this EBook from www.besthinditutorials.com


Example

class

class

(< >) brackets

declare
declare

generic

generic
variable declaration

variable type generic

constructor

object create

generic argument

convert

generic class

argument

different
object

You can download this EBook from www.besthinditutorials.com

Chapter 23
Applets
This chapter covers

What are applets?


Applets life cycle methods
Example

What are applets?


Applet java

save

HTML

visitor

load

Java.applet.Applet

important

applets client side applications

HTML

load

.class extension

save

server

init ()
variables initialize

initialization

start()
init()

automatically

You can download this EBook from www.besthinditutorials.com


stop()

destroy()

paint()

start()

java.applet.Applet java.awt

Example

class picture extends Applet


{
public void init()
{
void setBackground(color.red);
}

public void paint(Graphics g)


{
g.drawString("Applet are for GUI",30,30);
}
}

drawString()
coordinates

show

HTML page

argument

open

java

browser

window background color

setBackground()

example
browser

open
.

You can download this EBook from www.besthinditutorials.com


<html>
<head>
<title>applet example</applet>
</head>
<applet code="picture.class" width="300" height="300">
</applet>
</html>

java

HTML

HTML

applet

.
html

java

param.

through

2 attribute

name value.

Example

<html>
<head>
<title>applet example</applet>
</head>
<applet code="picture.class" width="300" height="300">
<param name=userName value=Vipin Sharma>
</applet>
</html>

HTML

userName

class userName extends Applet


{
String username;

. Java

You can download this EBook from www.besthinditutorials.com

public void start()


{
username = getParameter("userName");
}
public void paint(Graphics g)
{
g.drawString(username,30,30);
}
}

You can download this EBook from www.besthinditutorials.com

Chapter 24
Abstract Windows Toolkit
This chapter covers

What is AWT?
Container classes
Component classes
Frame class

What is AWT?
Java

GUI (Graphical User Interface)

extended version

swing

create

calculator

AWT Swing. Swing AWT

. AWT

buttons , menus

. Graphical user interface


AWT

notepad

AWT GUI

components(button, textbox etc..)

packages classes

java

applets

. AWT

java.awt.*

. AWT

.
AWT

AWT

Container - Frame,Window,Panel,Dialog
Component - Button,Label,TextFeild,RadioButton,ScrollBar,checkbox

Container classes
Container

components

base

. Container

main

components

You can download this EBook from www.besthinditutorials.com

Window, Panel, Dialogue.

available

Frame,

Frame

add(component obj)

add()

add()

Example add(b1);

setSize(height,width)

setSize()

width

height

Example setSize(100,100);
setLayout(layoutManager Obj)

setLayout()

LayoutManager

LayoutManagers

BorderLayout GridLayout.

Layout

null

Example setLayout(null);

setVisible(true or false)

setVisible()

true

visible
.

show

open

method

false

Example setVisible(true);

Component classes
Components
TextArea

interact

controls
.

standalone

components

container

Button, TextField, ScrollBar,

.
components

methods

class represent

You can download this EBook from www.besthinditutorials.com


setBackground(color)

Example setBackground(red);

resize(height,width)

Example resize(30,30);

move(int,int)

Dimension

Example move(50,120);

setBounds(int,int,int,int)
starting

component position

Example setBounds(30,150,200,160);

Frame Class
Frame

AWT example
.

By Extending Frame Class


Example

class guiLabel extends Frame


{
guiLabel()
{
Label lb = new Label("This is AwT BeSt ExAmPlE");

setSize(200,200);

Frame

add(lb);

. Frame

You can download this EBook from www.besthinditutorials.com


setVisible(true);
}

public static void main(String args[])


{
guiLabel gl = new guiLabel();
}

By Creating Frame Class Object


Example

class guiButton
{
public static void main()
{
Frame f = new Frame();
Button b1 = new Button("Click it!!");

f.add(b1);
f.setTitle("Button Example");
f.setSize(200,200);
f.setVisible(true);
}

You can download this EBook from www.besthinditutorials.com

Chapter 25
Servlets
This chapter covers

What are servlets


Architecture of servlets
Life cycle of servlets
Example

What are servlets?


Servlets

. Java Servlets web applications

java

generate

request server response

submit

input

dynamically

CGI (Common Gateway Interface )


CGI

advantages

. Servlet

java servlets

advantages

Java servlets performence CGI

Servlets execute

Servlets

Servlets secure

independent
.

better .

java

You can download this EBook from www.besthinditutorials.com

Packages used by servlets


Servlets

2 packages import

javax.servlet

interfaces

java

servlets

GenericServlet

javax.servlet.http

java classes interfaces

http

Architecture of Servlets

servlets

development support

servlet

HttpServlet

You can download this EBook from www.besthinditutorials.com

Tasks of servlets

http server request

Servlets client (browser)

servlets request

processing

requested

(browser)

Life cycle of servlets


applets init ( )

Init()

Service()

servlet life cycle

important

response

Destroy() -

client request

connection

database connectivity server connection . Destroy


garbage collect

servlet

servlet

doGet() and doPost() methods


http protocol

doGet( ) method

HttpServlet

extend

doGet( )
url request

method(GET , POST ) mention

submit
doGet( )

doPost( )
html

method POST mention

request doPost( ) method

Steps to create a servlet program


servlet

import javax.servlet.*;

javax.servlet.http.*;

http servlets
GenericServlet

HttpServlet

You can download this EBook from www.besthinditutorials.com


http servlet create

request

doGet ( ) doPost ( )
generic

response

response.setContentType("text/html");

getWriter( )
PrintWriter stream
close( )

generate

html
close

Example

public class HelloWorld extends HttpServlet


{
private String message;
public void init() throws ServletException
{
message = "Hello World";
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");

PrintWriter out = response.getWriter();


out.println("<h1>" + message + "</h1>");
}
public void destroy()

}
}

life cycle methods

html

Vous aimerez peut-être aussi