PDF Archive

Easily share your PDF documents with your contacts, on the Web and Social Networks.

Share a file Manage my documents Convert Recover PDF Search Help Contact



JavaUnit1 .pdf



Original filename: JavaUnit1.pdf
Author: ILOVEPDF.COM

This PDF 1.6 document has been generated by ILOVEPDF.COM, and has been sent on pdf-archive.com on 23/08/2015 at 15:42, from IP address 103.5.x.x. The current document download page has been viewed 372 times.
File size: 262 KB (12 pages).
Privacy: public file




Download original PDF file









Document preview


Java/J2EE

UNIT-1 INTRODUCTION TO JAVA

06CS753

1. Introduction to Java
x Java is an object-oriented programming language developed by Sun
Microsystems, a company best known for its high-end Unix workstations.
x Java is modeled after C++
x Java language was designed to be small, simple, and portable across platforms
and operating systems, both at the source and at the binary level (more about
this later).
x Java also provides for portable programming with applets. Applets appear in a
Web page much in the same way as images do, but unlike images, applets are
dynamic and interactive.
x Applets can be used to create animations, figures, or areas that can respond to
input from the reader, games, or other interactive effects on the same Web
pages among the text and graphics.
1.1 Java Is Platform-Independent
Platform-independence is a program's capability of moving easily from one
computer system to another.
x Platform independence is one of the most significant advantages that Java has over
other programming languages, particularly for systems that need to work on many
different platforms.
x

Java is platform-independent at both the source and the binary level.

1.2 Java Development Kit (JDK)- Byte code
x Bytecodes are a set of instructions that look a lot like machine code, but are not
specific to any one processor
x Platform-independence doesn't stop at the source level, however. Java binary files
are also platform-independent and can run on multiple platforms without the need to
recompile the source. Java binary files are actually in a form called bytecodes.
1.3 Object-Oriented Programming
x Many of Java's object-oriented concepts are inherited from C++, the language on
which it is based, but it borrows many concepts from other object-oriented languages
as well.
Page 6

Java/J2EE

06CS753

x Java includes a set of class libraries that provide basic data types, system input and
output capabilities, and other utility functions.
x T h e s e ba s i c c l a s s e s a re p a r t o f t h e J a va d e ve l op me n t ki t , wh i c h a l s o h a s c l a s s e s
t o s u p p o rt n e t w o r ki n g, c o mm on I n t e rn e t p r o t oc ol s , a n d u s e r i n t e r f a c e t o ol k i t
f u n c t i on s .
x B e c a u s e t h e s e c l a s s l i b r a ri e s a r e w ri t t e n i n Ja v a , t h e y a r e p o r t a bl e a c ros s
p l a t f o r ms a s a l l Ja v a a p p l i c a t i on s a re .
1.4 Creating a simple Java Program
H e ll o W o r l d ex a mp l e :
c l a s s He l l oW o r l d {
p u b l i c s t a t i c v o i d m a i n ( St r i n g a r gs [ ] ) {
S y s t e m. ou t . p r i n t l n (" H e l l o W o r l d ! " );
}
}
T h is p r o g r a m h a s t wo m a in p a rt s :
x A l l t h e p r o g ra m i s e nc l os e d i n a c l a s s d e fi n i t i on — h e re , a c l a s s c a l l e d
H e ll o W or l d.
x T h e b od y o f t h e p r og r a m (h e r e , ju s t t h e o n e l i n e ) i s c on t a i n e d i n a me t h o d
( f u n c t i on ) c a l l e d m a i n ( ) . I n J a v a a p p l i c a t i on s , a s i n a C o r C + + p r o g r a m , m a i n ( ) i s
t h e fi rs t m e t h od (fu n c t i on ) t h a t i s ru n wh e n t h e p r o g r a m i s e x e c ut e d .
1.5 Compiling the above program :
x

I n Su n ' s JD K, t h e Ja v a c o mp i l e r i s c a l l e d ja va c .

javac HelloWorld.java
x W h e n t h e p r o g ra m c o mp i l e s wi t h ou t e r r or s , a f i l e c a l l e d H e l l oW o r l d . c l a s s i s
c re a t e d , i n t h e s a me d i r e c t o r y a s t h e s ou rc e f i l e . T h i s i s t h e Ja va by t e c od e f i l e .
x T h e n ru n t h a t b yt e c od e f i l e u s i n g t h e J a va i n t e rp r e t e r. I n t h e J D K, t h e Ja va
in t e rp r e t e r is c a ll e d s imp l y ja v a .
j a v a H e l l oW o r l d
I f t h e p r o g r a m w a s t y p e d a n d c o m p i l e d c o r r e c t l y, t h e o u t p u t w i l l
be : " He ll o W o r l d! "

