Download JAVA - Sunrise Academy, Ajmer

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Stream processing wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Abstraction (computer science) wikipedia , lookup

Class (computer programming) wikipedia , lookup

Covariance and contravariance (computer science) wikipedia , lookup

Name mangling wikipedia , lookup

Java syntax wikipedia , lookup

C syntax wikipedia , lookup

Object-oriented programming wikipedia , lookup

Scala (programming language) wikipedia , lookup

C++ wikipedia , lookup

Go (programming language) wikipedia , lookup

C Sharp syntax wikipedia , lookup

Java (programming language) wikipedia , lookup

Java performance wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
INDEX
INTRODUCTION TO JAVA ................................................................................................................... 3
CHARACTERISTICS ............................................................................................................................. 4
DATA TYPE ........................................................................................................................................ 6
VARIABLES......................................................................................................................................... 7
KEYWORDS ........................................................................................................................................ 8
COMMENTS....................................................................................................................................... 9
OPERATORS ....................................................................................................................................... 9
CONTROL STATEMENTS ................................................................................................................... 14
SIMPLE JAVA PROGRAM .................................................................................................................. 20
ARRAYS ........................................................................................................................................... 22
GETTING INPUT FROM USER............................................................................................................ 24
INTRODUCTION TO CLASSES ............................................................................................................ 25
DECLARING OBJECT ......................................................................................................................... 26
CALLING A METHOD USING OBJECT ................................................................................................. 27
CONSTRUCTORS .............................................................................................................................. 28
CONSTRUCTOR OVERLOADING ........................................................................................................ 29
INHERITANCE .................................................................................................................................. 30
USING CONSTRUCTOR IN INHERITANCE........................................................................................... 33
POLYMORPHISM ............................................................................................................................. 36
METHOD OVERLOADING ................................................................................................................. 36
METHOD OVERRIDING..................................................................................................................... 38
INTERFACES..................................................................................................................................... 40
IMPLEMENTING INTERFACE............................................................................................................. 41
INHERITING MULTIPLE INTERFACE: - ................................................................................................ 43
PACKAGES ....................................................................................................................................... 45
DEFINING A PACKAGE...................................................................................................................... 45
IMPORTING PACKAGES .................................................................................................................... 46
ACCESS MODIFIERS.......................................................................................................................... 47
STACK CLASS ................................................................................................................................... 50
EXCEPTION HANDLING .................................................................................................................... 51
USING TRY AND CATCH.................................................................................................................... 53
USING THROW ................................................................................................................................ 55
USING THROWS............................................................................................................................... 56
FINALLY: .......................................................................................................................................... 57
EXCEPTION CLASSES OF JAVA .......................................................................................................... 58
MULTI THREADED PROGRAMMING ................................................................................................. 59
CREATING A THREAD ....................................................................................................................... 62
IMPLEMENTING RUNNABLE INTERFACE........................................................................................... 65
SYNCHRONIZATION ......................................................................................................................... 66
INTER THREAD COMMUNICATION ................................................................................................... 67
INPUT / OUTPUT APPLET ................................................................................................................. 69
STREAM........................................................................................................................................... 69
TYPES OF STREAM CLASSES ............................................................................................................. 70
PREDEFINED STREAM ...................................................................................................................... 76
APPLET ............................................................................................................................................ 78
1
CREATION OF AN APPLET................................................................................................................. 78
JAVA LIBRARY (STRING HANDLING).................................................................................................. 80
STRING CONSTRUCTORS .................................................................................................................. 81
STRING LENGTH............................................................................................................................... 83
STRING LITERALS ............................................................................................................................. 83
CHARACTER EXTRACTION ................................................................................................................ 85
STRING COMPARISON ..................................................................................................................... 87
SEARCHING STRING ......................................................................................................................... 88
MODIFYING STRING......................................................................................................................... 91
CHANGING THE CASE OF CHARACTERS ............................................................................................ 92
STRING BUFFER ............................................................................................................................... 93
METHODS OF STRING BUFFER CLASS ............................................................................................... 94
VECTOR ........................................................................................................................................... 97
VECTOR ASSIGNMENT ..................................................................................................................... 98
UTILITY CLASSES ............................................................................................................................ 101
STRING CLASS ................................................................................................................................ 102
WRAPPER CLASS ............................................................................................................................ 102
MATH CLASS.................................................................................................................................. 106
APPLET CLASS ................................................................................................................................ 111
METHOD OF APPLET CLASS............................................................................................................ 112
INTRODUCTION TO AWT ............................................................................................................... 115
WORKING WITH FRAME WINDOW ................................................................................................ 123
WORKING WITH GRAPHICS............................................................................................................ 126
WORKING WITH COLOR................................................................................................................. 128
JDBC .............................................................................................................................................. 129
JDBC COMPONENTS ...................................................................................................................... 129
JDBC CLASSES ................................................................................................................................ 131
RMI (REMOTE METHOD INVOCATION) .......................................................................................... 131
CGI ................................................................................................................................................ 134
CORBA........................................................................................................................................... 135
JAVA BEANS................................................................................................................................... 138
JAVA BEAN CONVENTIONS ................................................................................................ 138
UNSOLVED QUESTIONS ................................................................................................................. 141
SOLVED QUESTION ........................................................................................................................ 142
SOLVED PROGRAMMING QUESTIONS............................................................................................ 151
2
INTRODUCTION TO JAVA
Java was developed James Gosling, Patrick Naughton, Chris Worth, Ed
Frank and Mike Sheridan at SUN Micro system in 1991. Initially, this language
was called by. “OAK” but it was renamed by “Java” in 1995.
Sun released the first version of java in 1996, named “java1.0” but it had
no features for print then the next version was “java1.1”.
In 1998 Java released with “java1.2, which replaced the early simple GUI
and graphics toolkits with new ones. Three days after it’s released in December
1998, Sun’s marketing department change its name to: “Java2 standard edition software Development kit version 1,2”: Version 1.3 and 1.4 of the standard edition are incremental
Improvements ours the initial java2 release, with an ever growing standard
Library, increased performance and few bugs.
Version 5.0 is the first release version since version 1.1 that updates the
java language in significant ways. This version was originally numbered 1.5.
Growth of the Java Standard Edition: Version
Number of Classes & Interface
1.0
211
1.1
477
1.2
1524
1.3
1840
1.4
2723
5.0
3270
Java is also a OOPs Language. It derives most of the characteristics from
C and C++. The java designers knew that by using the syntax of ‘c’ language
and the object oriented features of ‘C++’ would make their language appealing
to the experienced C/C++ programmers. Java was not designed to replace C++
it was designed to solve certain set of problems.
3
Java is only a language, whose programs are run on any operating system.
Java is developed to creating programs for electronic consumer devise likes T.V., VCR, microwave oven, remote control etc.
Java is a high language, which is featured with operators, key-words,
programming structures and compiling unit.
CHARACTERISTICS
The main features of Java are as follows 1. Simple: - Java is a very simple programming language like C and C++ but
Java uses very less features of C and C++. In Java there is no need for header
file, pointer arithmetic, structures, unions, operator overloading etc.
2. Secure: - In C and C++ we can developed executable code but in java
programming java compiler change the instructions into byte codes, which can
run any computer machine byte code protect Java from virus but in C and C++
the executable codes directly effects the memory and chances of viruses very
high.
Java is designed to make certain kinds of attacks impossible i.e.
corrupting memory outside. Its own space, reading or writing file without
permission, etc.
3. Portable: - The most significant contribution of Java over other language is
its portability. Java programs can be easily moved from one computer system to
another, any- where and anytime. Changes and upgrades in operating system,
processors and system resources will not force any change in Java programs.
This is the reason why Java has become a popular language.
4. Object Oriented: - Java is also an OOP language like C++. Object oriented
programming is a way to write software that is reusable, extensible and
maintainable. Java comes with extensive set of classes arrange in packages that
we can use in our program by inheritance. The object model in Java is simple
and easy to extend.
5. Robust: - Java is a robust language which provides many safe guards to
ensure reliable code. In C and C++, if users occupy memory for any program
then after exaction of the program user should release this memory otherwise
memory will not be free and which makes memory wastage, but in Java if user
occupy the memory and if user do not release this memory after the execution
of the program then this memory is automatically free. This feature is known as
robust.
There are two main reasons to better understand how Java is
robust. First is memory management mistakes and the second is exceptional
conditions.
4
Memory management can be a difficult task in programming
environment. Java virtually eliminates this memory management problem by
managing memory allocation and deal location. In Java deal location is
completely automatic, because Java provides garbage collection for unused
Object.
In Java exceptional handling captures series of errors. Exceptional
condition after arise in situations like -division by zero, file not found etc.
4. Multithreaded: - Java support multithreaded programming which allows us
to write those programs that do many things simultaneously. A multithreaded
application can have several threads. There thread may communicate and cooperate to each other and will appear to be a single program to the user.
5. Architecture Neutral: - If a programmer write a program today then
programmer’s facing the main problem is that there is no guarantee exists, the
program will run tomorrow - even on the same machine. To alter these situation
Java designers made several hard decisions in the Java language and the Java
virtual machine. In which, ”architecture Neletral” feature allows user that if
user write a program today then it will run tomorrow even on the same machine.
Because architecture neutral object are easy to interpret on any machine and
easily translated into machine code. So the goal of Java is:
“Write Once, Run Anywhere, Any Time, Forever”
6. Interpreted: - The Java interpreter can execute Java byte codes directly on
any machine to which the interpreter has been ported.
It is true that Java was designed for interpretation, the Java byte code was
carefully designed so that it would be easy to translate byte code directly into
machine code for vary high performance by using a Just-in-time compiler.
“In Java, Java Virtual machines (JVM) have the option of translating the
executed byte code into machine code, This process is called as Just-In-Time
compilation”.
In the interpretation process of byte code to machine code, JIT compilers
given an improvement in speed and also monitor which code is executed
frequently.
7. High Performance: - In Java JIT process translates byte code into machine
code at a very high performance. The performance of interpreted byte code is
usually more.
8. Distributed: - Java is designed for the distributed environment of the Internet
because Java handles TCP/IP protocols like HTTP and FTP.
Java is designed as a distributed language for creating application on
network. Java application can open and access remote object on internet as
5
easily as they can do in a local system this enables multiple programmers at
multiple remote locations to collaborate and work together on a single project.
9. Dynamic: - Java is a more dynamic language than C and C++. This is an
important feature in those situations in which code needs to be added to a
running program. For example libraries can add new methods and instance
variables without any effect on their client.
DATA TYPE
In Java data type are divided in two parts:
1. Primitive
2. Non-Primitive
1. Primitive data type: - These data types provide a way to store the simple
values for that the program is dealing with. Primitive data types are predefined
types of data, which are supported by the programming language. For example,
integer, character, and string are all primitive data types. Programmers can use
these data types when creating variables in their programs. For example, a
programmer may create a variable called "name" and define it as a string data
type. The variable will then store data as a string of characters. Java defines
eight simple primitive data type.
(1) Byte
(2) Short
(3) Int
(4) Float
(5)Long
(6) Char
(7) Double
(8) Boolean
(1) Byte: - Byte is a smallest Integer type. Byte required 8-bit memory and its
range from -128 to 127. Byte variables are declared by “byte” Keyword.
(2) Short: - Short required 16 -bits memory and its range from -32768 to 32767.
It is least used data type. Short variables are declared by short keyword.
(3) Int: - The int type is the most efficient type and it should be used when we
want to create a number for counting, indexing arrays or for calculate integer
math. Int required 32-bit memory and its range from -2,147,483,648 to
2,147,483,648. Int variables are declared by keyword int.
(4) Long: - When an int type is not capable to hold large enough value then
long data type is used. The range of long data type is used. The range of long
type is quite large. Long type is useful when big, whole numbers are needed
6
long required 64-bits memory and its range from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,808.
(5) Float: - The type float specifies a single precision value. It user 32 bits
memory float data type are useful when variables need a fractional component.
For example, Float can be useful when representing dollar and cents. Float
variables are declared by keyword float.
(6) Double: - Double specifies a doubles precision value. Double precision is
actually faster than single precision on some modern processors that have been
used for high speed mathematical calculations. In Java all math function such as
sin(), cos(), sqrt() etc return double values. Double user 64 bits memory and its
range from 1.7E-308 to 1.7E+308 Double variables are declared by keyword
double.
(7) Char: -Char in Java is not the same as char in C and C++. In C or C++ char
is an integer type but in Java character are represented by Unicode. Unicode
define a fully international character set to represent characters. In Java char
required 16 bit memory or it takes 2 byte. The range of char is 0 to 65536.
The standard set of character known as ASCII and its range is 0 to 127,
and the extended 8-bit character set known as ISO-Latin-1 and its range is 0 to
255.
(8) Boolean: - Java has a simple data type for logical values that is known as
Boolean. Boolean data type can have only one of two possible value i.e true or
false. Boolean data type required 1 byte memory.
2. Non Primitive data types: Non-primitive data types are not defined by the
programming language, but are instead created by the programmer. They are
sometimes called "reference variables," or "object references," since they
reference a memory location, which stores the data. In the Java programming
language, non-primitive data types are simply called "objects" because they are
created, rather than predefined.
VARIABLES
Variables are the identifiers variables are used to display the location of
stored value in the memory. Variables are the basic unit of storage. In Java all
the variables have a scope which defines visibility and lifetime.
Declaration of Variable: - In each program all variable must be declared
before they can be used. The main work of variable declaration is to define that
which variable holds which data type.
Syntax: - Data type <variable1>, <variable2>, …..<variable n>
Example: int a, b=10;
Float x, y;
7
Naming Convention of Variable: 1) Variable name should not start with blank space.
2) We cannot use any keyword as a variable name.
3) Variable name should not be a number, it always start with letter.
4) Variable name should be of any length.
Scope of Variable: - In Java programs the area from which variable can be
accessed are known as scope of variable or Java allows variable to be declared
with in any block, this block defines a scope of variable.
In Java a scope determines what object are visible to other part of
our program. In Java scope of variable are divide in three parts
1) Local variable
2) Instance Variable
3) Class Variables
1) Local Variable: - The variable that are declared and used inside methods are
known as local variable. User cannot use this local variable in another method,
the scope of local variable is only for that method in which they are declared.
2) Instance variable: - Instance variable are used to initialize an object or
instance variable are created when the object are initialized.
3) Class Variable: - Class variable are same as global variable. We can use
class variable anywhere within a class. For each class variable, memory
occupied a definite space.
Scope in Java can be nested in which the outer scope encloses the
inner scope. This means that the object that are declared in the outer scope, will
be visible to code within the scope. But the object that are declared within the
inner scope, will not be visible to outer scope.
KEYWORDS
Keyword is also known as reserve words. In Java each keyword have a special
meaning for the Java compiler. Keyword cannot be used as a class, method or
variable name. Java have more keywords in compare to C and C++. There are
50 reserved keywords that are currently defined in Java some keywords are:
Abstract
do
implements
protected
Boolean
double
import
return
Break
else
interface
static
Case
enum
new
super
Catch
extends
package
throw
Char
final
private
try
Class
finally
public
void
8