Академический Документы
Профессиональный Документы
Культура Документы
?
?
???
C is a structured programming language invented in the year 1980¶s. It is a powerful tool
that enables programmers to write complex programs.
OOPs is an approach to program organization and development. Not all languages are
suitable to implement the OOP concepts easily. Languages that support OOPs features include
small talk, objective C, C++, Ada and object Pascal.
C++ is an extension of C language. It is the most popular language with object-oriented
extension. The latest one added to this list is Java, a pure object-oriented language.
?
?
??
OOP¶s allows us to decompose a problem into a
? ?
?
?
and
then build data and functions around these entities. The combination of data and methods make
up an object.
Object = Data + methods
?
? ? ? ? ? ?
? ? ? ? ?
??
?
?
?
????????
?
??
??
?
???
??
!?
"#$ ?%?
$&'?
?()*+' ?$'$ ,-!?
?
·? -mphasis is on data rather than procedure.
·? Programs are divided into objects.
·? Data structures are designed such that they characterize the objects.
·? |ethods that operate on the data of an object are tied together in the data structure.
·? Data is hidden and cannot be accessed by external functions.
·? Objects may communicate with each other through methods.
·? New data and methods can be easily added whenever necessary.
·? ×ollows bottom-up approach in program design.
?
"#$ ?%?($ *?**?
?()*+' ?','$-- ,.??
©?
?Objects are the basic runtime entities. They may represent a person, a place, a
bank account or any item that the program may handle.
©? *
? A class is a collection of objects of similar type. -xample: |ango, Apple &
Orange are members of the class fruit. Classes are user-defined data types and behave
like the built-in types of a programming language.
©? The wrapping up of data and methods into a single unit (called class).
©? $ It refers to the act of representing essential features without including the
background details or explanation.
©?
It is the process by which objects of one class acquires the properties of
another class.
©? means the ability to take more than one form. The behavior depends
upon the types of data used in the operation.
©? ? It means that the code associated with a given procedure call is not
know until the time of the call at runtime. Binding refers to the linking of a procedure
call to the code to be executed in response to the call.
?
(
?? ?
OOP offers several benefits to both the program designer and the user. The new technology
promises greater programmer productivity, better quality of software and lesser maintenance cost.
X? Through inheritance, eliminate redundant code and extend the use of existing classes.
X? Build programs from the standard modules that communicate with one another.
X? The principle of data hiding helps the programmer to build secure programs that cannot
be invaded by code in other parts of the program.
X? It is possible to have multiple objects to co-exist without any interference.
X? It is possible to map objects in the problem domain to those objects in the program.
X? It is easy to partition the work in a project based on objects.
X? The data centered design approach enables us to capture more details of a model in an
implementable form.
X? Object oriented systems can be easily upgraded from small to large systems.
X? |essage passing techniques for communication between objects make the interface
descriptions with external systems much simpler.
X? aoftware complexity can be easily managed.
' ?%?$# *$ ?
?!??
$?? ?
·? eal-time systems
·? aimulation and modeling
·? Object oriented databases.
·? rypertext, hypermedia and expertext
·? AI and expert systems
·? Neural networks and parallel programming
·? Decision support and office automation system
·? CAD/CI| systems
' ?%?
'*?(?*?$?)$/$.??
)??* ?
·? Java does not include the C unique statement keywords goto, sizeof and typedef.
·? Java does not contain data type struct, union and enum.
·? Java does not define the type modifiers keywords auto, extern, register, signed and
unsigned.
·? Java does not support an explicit pointer type.
·? Java does not have a preprocessor and therefore we cannot use #define, #include and
#ifdef.
·? Java does not support any mechanism for defining variable arguments to functions.
·? Java requires that the functions with no arguments must be declared with empty
parenthesis and not with the void keyword as done in C.
·? Java adds new operator such as instance and >>>.
·? Java adds labeled break and continue statements
·? Java adds many features required for object oriented programming.
"#$ ?%?
'*?(?)$/$?$?*00!??
)??*00 ?
Java is a true object oriented language while C++ is basically C with object oriented extension.
·? V ???
?
?
?
·? Java does not have template classes.
·? Java does not support multiple Inheritances of classes. This is accomplished using a new
feature called 6.
·? Java does not support global variables. -very variable and method is declared within a
class and forms parts of that class.
·? Java does not use pointers.
·? Java has replaced the destructor functions with finalize () function with a finalize()
function.
·? There are no header files in JAVA.
·? Java does not have a preprocessor and therefore we cannot use #include and # ifdef
statement
` ??
? `
)$/$?$? '?
?
%?)$/$? ?',#1?$* $? %?%? '.??
*' (?%?/$' &?1-?'2& '?
'? '?','$-- ,.??
)$/$?3? ' ?
¦? Java expands the universe of objects that can move about freely in cyberspace. In a
network, two very broad categories of objects are transmitted between the server and your
personal computer: passive information and dynamic, active programs.
¦? Java is strongly associated with the Internet because the first application program written in
Java was rot Java, a web browser to run applets on Internet.
¦? Internet users can use Java to create applet programs and run then locally using ³Java-
enabled browser6 such as rot Java.
¦? sing Java-enabled browser to download an applet located on a computer anywhere in the
Internet and run it on local computer.
¦? Java is popularly known as Internet language.
¦? Java can be used to create two types of programs: applications and applets. An application
is a program that runs on your computer, under the operating system of that computer.
¦? An is actually a Java program, dynamically downloaded across the network, just
like an image, sound file, or video clip
?
? 4??
??
5?
?
%$? ?.?%$? ?%?*' (& ?
?)$/$??%?.??
)?? ?
â? is an open ±ended information retrieval system designed to be used in the internet¶s
distributed environment.
â? The web pages contain rT|L tags that enable us to find, retrieve, manipulate and display
documents world wide. The java could be easily incorporated into the web systems.
â? The incorporation of java into web pages has made it capable of supporting animation,
graphics, games and a wide range of special effects.
!""#
â? Java communicates with a web page through a special tag called <APPL-T>. The below
figure shows the communication steps:
? The user sends a request for an rT|L document to the remote computers web server.
The web server is a program that accepts a request processes the request, and sends
the required document.
? The rT|L document is returned to the users browsers. The document contains the
APPL-T tag, which identifiers the applet.
? The applet byte code is transferred to the user¶s computer. This byte code had been
created by the Java compiler using the Java source code file for that applet.
? The Java enabled browser on the users computer interprets the byte code and provides
output.
? The user may have further interaction with the applet but with no further
downloading from the provider¶s web server. This is because the byte code contains
all the information necessary to interpret the applet.
$
?
?
?
*&?$(&?(?(''?3? ?1!?
?
?
The Internet consists of more information represented in many formats and stored on
many computers. The web browsers are used to navigate through the information found on the
net. They allow retrieving the information spread across the Internet and displaying it using the
rypertext |arkup Language (rT|L).
An example of web browsers includes:
â? rot Java.
â? Netscape Navigator.
â? Internet -xplorer.
?
%?)$/$ ?
? rot java is the web browser from aun |icrosystems that enables the display of
interactive content on the web using the Java language. It was entirely in Java.
? hen the Java language was first developed and ported to the Internet, no browsers were
available that could run java applets.
? rot java is currently available for the aPAC/ aolaris platform as well as indows 95
and indows NT.
? ?
? Netscape navigator from Netscape Communication Corporation is a general purpose
browser that can run Java applets. ith versions available for window 95, NT, aolaris and
Applet |acintosh, NN is one of the most widely used browsers today.
½ ? Visual display about downloading process and indication of the number of bytes
downloaded. It also supports Javaacript, a scripting language used in rT|L
document.
?6
?
A popular web browser developed by |icrosoft for indows 95 and NT workstation.
Both the Navigator and -xplorer use tool bars, icons, menus and dialog boxes for easy
navigation. Internet -xplorer uses Just-In-Time (JIT) compiler which greatly increases the speed
of execution.
%&''
?
' ?%'???)$/$?/ '&$#?-$*% .?
%$? ?-$?(1?(1?*.?
? Byte code is a highly optimized set of instructions designed to be executed by the java
run-time system, which is called the ava irtual achine (JV|). It is an interpreter for byte
code.
In other word, the Java compiler produces an intermediate code known as byte code for a
machine that does not exist. This machine is called the Java Virtual |achine and it exists only
inside the computer memory.
The process of compiling a java program into byte code is referred to as virtual machine
code.
???
?
?
? ? ?
? ? ? ??????????(
?*
?
The virtual machine code is not machine specific. The machine specific code (known as
machine code) is generated by the java interpreter by acting as an intermediary between the
virtual machine and the real machine.
??
?
?
??
?
?
`
"#$ ?%?1?
?'$'? ?)$/$? %?"$-#!?
Java operators can be classified into number of related categories.
¦? Arithmetic Operator
¦? elational Operator
¦? Logical Operator
¦? Assignment Operator
¦? Increment and Decrement Operator
¦? Conditional Operator
¦? Bit-wise Operator
¦? apecial Operators
? -
?
** Logical AND
ii Logical O
+ Logical NOT
These operators are used to combine two or more relations to form compound conditions.
It is termed as logical expression or compound relational expressions. It also yields a value of
true or false.
6
?
a >b && x = = 10
Combines two or more relational expression is termed as a logical expression or a
compound relational expression.
?
?
?/
??
6
?
+;? +<?
+;?33?+<? +;?==?+<?
True True True True
True ×alse ×alse True
×alse True ×alse True
×alse ×alse ×alse ×alse
6
???
????? ???
? ?????
????
???
$
?
?
Assignment operators are used to assign the values of an expression to a variable.
Assignment operator is = (equal to).
a m
here exp1, exp2 and exp3 are expressions. The exp1 is evaluated first. If it is true, then
the expression exp2 is evaluated and becomes the value of the conditional expression. If exp1 is
false, exp3 is evaluated and its value becomes the value of the conditional expression.
6
? ??>
???
/01! ½0"
*!
,
*!.-!2·, 2
, !2
(
?
?
It is used for testing the bits or shifting them to the right to left. It may not be applied to
float or double.
? -
?
* Bitwise AND
i Bitwise O
3 Bitwise exclusive O
4 One¶s complement or bitwise unary NOT
ùù ahift left
** shift right
*** ahift right with zero fill
* Bitwise AND assignment
i Bitwise O assignment ?
?
3 Bitwise exclusive O assignment
?
** ahift right assignment ?
ùù ahift left assignment ?
*** ahift right zero fill assignment ?
?
6
?
1010 · 0101 (bitwise NOT)
1010 & 1111 · 1010 (bitwise AND- produces a 1 bit if both operands are also 1, otherwise 0)
1010 | 1110 · 1111 (bitwise O- produces a 1 bit if either of the bits in the operands is a 1,
otherwise 0)
1010 ^ 1111 · 0101 (bitwise XO ± produces a 1 bit if exactly one operand is 1, otherwise 0)
?
?
?
Java contains two special operators. They are:
¦? Instance of operator.
¦? |ember selection operator (.)
??
?
It is an object reference operator and returns true if the object on the left-hand side is an
instance of the class given on the right-hand side. It allows us to determine whether the object
belongs to a particular class or not.
6
person
? student
It returns µtrue¶ if the object person belong to class student, otherwise it is µfalse¶.
?4!5?
?
The dot operator is used to access the instance variables and methods of class objects.
6
person.age; // reference to variable age
person.salary ( ); // reference to method salary ()
?
?
?
Parentheses are used to alter the precedence of an operation. It is often necessary to
obtain the result.
)"5#
67
88 99 4
: ; <
8 9
** *** ùù
* * ù
+
3
i
**
ii
.-
=
?
''
The µ ? !" may be a single or group of statements. If the test expression is true
then statement block will be executed otherwise statement block will be skipped and execution
will jump to µstatement x¶.
6
?
if ( weight <10)
{
weight = weight +10;
}
weight = 0;
?
<5??C?
?
?
The ?statement is Java¶s conditional branch statement. It can be used to route program
execution through two different paths.
The conditions are evaluated from top to towards. The true condition is found, the
statement associated with it is executed and the control is transferred to the statement-x. hen
all the n conditions become false, then the final else containing the default statement will be
executed.
- 0?
1 ?
?
%? ? ?
? ? ?? a
?
#$?
? %??
? ? ?
#$??%''&')/*?
? ? ?#$??%2'(')&'2/*?
? ?
???*?
-*?..?
? ? %?
? ? ? #$??(3?
? ? ? ? a
#$*?
? ? ? ??#$?3?
? ? ? ? a
#$*?
? ? ? ?#$?)3?
? ? ? ? a
#$?.?+??4*?
? ? ? ?
? ? ? ? a
#$?.?+ 4*?
? ? /?
? /?
/?
?
*&?$(&? *%?$-!??
?
?
? Java has a built-in multi-way decision statement known as a ="# The switch statement
tests the value of a given variable against a list of case values and when a match is found, a block
of statements associated with the case is executed.
?
,
? ?
??4
6
5? ?
!
case value-1:
block-1;
break;
case value-2:
block-2;
break;
««
default:
default-block
break;
?
?6I?
The expression is an integer expression or characters. Value1, value 2 «.. are constants
or constants expression and are known as case labels. Block1, block2 are statement lists and may
contain zero or more statement.
hen the switch is executed, the value of the expression is successively compared
against the values value1, value2« If a case is found whose value matches with the value of the
expression, then the block of statements that follow the case are executed.
The break statement at the end of each block signals the end of a particular case and exit
from the switch statement, transferring the control to the statement x following the switch.
The default is an optional case. The selection process of switch statement is as follows:
'>
a
',C ½
',C A
#@
?
6
?
class city
{
public static void main(atring args[])
{
char choice;
aystem.out.println(³choice6);
aystem.out.println(³|· |adras6);
aystem.out.println(³B· Bombay6);
aystem.out.println(³C· Calcutta6);
aystem.out.flush();
try{
switch(choice = (char)aystem.in.read()
{
case µ|¶
case µm¶: aystem.out.println(³|adras6);
case µB¶
case µb¶: aystem.out.println(³Bombay6);
case µC¶
case µc¶: aystem.out.println(³Calcutta6);
default: aystem.out.println(³Invalid6)¶
}
}
catch(-xception e)
{
aystem.out.println(³error in statement6);
}
}
}
??
?
A switch is part of the statement sequence of an outer switch. This is called a nested
switch.
switch (count)
{
case 1:
switch(target)
{
case 0:
aystem.out.println (³target is zero6);
break;
case 1:
aystem.out.println (³target is one6);
break;
}
break;
case 2: «.
? ? ? ?
There are three important features of the switch statement:
½ ? The ="#differs from the "?in that switch can only test for equality, whereas " can
evaluate any type of Boolean expression.
A ? No two constants in the same ="# can have identical values. A ="#
statement enclosed by an outer ="# can have constants.
B ? A ="# statement is usually more efficient than a set of nested "
?
)A?
?
?
Java iteration statements create loops. The process of repeatedly executing a block of
statements is known as looping. The statements in the block may be executed any number of
times from zero to infinite number. If a loop continues forever, it is called as infinite loop.
A program loop consists of two segments
©? Body of the loop
©? Control statement
The control statement tests certain conditions and then directs the repeated execution of the
statements contained in the body of the loop. It classified into 2 types.
? +*
??
? 6+*
??
(
"
?
(
" ?
'>
',"
+*
?·?The control conditions are tested before the start of the loop execution.
If the conditions are not satisfied then the body of the loop will not be executed.
6+*
??·?The test is performed at the end of the body of the loop and therefore the
body is executed unconditionally for the first time.
The looping process involving four steps:
X? aetting and initialization of a counter.
X? -xecution of the statements in the loop.
X? Test for a specified condition for execution of the loop.
X? Incrementing the counter.
The java language provides three constructs. They are:
·?
?
?
·? ?
?
·?
?
?
?
"#$ ?% #?$-?$?, /?$?"$-#!??
?
?
It repeats a statement or block while its controlling expression is true.
????6 ???????????
initialization
?
test condition
!
body of the loop2
?
?
The while is an entry-controlled loop statement. The test condition is evaluated and if the
condition is true the body of the loop is executed. After execution of the body, the test condition
is once again evaluated and if it is true, the body is executed once again. This process of repeated
execution of the body continues until the condition finally becomes false, and the control is
transferred out of the loop.
The body of the loop may have one or more statements. The braces are needed only if
the body contains two or more statements.
?
6
?
?5- ?%?
?
? ?? a
? #$?
%?
? ?? a
? #$?
%?
?%? ???*?
? ??'?6*?
7-?
? 7-..??6..*88???
%?
? ?a
+9??4.*?
a
+:4.*?
?/? ? ;;*?
?/? /?
/?/?
?
esult is 150
?
'? ? ? ? ?
The body of the while loop can be empty. This is because a null statement is syntactically valid
in Java.
?
"#$ ??$-? %?& $(#?"$-#!??
It executes the body of the loop before the text is performed.
6 ??
initialization
?????
????
??????????? ?
?
?
?????G ?
?
?
?
The program evaluates the body of the loop statement first. At the end of the loop, the
test condition in the while statement is evaluated. If the condition is true the program continues
to evaluate the body of the loop once again. This process continues as long as the condition is
true.
hen the condition becomes false, the loop will be terminated and the control goes to the
statement that appears immediately after the =#" statement.
The test condition is evaluated at the bottom of the loop. And therefore the body of the
loop is executed at least once.
6
??
???*?
? ??%?
? ? a
+:?+?.*?
? ? ?;;*?
? ???????/?7-*?
' ?$???L
'A?$-!?
The for loop is a entry-controlled loop that provides a more concise loop control
structure.
>,-
< *?? *?
!
????????????????
??? #
The execution of the µfor¶ statement is as follows:
? Initialization of the control variables is done first using assignment statements. " = 0,
count = 0. The variables " and count are known as loop control variables.
? The value of the control variable is tested using test condition. If the condition is true
the body of the loop is executed, otherwise the loop is terminated and the execution
continues with the statement that immediately follows the loop.
? hen the body of the loop is executed the control is transferred back to the µfor¶
statement after evaluating the last statement in the loop.
6
?
class ×orex
{
public static void main(atring a[])
{
int n;
for(n=10; n>0; n--)
aystem.out.println(n);
}
}
?#??
?
?
?? ?
It is possible to declare the variable inside the initialization portion of the
for (int n=10; n>0; n--)
aystem.out.println(n);
hen we declare a variable inside a for loop, there is one important point to remember:
the scope of that variable is limited to the loop. Outside the loop, the variable will cease
to exist. If we need to use the loop control variable elsewhere in our program, we will not be
able to declare it inside the loop.
- 0?
class forst
{
public static void main(atring a[])
{
int num;
boolean isprime = true;
num=14;
for(int i =2; i< num/2; i++)
{
if(num % i) == 0)
{
isprime = false;
break;
}
}
if(isprime)
aystem.out.println(³prime6);
else
aystem.out.println(³Not prime6);
}
}
$?
??? ?
1) |ore that one variable can be initialized at a time in the for statement.
-xample : for (i=0, j=0; i<10; i++) // Initialization section has
separated by comma.
2) The increment section has more than one part.
-xample: for (n=1, m=50; n<=m; n=n+1, m=m-1)
---
---
}
3) The test condition may have any compound relation and the testing need not be limited only to
the loop control variable.
-xample:
sum=0;
for (i=1, i<20 && sum <100; i++)
{
---
}
4) It is also permissible to use expression in the assignment statements of initialization and
increment section.
aystem.out.println();
}
aystem.out.print(³\n6);
}
}
here,
is the name of a label that identifies a block of code. The control is transferred out
of the named block of code. A
?is any valid Java identifier followed by a colon.
? 6
?
class Breakst
{
public static void main(atring a[])
{
first: for(int i=0; i<3; i++)
{
aystem.out.print(³Pass ³ + i + ³: ³);
for(int j=0; j<100; j++)
{
if(j == 10)
break first;
}
aystem.out.println(³This will not print6);
}
aystem.out.println(³Loop complete.6);
}
}
?
&?
?
It helps to skip a part of the body of the loop under certain conditions. The part of the
program loop that processes the application details is skipped and the execution continues with
the next loop operations.
Java supports another similar statement called the continue statement. The break
statement called the continue statement.
The break which causes the loop to be terminated, the " as the name implies
causes the loop to be continued with the next iteration after skipping any statement in between.
>,-"2
? 6
?
class Cont {
public static void main (atring a[]) ? ?
{
for(int i =0; i<10; i++) { ? ?
aystem.out.print(i+ ³6);
? '?
If(i%2 == 0)
Continue;
aystem.out.println(³6);
}
}
}
?
It is used to explicitly return from a method. It causes program control to transfer back to
the caller of the method. It is categorized as a jump statement.
At any time in a method the return statement can be used to cause execution to branch
back to the caller of the method. The statement immediately terminates the method in
which it is executed.
? 6
?
class eturnex
{ public static void main(atring a[]) ?
?
!?
{ boolean t = true;
aystem.out.println(³Before the return.6);
if(t) return;
aystem.out.println(³This won¶t execute.6);
}
}
As soon as is executed, control passes back to the caller.
c'c
Java program may contain many classes of which only one class defines a main method
classes contain data members and methods that operate on the data members of the class.
|ethods may contain data type declarations and executable statements.
"" a
cF5"
"
"
"""
"#@
!
"#@@"""
?2
4,
?
??
?)$/$?5?
?
?
??
It contains set of comment lines giving the name of the program, the author and other
details. Comments must explain why and what of classes and how of algorithms.
/** « */ is known as @" . It is used for generating
documentation automatically.
?
9
?
??
This statement declares a package name and informs the compiler that the classes defined
here belong to this package.
6
?cF5>F52·This statement is optional.
?
?
??
Importstatement is similar to the #include statement in C.
">F5 >2
It instructs the interpreter to load the > class contained in the package student.
sing import statements, we can access to classes that are part of other named packages.
?
?
?
?
An interface is like a class but includes a group of method declarations. It is an optional
section and is used only to implement the multiple inheritance features in the program.
a% 6?
?
? *?
?
Class Classname · declares a class,
?
which is an object-oriented construct. Java is
?`
? a true object-oriented language and therefore
everything must be placed inside a class.
? Class is a keyword.
?
?????? ?
??
??
??
?
?
-very class definition in java begins
???????? with an opening brace ³{³and ends with
matching closing brace ³}6 appearing in the last line.
?
-?
??
c ? ?? ?a
?
#?$?defines a method named . m method is
the starting point for the interpreter to begin the execution of the program. A java application
can have any number of classes but only one of them must include a main method to initiate the
execution.
atring args[ ] declares a parameter named args, which contains an array of objects of the class
type string.
?
??
?
The only executable statement in the program is:
a
(³rai16);
? here, println method is a member of the out object, which is a static data member of
system class. This line prints the string += >4
Println · appends a newline character to the end of the string. -very java statement must
end with a semicolon.
"!- A named memory location that may be assigned a value by our program. The value of
a variable may be changed during the execution of the program.
a% ',"
? " · it declares an integer variable
called Java requires that variables
?`
? be declared before they are used.
? "-
?+
I?
?
?????? ?
??
??
?? Java defines several data types,
including integer, character, and floating
???????? point.
) &('
'
?
,-?VBC?- ??
? ? ? ??
?'?
0?
«? java ± the loader for Java applications. This tool is an interpreter and can interpret the
class files generated by the javac compiler.
«? javac ± the compiler, which converts source code into Java bytecode
«? jar ± the archiver, which packages related class libraries into a single JA file. This tool
also helps manage JA files.
«? javadoc ± the documentation generator, which automatically generates documentation
from source code comments
«? jdb ± the debugger
«? jps ± the process status tool, which displays process information for current Java
processes
«? javap ± the class file disassembler
«? appletviewer ± this tool can be used to run and debug Java applets without a web
browser.
«? javah ± the C header and stub generator, used to write native methods
«? extcheck ± a utility which can detect JA-file conflicts.
«? jstack ± (experimental) utility which prints Java stack traces of Java threads
«? jstat ± (experimental) Java Virtual |achine statistics monitoring tool
«? jinfo ± (experimental) This utility gets configuration information from a running Java
process or crash dump.
«? jmap ± (experimental) This utility outputs the memory map for Java and can print shared
object memory maps or heap memory details of a given process or core dump.
G'
?
?8!??
' ?%'???)$/$?8!??
The tokens of a language are the basic building blocks which can be put together to
construct programs.
! <=
| !7
|
|
|
| ?
?
A token can be a reserved word (such as "or =#"), an identifier (such as ! or ), a
constant (such as A/ or H @@H), a delimiter (such as ! or2) or an operator (such as 8 or
).
?
)?
?
??
M? The smallest units of java language are the characters used to write java tokens. These
characters are defined by the nicode character set.
M? The
"@ is a 16-bit character coding system. There are 216 = 65,536 nicode
characters. aince this character set is very large and its structure very complex. It is
derived from 24 languages from America, -urope, |iddle -ast, Africa and Asia.
M? It includes letters, digits and punctuation marks. These characters are defined by the
nicode character set.
?
?81'!?
8
?
Java has reserved 50 words as keywords. It cannot be used as names for variables,
classes, methods and so on. Keywords are written in lower-case letters. aince java is case
sensitive. These identifiers have built-in meanings that cannot change. Thus, programmers
cannot use these identifiers for anything other than their built-in meanings.
?
abstract continue goto package switch ??????????????????? ?
''
"#$ ?$(&?)$/$?$-?$? ?1?
?$-.??
%$? ? $-.? %? ? %? )$/$? $-?
'?
'-? %?
?*?$?*00.??
A statement is an executable combination of tokens ending with a semicolon (;) mark.
atatements are usually executed in sequence order.
??
?
·? '>·sed during program development as a place holder.
·? !@ · Labels are not keywords. It is used as the arguments of jump
statements.
·? ',"·?There are 7 types of expression statements.
? Assignment
!? Pre-increment
? Pre-decrement
@? Post-increment
? Post-decrement
? |ethod call,
5? Allocation expression
*?
?
It contains the following three types:
©? aelection statement
©? Iteration statement
©? Jump statement
©?
?
??
There are three types of selection statements in java. They are
1. if statement
2. if-else statement
3. switch statement
©
?
??
These specify how and when looping will take place. There are three types of iteration
statements.
They are:
½ hile
A do
B for.
©?)?
??
It passes control to the beginning or end of the current block or to a labeled statement.
×our types:
½ ? break,
A ? continue
B ? return
ß ? throw.
c ?
?
It is used for handling issues with multithreading.
c?,?
? ?
It is used for safe handling of code that may cause exceptions. These statements use the
keywords try, catch, and finally.
&
?$''$1 ??
An array is a group of like-typed variables that are referred to by a common name.
Arrays of any type can be created and may have one or more dimensions. A specific element in
an array is accessed by its index. Arrays offer a convenient means of grouping related
information.
A particular value is indicated by writing a number called
6?
or in
brackets after the array name.
??????????6
6½072
It represents the sum of 10 numbers. Thecomplete set of values is referred to as an array;
the individual values are called
.
?
%1?$''$1?$'?$ '??&?*-$'??$?(&*%?
?'#$?
/$' $(#.?
$??
???
?
?
The ability to use a single name to represent a collection of items and to refer to an item
by specifying the item number enables us to develop concise and efficient programs.
There are 3 types:
¦? One-dimensional array
¦? Two dimensional array
¦? |ulti-dimensional array
?
"#$ ?? - $#?$''$1!?
A list of items can be given one variable name using only one subscript and such a
variable is called a single-subscripted or one-dimensional array.
An array must be declared and created in the computer memory before they are used.
Creating an array involves 3 steps.
·? Declare the array.
·? Create memory locations.
·? Put values into the memory locations.
?
?? ?
There are two forms to declare
;!?
?
MNI?
<!?
MN?
I?
?
*
?? ?
Java allows to create arrays using new operator. = is a special operator that allocates
memory.
? ,
?? ??+
?F?
?
M
NI?
????????????????????????????????????????6??? ???F?
?M;GNI?
p[i][j] = i * j;
aystem.out.println(p[i][j]);
aystem.out.println(³ ³);
}
}
}
'
?/*'!??
%$? ?%?
'*?(?/*'?$?$''$1.
%$?$'?%?-%?%$?$'?$/$ #$(#? ?/*'.? ?
·? Vector represents a collection of similar items. In Java, the m M class is defined in
EC " package that implements a growable array of objects.
·? Like an array, its items can be accessed using an integer index. Once the m has been
created, the size of a m can be grown or reduced as needed to adding and removing
items to that vector.
·? Vectors are extremely easy to use, and implement. Creating a vector is easy. e just need
to simply define a variable of type Vector, and call the vector constructor.
Vectors are
¦? -
- store only one kind of element;
¦? - permit clients to access values using integers that indicate position;
¦? - - provide fast access to values.
nlike arrays, vectors are - we can continue to add elements to a vector, even if the
vector has grown beyond its original size
- intVect = new Vector ( ); // declaring without size
list = new Vector (3); // declaring with size
?
Vectors possess a number of advantages over arrays.
¨? It is convenient to use vectors to store objects.
¨? A vector can be used to store a list of objects that many vary in size.
¨? e can add and delete objects from the list as and when required.
In vector we cannot directly store simple data types in a vector; we can only store
objects. This can be done using the wrapper classes.
?
/
?-
?
?
-
?? 9?
?
è? list.add-lement(item) è? Adds the item specified to the list at the list
è? list.elementAt(10) at the end.
è? list.size( ) è? Gives the name of the 10th object.
è? list.remove-lement è? Gives the number of objects present.
è? list.remove-lementAt(n) è? emoves the specified item from the list.
è? list.removeAll-lement( ) è? emoves the item stored in the nth position
è? list.copyInto(array) of the list.
è? list.insert-lementAt(item, n)? è? emoves all the elements in the list.
è? Copies all items from list to array.
è? Inserts the item at nth position.?
?
6
?
import java.util.Iterator;
import java.util.Vector;
public class Vector1
{
public static void main(atring[] args)
{
Vector v = new Vector();
? ? ::?&??
?
?
?
??
?
v.add("1");
v.add("2");
v.add("3");
v.add("4");
::?&?
??
??
?
?
?
aystem.out.println("Vector aize :"+v.size());
aystem.out.println("Getting elements of Vector by get method ");
aystem.out.println("*****************************************");
::?&?
?
???
?
??
?
?
for(int i=0;i<v.size();i++)
{
aystem.out.println("Vector -lement "+i+" :"+v.get(i));
}
)?
*
? ?$
??)
?*
? );
}
ööööööööööööööööööööööööööööööööööööööööö? }
$
?+
*
?? ?
$
?+
*
?? ?
$
?+
*
?? ?
$
?+
*
?? ?
)?
*
? ?$
??*
?
ööööööööööööööööööööööööööööööööööööööööö?
????????,?
*
? ?
???????? ?-.?.?.? /?
????????,?
*
?
?
??? ??
????????,?
?
*
? ?
? ??
????????,?
?
*
? ?
?? ? ?
2 "!#?
?
%
(('
?' ,!?
%??*#$'?$?' ,.?
atrings represent a sequence of characters. The easiest way to represent a sequence of
characters in java is by using character array.
char name[]=new char[4];
name[0] = µJ¶;
name[1] = ¶a¶;
name[2] = ¶v¶;
name[3] = µa¶;
In Java, strings are class objects and implemented using two classes, namely string and
string buffer. A Java string is an instantiated object of the string class. It is not a character array
and is not NLL terminated.
6 ?
? ????????
I? ?
G
????????4'5?
?
?F?
?4HO5I????
?
6 ??
atring name;
G
!
4'5
name = new atring(³nila6);
To get the length of the string using the length method of the string class, using
parentheses string can be concatenated using the + operator.
?
?
?
?
?
?
?
?
6 ?
atring name1 = new string(³ aathya 6);
atring name2 = new string(³ |oorthy 6);
atring name = name1 + name2;
a
#@ F
th
ystem.ou
s1.setcharAt(n,¶x¶) |odifies the n character to x. t.println(
s1.append(s2) Appends the string s2 to s1 at the end. name);
Inserts the string s2 at the position n of the string s1. ?
s1.insert(n,s2)
aet the length of the string s1 to n. If n<s1.length ( ) s1 is
s1.setLength(n)
truncated. If n>s1.length ( ) zeros are added to s1.
? ?
atring a[] = new atring[3]; // It holds three string constants.
?
?
$-?-?
?%?' ,?-%?%$?$'?&? ?)$/$.??
The string class defines a number of methods that allow us to accomplish a variety of string
manipulation tasks.
#@ F@
s2 = s1.toLowerCase Converts the string s1 to all lowercase.
s2 = s1.topperCase Converts the strings s1 to all ppercase.
s2 = s1.replace(µx¶,¶y¶); eplace all appearances of x with y.
s2 = s1.trim( ); emove white spaces at the beginning and end of the string s1.
s1.equals(s2) eturns µtrue¶ if s1 is equal to s2.
s1.equalsIgnorecase(s2) eturns µtrue¶ if s1 = s2, ignoring the case of characters.
s1.legnth( ) Gives the length of s1.
s1.CharAt(n) Gives nth character of s1.
s1.compareTo(s2) eturns negative if s1<s2, positive if s1>s2, and zero if s1 is equal s2.
s1.concat(s2) Concatenates s1 and s2.
s1.substring(n) Gives substring starting from nth character.
s1.substring(n, m) Gives substring starting from nth character up to mth
atring.ValueOf(p) Creates a string object of the parameter p.
p.toatring( ) Creates a atring representation of the object p.
s1.indexOf(µx¶) Gives the position of the first occurrence of µx¶ in the string s1.
s1.indexOf(µx¶,n) Gives the position of µx¶ that occurs after nth position in the string s1.
atring.ValueOf(Variable) Converts the parameter value to string representation.
'?*#$!?
?
? ?
A string buffer implements a mutable sequence of characters. A string buffer is like a
a, but can be modified. It contains some particular sequence of characters, but the length
and content of the sequence can be changed through certain method calls.
atring buffers are safe for use by multiple threads.
?
?
(
?
?
substring(start,end)
The mantissa is either a real number expressed in decimal notation or an integer. The
exponent is an integer with an optional plus or minus sign.
A floating point constant has 4 parts:
· a whole number
· a decimal point
· a fractional part
· an exponent.
?
?
?
?
?
? 2
?
?
?
?
" #
"
?
?
?
? %
? # # # `
?
?
?
? $
×
`
& $
?
+?
?
A whole number with decimal point. There are two kinds of floating point storage in
Java.
Ö? The type values are single-precision numbers while the double types
represent double-precision numbers.
Ö? ! precision types are used when we need grater precision in storage of
floating point numbers.
c''
?