Page 7

Java/J2EE

2. Variables and Data Types

06CS753

x V a r i a b l e s a re l oc a t i on s i n me m o r y i n wh i c h v a l u e s c a n be s t o re d . T h e y h a ve a
n a me , a t yp e , a n d a va l u e .
x J a v a h a s t h re e ki n d s o f v a r i a b l e s : i n s t a n c e v a r i a b l e s , c l a s s va r i a b l e s , a n d
l o c a l v a ri a bl e s .
x I n s t a n c e va r i a b l e s , a re u s e d t o d e fi n e a t t ri bu t e s o r t h e s t a t e f or a p a rt i c u l a r
o b j e c t . C la s s v a ri a bl e s a re s im i la r t o in s t a n c e v a ri a bl e s , e x c e p t t h e i r va lu e s a p p l y
t o a l l t h a t c l a s s ' s in s t a n c e s ( a n d t o t h e c la s s it s e l f ) ra t h e r t h a n h a v in g d if f e re n t
v a lu e s f o r e ac h o b je c t .
x L o c a l v a ri a bl e s a re d e c l a re d a n d u s e d i n s i d e me t h o d d e f i n i t i on s ,
f o r e x a m p l e , f o r i n d e x c o u n t e r s i n l o op s , a s t e m p o r a r y va r i a b l e s , o r t o h o l d
v a l u es t h a t y ou n e e d o n l y i n s i d e t h e m e t h od d e f i n i t i on i t s e l f
V a r i a b l e d e c l a r a t i on s c on s i s t o f a t yp e a n d a v a r i a b l e n a m e :
E x a mp l es :
int myAge;
Strin g
myName;
boolean isTired;
2. 1 Integer types.
Type Size Range
b y t e 8 bi t s —12 8 t o 127
s h o rt 1 6 bit s — 3 2 , 7 6 8 t o 3 2 , 7 6 7
in t 3 2 b its —2, 1 47, 4 83, 6 48 t o 2, 14 7, 48 3, 64 7
— 9 2 2 3 3 7 2 0 3 6 8 5 4 7 7 5 8 0 8 t o 9 2 2 3 3 7 2 0 3 6 8 5 4 7 7 5 8 0 7 l on g 6 4 b i t s

2. 2 Floating-point
T h i s i s u s e d f o r n u m b e r s w i t h a d e c i m a l p a rt . J a v a f l o a t i n g - p o i n t n u m b e rs a re
c o mp l ia n t w i t h I E E E 7 5 4 ( a n i n t e r n a t i o n a l s t a n d a r d f o r d e f i n i n g f l o a t i n g - p o i n t
n u m b e rs a n d a rit h me t ic ).

Page 8

Java/J2EE

06CS753

T h e r e a r e t w o f l o a t i n g - p o i n t t yp e s : f l o a t ( 3 2 b i t s , s i n g l e - p re c i s i on ) a n d d ou b l e ( 6 4
bi ts, d ou bl e -p re c i s i on ).

2. 3 Char
T h e c h a r t yp e i s u s e d f o r i n d i v i d u a l c h a ra c t e rs . B e c a u s e J a v a u s e s t h e U n i c o d e
c h a ra c t e r s e t , t h e c h a r t y p e h a s 1 6 b i t s o f p r e c i s i o n , u n s i g n e d .
2. 4 Boolean
T h e b o o l e a n t yp e c a n h a v e on e o f t w o va l u e s , t ru e o r f a l s e . N o t e t h a t u n l i k e i n
o t h e r C - l i k e l a n gu a g e s , b o o l e a n i s n o t a n u m b e r, n o r c a n i t b e t re a t e d a s on e . A l l
t es t s o f B o o le an v a r i a b l e s s h o u l d t e s t f o r t r u e o r f a l s e .
2. 5 Literals
L i t e r a l s a r e u s e d t o i n d i c a t e s i m p l e v a l u e s i n y ou r J a v a
p r o g r a ms . N u m b e r L i t e r a l s
x T h e re a r e s e ve r a l in t e g e r lit e ra ls . 4 , f o r e x a mp le , is a d e c i ma l in t e g e r l it e r a l o f
t yp e i n t
x A d e c i m a l i n t e ge r l i t e r a l l a r ge r t h a n a n i n t i s a u t o ma t i c a l l y o f t yp e l on g.
x F l o a t i n g -p o i n t l i t e r a l s u s u a l l y h a ve t w o p a rt s : t h e i n t e ge r p a rt a n d t h e d e c i m a l
p a rt — f o r e x a mp l e , 5 . 6 7 7 7 7 7 .
B ool ean L ite r als
B o o l e a n l i t e r a l s c on s i s t o f t h e k e y w o r d s t ru e a n d f a l s e . T h e s e k e y w o r d s c a n
be u se d a n y wh e re n e e d e d a t e s t o r a s t h e o n l y p os s i b l e v a l u e s f o r b o o l e a n
v a r i ab les .
2. 6 Ch ar ac t er L i t er als
C h a r a c t e r l i t e r a l s a r e e x p re s s e d b y a s i n g l e c h a r a c t e r s u r r ou n d e d b y s i n g l e
q u ot e s : ' a ' , ' # ' , ' 3 ' , a n d s o o n . C h a r a c t e r s a r e s t o r e d a s 1 6 - b i t U n i c o d e c h a r a c t e r s .
3. Expressions and Operators
x E x p r e s s i on s a r e t h e s i mp l e s t f o rm o f s t a t e m e n t i n J a va t h a t a c t u a l l y
a c c o mp l i s h e s s o m e t h i n g. E x p r e s s i o n s a re s t a t e me n t s t h a t r e t u rn a v a l u e .
x O p e r a t o r s a re s p e c i a l s y m b ol s t h a t a r e c o mm o n l y u s e d i n e x p re s s i on s .

Page 9

Java/J2EE

06CS753

A r i t h me t i c a n d t e s t s f o r e q u a l i t y a n d m a gn i t u d e a r e c o m m on e x a mp l e s o f
e x p r e s s i on s . B e c a u s e t h e y re t u rn a v a l u e t h e v a l u e c a n be a s s i gn e d t o a
v a r i a b l e o r t e s t t h a t va l u e i n ot h e r J a va s t a t e m e n t s .

O p e ra t o rs i n J a v a i n c l u d e a r i t h m e t i c , va r i o u s f o r m s o f a s s i g n m e n t ,
i n c re m e n t a n d d e c r e m e n t , a n d l o g i c a l o p e r a t i o n s .
3. 1 Arithmetic
J a v a h a s fi v e op e r a t o rs f o r ba s i c a ri t h me t i c
A r ithm etic oper ator s.
Operator
Exa m
Meaning
ple
+
A d d i t i on
3+4

S u bt ra c t i on 5 —
*
Mu ltiplica tion75 * 5
/
D ivis i on
14 / 7
%

M odu lu s 20 % 7

Examp le p rogra m :
c lass Arit h met icT es t {
pu blic st at ic void main (St rin g args []) {
s h ort x = 6;
in t y = 4;
float a = 12. 5f;
fl oa t b = 7 f;
Sy
= x"
S
ysstteem
m..oou
utt..pprriinnttllnn((""xx +i s y" +
+

}}

S y s t e m . ou t . p r i n t l n ( " x - y
+
Sys t e m. ou t . p ri n t l n ("a i s "
S y s t e m . ou t . p r i n t l n ( " x / y
+
Sys t e m. ou t . p ri n t l n ("a / b

+
",

y
"

is

(
x

+ y));

- y));
(
x / y));
+ a + ", b i s "
= " (
%
= " + (xa / yb) ) ;

+
y);

= "

S y s t e m . ou t . p r i n t l n ( " x % y = "
+
Assignment operators.

+
b;

(
x

Expression Meaning
x += y
10

x=x+y
Page

Java/J2EE
x

06CS753

—= y x = x — y

x *= y

x=x*y

x = x / y x /= y
Incrementing and Decrementing
x + + i n c r e m e n t s t h e va l u e o f x b y 1 j u s t a s i f y o u h a d u s e d t h e e x p r e s s i o n x = x + 1 .
Si mi la rl y x - - dec r em e n t s t h e valu e of x by 1.
Ex erc is e : writ e th e differen c e bet ween :
y = x++;
y

= ++x;

Comparison operators.
Operator Meaning

Example

==

x == 3

Equal

!=
<
>
<=

N ot equ al
L ess th an
G re a t e r t h a n
L ess
th an
or
e qu a l t o
>= G reat e r th an or equ al t o

x
x
!=<
x>
3
3
x
3
<=
x3 >= 3 Logical

Operators
x E x p r e s s i on s t h a t re s u l t i n b o o l e a n v a l u e s (f o r e x a mp l e , t h e c o mp a ri s on
o p e r a t o rs ) c a n be c om b i n e d b y u s i n g l o g i c a l op e ra t o rs t h a t re p r e s e n t t h e l o g i c a l
c o m bi n a t i on s
x A ND , OR, XOR, an d logi c a l NOT .
x F o r A N D c o m b i n a t i on s , u s e e i t h e r t h e & o r & & . T h e e x p r e s s i on w i l l be t ru e
o n l y i f b ot h e x p r e s s i on s a re a l s o t ru e
x F o r O R e x p re s s i on s , u s e e i t h e r | o r | | . O R e x p re s s i on s r e s u l t i n t ru e i f e i t h e r o r
b o t h o f t h e op e ra n d s i s a l s o t ru e
x I n a d d i t i on , t h e re i s t h e X O R op e r a t o r ^ , wh i c h re t u rn s t ru e on l y i f i t s
o p e r a n d s a r e d i f f e r e n t ( on e t ru e a n d on e f a l s e , o r v i c e v e rs a ) a n d f a l s e o t h e r w i s e
( e v en i f b oth a r e t r u e ) .
x I n ge n e r a l , on l y t h e & & a n d | | a re c o m m on l y u s e d a s a c t u a l l o g i c a l
c o m bi n a t i on s . & , | , a n d ^ a re m o re c o m m on l y u s e d f o r b i t w i s e l o g i c a l
11

Page

Java/J2EE

06CS753

o p e r a t i on s .
x F o r N O T , u s e t h e ! op e r a t o r w i t h a s i n g l e e x p re s s i on a r gu me n t . T h e va l u e o f t h e
N O T e x p r e s s i on i s t he n e g a t i on o f t h e e x p re s s i on ; i f x i s t ru e , ! x i s f a l s e .

Bitwise Operators
These are used to perform operations on individual bits in integers.
Operator Meaning
&

B it w is e A ND

|

B it w is e OR

^

B it w is e XOR

<<

L e ft s h if t

>>

R i gh t s h i ft

>>>

Z e r o f i l l r i gh t s h i f t

~

B i t w i s e c omp l e me n t

<<=

L e ft s h i f t a s s i gn me n t ( x = x < < y )

>>=

R i gh t s h i ft a s s i gn m e n t ( x = x > > y )

> > > = Z e ro f i l l ri gh t s h i ft a s s i gn me nt (x = x >>> y)
x & = y A N D a s s i gn me n t (x = x & y )
x | = y O R a s s i gn m e n t (x + x | y )
x^= y

X O R a s s i gn me n t (x = x ^ y )

Operator Prec edence
O p e ra t o r p re c e d e n c e d e t e r m i n e s t h e o r d e r i n w h i c h e x p r e s s i on s a r e e v a l u a t e d .
T h is , in s o m e c a s e s , c a n d e t e r m i n e t h e o v e r a l l v a l u e o f t h e e x p r e s s i o n . F o r
e x a mp l e , t a k e t h e f ol l o w i n g e x p re s s i on :
y=6+4/2

12

Page

Java/J2EE

06CS753

D e p e n d i n g on w h e t h e r t h e 6 + 4 e x p r e s s i o n o r t h e 4 / 2 e x p re s s i o n i s e v a l u a t e d
fi rst, the v a l u e o f y c a n e n d u p be i n g 5 o r 8 . I n g e n e r a l , i n c r e m e n t a n d d e c re m e n t
a r e e va l u a t e d be f o re a ri t h me t i c , a r i t h me t i c e x p re s s i on s a r e e v a l u a t e d be f o r e
c o mp a ri s on s , a n d c o mp a ri s on s a re e v a l u a t e d be f o re l og i c a l e x p re s s i on s .
A s s i gn m e n t e x p r e s s i on s a re e va l u a t e d l a s t .
4 Arrays

A r r a y s i n J a v a a re a c t u a l o b j e c t s t h a t c a n b e p a s s e d a r o u n d a n d t r e a t e d ju s t l i k e
oth e r o bj e c t s .
A r r a y s a r e a w a y t o s t o re a l i s t o f i t e m s . E a c h s l ot o f t h e a r r a y h ol d s a n
i n d i v i d u a l e l e m e n t , a n d y o u c a n p l a c e e l e m e n t s i n t o o r c h a n g e t h e c on t e n t s o r
t h os e s l ot s a s y o u n e e d t o .
T h r e e s t e p s t o c re a t e a n a r r a y:
1.

D e c l a r e a v a ri a bl e t o h ol d t h e a r ra y.

2.

C r e a t e a n e w a r r a y ob j e c t a n d a s s i gn i t t o th e a r r a y v a ri a bl e .

3.

S t o r e t h i n gs i n t h a t a rr a y.

E . g.
Strin g[] n ames ;
names = new String[10];
n a m es [ 1 ] = “ n 1 ” ;
n a m e s [ 2 ] = ‘n 2’;
...
4. 1 Multidimens ional Arrays
J a v a d o e s n o t s u p p or t m u l t i d i m e n s i on a l a r r a y s . H o w e ve r, y o u c a n d e c l a re a n d
c r ea t e an a r r a y o f a r r a y s ( a n d t h o s e a r r a y s c a n c o n t a i n a r r a y s , a n d s o o n , f o r
h o we v e r ma n y d i me n s i on s y ou n e e d ), a n d a c c e s s t h e m a s yo u w ou l d C -s t yl e
m u l t i d i me n s i on a l a r ra y s :
in t c oo rds [ ] [ ] = n ew in t [12 ] [1 2];
c o or ds [ 0] [0 ] = 1; c oo rds [0 ] [1 ] = 2;
5 Control Statement
13

Page

Java/J2EE

5. 1 if Conditionals

06CS753

x T h e i f c on d i t i on a l , wh i c h e n a bl e s y ou t o e x e c u t e d i f fe r e n t bi t s of c o d e b a s e d
o n a s i mp le t es t in J av a, is n ea r l y i den t ic al t o i f s t at e men t s in C.
x i f c on d i t i on a l s c on t a i n t h e k e y w o rd i f, f o l l o we d b y a b oo l e a n t e s t ,
f o l l o w e d b y a s t a t e me n t ( o ft e n a bl oc k s t a t e m e n t ) t o e x e c u t e i f t he t e s t i s t ru e :
x i f (x < y )
S y s t e m. ou t . p r i n t l n (" x i s s m a l l e r t h a n y " );
A n op t i on a l e l s e k e yw o r d p r o vi d e s t h e s t a t e m e n t t o e x e c u t e i f th e t e s t i s f a l s e :
i f (x < y )
S y s t e m . ou t . p r i n t l n ( " x i s s m a l l e r t h a n y " ) ; e l s e
S y s t e m. ou t . p r i n t l n (" y i s b i g g e r" );

5. 2 The Conditional Operator
A n a l t e rn a t i v e t o u s i n g t h e i f a n d e l s e k e y w o r d s i n a c o n d i t i on a l s t a t e m e n t i s t o
us e the c on d i t i on a l op e r a t o r, s o me t i m e s c a l l e d t h e t e rn a r y op e ra t o r.
T h e co nd i t i o na l o p e r a t o r i s a t e r na r y o p e r a t o r b e c a u s e i t h a s t h re e te r m s .
S yn t a x : t es t ? t ru e re s u l t : fa l s e re s u l t
T h e t e s t i s a n e x p re s s i on t h a t r e t u rn s t ru e o r f a l s e , j u s t l i k e t h e t e s t i n t h e i f
s t a t e me n t . I f t h e t e s t i s t r u e , t h e c o n d i t i o n a l o p e r a t o r r e t u r n s t h e v a l u e o f
t r u e r e s u l t ; i f i t ' s f a l s e , i t r e t u r n s t h e v a l u e o f f a l s e r e s u l t . F o r e x a mp le , t h e f o l l o w in g
c on d i t i on a l t e s t s t h e va l u e s o f x a n d y, r e t u r n s t h e s m a l l e r o f t h e t w o , a n d a s s i g n s
t h a t va lu e t o t h e v a ria b le s ma ll e r:
in t s malle r = x < y ? x : y;
T h e c on d i t i on a l op e ra t o r h a s a v e r y l o w p r e c e d e n c e ; t h a t i s , i t ' s u s u a l l y e va l u a t e d
o n l y a f t e r a l l i t s s u b e x p re s s i on s a r e e va l u a t e d . T h e on l y op e r a t o r s l o w e r i n
p r ec e den c e a r e t h e a s s i g n m e n t o p e r a t o r s . .
5. 3 switc h Conditionals
T h i s i s t h e s w i t c h o r ca s e s t a t e m e n t ; i n Ja v a i t ' s s wi t c h a n d be h a ve s a s i t d oe s i n C :

14

Page


Related documents


PDF Document javaunit1
PDF Document javasyllabus
PDF Document object oriented programming
PDF Document php training in jalandhar
PDF Document core php training in noida 1
PDF Document c


Related keywords