Upload
bajagovindam
View
219
Download
0
Embed Size (px)
Citation preview
7/25/2019 Unit-I JAVA
http://slidepdf.com/reader/full/unit-i-java 1/10
SREE VENKATESWARA COLLEGEOF ENGINEERING
N.RAJUPALEM
NELLORE
JAVA PROGRAMMINGReference Material
Unit-I
COMPILE !"#- . ARUNPRASA
ASST. PROFESSOR$ CSE EPARTMENT$
SVCN$KOAVALURU.
7/25/2019 Unit-I JAVA
http://slidepdf.com/reader/full/unit-i-java 2/10
Unit - I
INTRODUCTION
Java is an object oriented programming language developed by James Gosling & his crew.
Previously it was called OAK but it’s renamed to java during !!".
#any versions evolved over the years now the current version is java J$K %.
KEY ATTRIBUTES OF OOP
n computer science' computer languages adopt particular programming methodology to write
programs.
(or e)ample' * language uses structured programming' Assembly language uses non structured
whereas java uses object oriented programming.
All java programs are written using object oriented programming concepts.
+he oop concepts or ,ey attributes o- oop are encapsulation, inheritance, an pol!"orphis"#
Encapsulation$
ncapsulation is the mechanism o- hiding the internal details and allowing a simple inter-ace
which ensures that the object can be used without having to ,now its internal details.
E%a"ple/ A swipe machine encapsulates0hides internal circuitry -rom all users and provides
simple inter-ace -or access by every user.
n java' encapsulation is achieved by binding data and methods in a single entity called class
and hiding data behind methods and allowing the inter-ace with the public methods o- class.
+he -ollowing program demonstrates the concept o- encapsulation.
n the above program data and methods are encapsulated in a class called *ustomer and data
is hidden -rom user by declaring them to private and inter-ace is allowed to setter and getter
methods.
Inheritance$
nheritance is a mechanism by which we can de-ine generali1ed characteristics and behavior
and also create speciali1ed ones. +he speciali1ed ones automatically inherit all the propertieso- generali1ed ones.
7/25/2019 Unit-I JAVA
http://slidepdf.com/reader/full/unit-i-java 3/10
nheritance can be achieved in java with the e)tends ,eyword.
(or e)ample in the above diagram' 2ehicle is a generali1ed class with its own characteristics
and behavior' and two3wheeler' -our3wheeler classes are speciali1ed ones which inherit all the
properties o- generali1ed ones li,e $' 4ame' 5icense4umber.
Pol!"orphis"$
+he ability o- an object0operation to behave di--erently in di--erent situations is called
polymorphism.
n the above program the same move67 operation is
behaving di--erently in di--erent situations.
SI&P'E PRO(RA&
7/25/2019 Unit-I JAVA
http://slidepdf.com/reader/full/unit-i-java 4/10
+he process o- getting the output -or above program re8uires 9steps.
nter the source code in either editor li,e notepad or $ li,e netbeans or eclipse and save the
program name as De"o#)a*a
*ompile the program using )a*ac co""an -ollowed by $emo.java in command prompt
which generates .class -ile.
)ecute the program using )a*a command -ollowed by $emo which will generates the output.
Description a+out each part o a pro-ra"$
+he -irst line import the classes re8uired -or the program. +he classes re8uired will be in the
subdirectory lang o- main directory java. :ince the above program re8uires print67 method o-
system class the pac,age has to be imported into the program.
+he statements written between 0; ;0 are called multiline comments which will be ignored by
compiler. t e)plains the operation o- program to anyone reading the program.
:ince java is oop language' every java program is written within a class. :o we use class
,eyword -ollowed by class name $emo.
A class code starts with a < and ends with =. n between < = we can write variables and
methods.
main67 is the method which is the starting point -or J2# to start e)ecution.
:ince J2# calls the main67 method there won’t be nothing -or the main67 to return to J2#.
:o it is declared as *oi type.
:ince main67 method should be available to J2# which is an outside program it should be
declared with an access speci-ier pu+lic. Otherwise it is not accessible to J2#.
+he ,eyword static allows main67 to be e)ecuted without creating an object -or class $emo.
+his is necessary because main67 is e)ecuted by J2# be-ore any objects are made.
+o print something to the console output we use :ystem.out.print67 method. :ystem is a class
and out is a variable in it. Print67 method belongs to Print:tream class.
>hen we call out variable Print:tream class object will be created internally. :o we write:ystem.out.print67.
+o print a string to the output we use ? @and passed as a parameter to print67 method.
Ele"ents o a .a*a Pro-ra"
7/25/2019 Unit-I JAVA
http://slidepdf.com/reader/full/unit-i-java 5/10
/hitespaces$ n Java' whitespace is a space' tab' or newline.>e use one whitespace
character between each to,en in java.
IDENTIFIERS$ denti-iers are the names given to a variable' class and method. +his helps
to re-er to that item -rom any place in the program. >e can’t start an identi-ier with a digit.
)/ int ) here ) is an identi-ier -or a variable.
'iterals$ A constant value in Java is created by using a literal representation o- it. (or
e)ample' here are some literals/
BB !%.C DE’ ?+his is a test@ Co""ents$ +here are three types o- comments de-ined by Java. :ingle3line' multiline'
documentation comment. $ocumentation comment is used to produce an F+#5 -ile that
documents the program. +he documentation comment begins with a 011 and ends with a 10.
Separators$ n Java' there are a -ew characters that are used as separators. +he separators are
shown in the -ollowing table/
S!"+o
l
Na"e Description
6 7 Parenthesis *ontains parameters in method call &
de-inition. *ontains e)pressions in controlstatements. :urrounds cast type.
< = races $e-ine bloc, o- code -or class' method and
local scopes. Hsed to initiali1e arrays.
I rac,ets Hsed to declare arrays.
:emicolon Hsed to terminate statements.
' *omma :eparates identi-iers in variable declaration.
*hains statements together in -or statement.
. Period :eparates pac,age names -rom sub pac,ages.
:eparate variable or method -rom re-.
variable.
KEY/ORDS$ +here are "B reserved ,eywords currently de-ined in the Java language. +hese
,eywords' combined with the synta) o- the operators and separators' -orm the de-inition o- the
Java language. +hese ,eywords cannot be used as names -or a variable' class' or method.
n addition to the ,eywords' java reserves true, alse and null. *onst and goto ,eywords are
reserved and meant -or -uture use.
7/25/2019 Unit-I JAVA
http://slidepdf.com/reader/full/unit-i-java 6/10
OPERATORS
An operator is a symbol which tells the compiler to per-orm a particular operation on
operands. )/ a b. a and b are operands D’ is an operator meant -or addition operation. Cate-ories o operators$
Arithmetic operators
Assignment operator
Hnary operators
Lelational operators
5ogical operators or short circuit operators
oolean operators
itwise operators
+ernary or conditional operator
#ember operator
nstanceo- operator
4ew operator
*ast operator.
Arith"etic operators$
Operator Operation e%a"ple
2 Addition' string
concatenation
MB
:tring sMN@one@'s9N@two@
:tring sNsMs9
3 :ubtraction nt )N'yN9'1 1Ny3)
1 #ultiplication nt )N'yN9'1 1Ny;)
0 $ivision nt )N'yN9'1 1Ny0)
4 #odulus operator
6gives remainder7
nt )N'yN9'1 1Ny)
Assi-n"ent operator$
+his is used to store a value or variable or an e)pression into a variable.
)/ int )N9' yN"' 1
1N ) 1N)09y
Unar! operators$
Operator Operation E%a"ple
3 Hnary minus6negates givenvalue7.
nt )N":ystem.out.print63)77
ncrements a variable value
by .
nt )N"
:ystem.out.print6)7
33 decrements a variable value
by .
nt )N"
:ystem.out.print6)337
Preincrement operator allows incrementation -irst and then other operations are per-ormed.
)/ int )NB
:ystem.out.print6)700 gives result because incrementation -irst happens & then
print operation happens.
Postincrement operator allows other operations to be per-ormed -irst and then incrementationhappens.
)/ int )NB
:ystem.out.print6)700 gives result B because print operation happens
7/25/2019 Unit-I JAVA
http://slidepdf.com/reader/full/unit-i-java 7/10
-irst incrementation then happens.
Predecrement operator allows decrementation -irst and then other operations are
per-ormed.
)/ int )NB
:ystem.out.print633)700 gives result ! because decrementation -irst happens & then
print operation happens.
Postdecrement operator allows other operations to be per-ormed -irst and then decrementationhappens.
)/ int )NB
:ystem.out.print6)33700 gives result B because print operation happens
-irst decrementation then happens.
Relational Operators$
+hese are used to per-orm comparison between elements.
Operator Operation e%a"ple
Q6greater than7 valuates to true i-
element greater than
other. Otherwise -alse
:ystem.out.print6MQ97
QN6greater than or e8ual to7 valuates to true i-
element greater than or
e8ual to other. Otherwise
-alse
:ystem.out.print6MQN97
R6lesser than7 valuates to true i-
element lesser than other
element. Otherwise -alse.
:ystem.out.print6MR97
RN6lesser than or e8ual to7 valuates to true i-
element lesser than or
e8ual to other element.otherwise -alse.
:ystem.out.print6MRN97
NN6e8ual to7 valuates to true i- both
elements are e8ual.
otherwise -alse.
:ystem.out.print6MNN97
SN 6not e8ual to7 valuates to true i- both
elements are e8ual.
otherwise -alse.
:ystem.out.print6MSN97
'o-ical Operators$
+his is used to construct compound conditions.
Operator Operation E%a"ple
&&6and operator7 valuates to true i- each simple
condition evaluates to true
otherwise -alse return.
:ystem.out.print6aQb&&
bRc7
TT6or operator7 valuates to true i- any one o-
simple conditions evaluates to
true otherwise -alse return.
:ystem.out.print6aQbTTbRc7
S6not operator7 valuates to true i- condition
evaluates to -alse otherwise true
returns.
:ystem.out.print6S6aQb77
Boolean operators$
7/25/2019 Unit-I JAVA
http://slidepdf.com/reader/full/unit-i-java 8/10
+hese operators act on oolean variables and produce oolean type result.
Operators Operation )ample6aNtrue bN-alse7
&6oolean and operator7 Leturns true i- both variables
true. Otherwise -alse returns.
:ystem.out.print6a&b700returns
-alse
T6oolean or operator7 Leturns true i- any variable
is true. Otherwise -alse
returns.
:ystem.out.print6aTb700returns
true
S6oolean not operator7 *onverts true to -alse and
vice3versa.
:ystem.out.print6S6aTb7700returns
-alse
Bit5ise operators$
+hese operators act on individual bits o- a number and produce the appropriate result.
Operators Operation )ample )NB' yN
U6itwise *omplement Operator7 Gives complement o-
a given number
6U)7N3
& 6itwise and Operator 7 Gives i- both bits
are . Otherwise -alse
returns
:ystem.out.print6)&y700returns
BBBBBB
T6itwise or Operator7 Gives i- any o- the
bits are . Otherwise
-alse returns.
:ystem.out.print6)Ty700returns
BBBBBB
V6itwise )or Operator7 Gives i- odd
number o- s present
in input. Otherwise
-alse returns.
:ystem.out.print6)Vy700returns
BBBBBBB
RR6itwise le-t shi-t operator7 :hi-ts bits to the le-twith the speci-ied
number o- times.
:ystem.out.print6)RRM700returnsBBBBBB
QQ6itwise right shi-t operator7 :hi-ts bits to the right
with the speci-ied
number o- times.
:ystem.out.print6)QQM700returns
BBBBBBB
QQQ6itwise 1ero -ill right shi-t
operator7
:hi-ts bits to the right
with the speci-ied
number o- times and
-ill le-t side empty
places with 1eroes. Ternar! Operator or Conitional Operator
+his acts an an alternative -or i- else statement.
:ynta)/ variable Ne)pressionW e)pressionM/e)pression9
)ample/ ma) N 6aQb7W a /b
- e)p evaluates to true e)pM will be e)ecuted. Otherwise e)p9 will be e)ecuted.
&e"+er operator 6#7
+his is used in 9ways.
Hsed when re-er to subpac,age and class o- a pac,age.
)ample/ import java).swing.JOptionPane
Hsed when re-er to variable o- a class or object.
)ample/ :ystem.out
Hsed when re-er to method o- a class or object.
)ample/ #ath.s8rt6M97
7/25/2019 Unit-I JAVA
http://slidepdf.com/reader/full/unit-i-java 9/10
instanceo Operator
+his is used to test i- an object belongs to a 6class or inter-ace7 or not.
:ynta)/ oolean variableN object instanceo- class
)ample/ oolean )N custobj instanceo- customer
Ne5 operator
new operator is o-ten used to create objects to classes.
:ynta)/ classname objNnew classname67
)ample/ customer custobjNnew customer67 Cast operator
*ast operator is used to convert one data type into another data type.
:ynta)/ datatype target3varN6target3datatype7variable
)ample/ int )
-loat yNM."CX%
)N6int7y
Priorit! o operators
n an e)pression some o- the operators will e)ecute -irst and some operators will e)ecute ne)t.
+o determine which operators e)ecute -irst priority will be assigned to operators.
+he priority is as -ollows.
priorit! operators
st 67 ' I
Mnd ' 33
9rd ;' 0'
th ' 3
"th Lelational operators
Cth oolean and bitwise
operators
Xth 5ogical operators
%th +ernary operator
!th Assignment operator
DATATYPES
$atatype is an identi-ied type -or any data being used in a program.
+his is used to determine the -ollowing.
o +ype o- data stored in a variable
o 4o o- bytes it occupies in memory
o Lange o- data.
n java datatypes are divided into M categories.
• Primitive datatypes
• 4on primitive datatypes
Hnder primitive datatypes we have % datatypes. Hnder non primitive we have class, arra!,
interace#
7/25/2019 Unit-I JAVA
http://slidepdf.com/reader/full/unit-i-java 10/10
+he -ollowing table shows various datatypes with their details.
*ategory $atatype 4o.o- bytes
occupied
#in3ma) value $e-ault
value
e)ample
nteger type
byte byte 63MX73 6MX37 B byte bNB
short Mbytes 63M"73 6M"37 B short sNM9"
int bytes 63M973 6M937 B int iNM9"CCC
long %bytes 63MC973 6MC937 B long lN9M"CX%!
(loating type-loat bytes 69.e3B9%73 69.eB9%7 B.B- (loat -N9."-
double %bytes 69.e39B%73 69.e9B%7 B.Bd double dN9.M9d
+e)tual type char Mbytes 3 C""9" 4ull char chN’)’
5ogical type boolean bit 3 -alse boolean bNtrue
• (loat can represent upto X digits accurately a-ter decimal point' whereas double can represent
upto " digits accurately a-ter decimal point.
• char datatype can support C""9C characters including all human language characters which isthere in the Hnicode system.
8ARIAB'E
A variable is an identi-ier -or the data in the program.
t holds data in a program
t is named location in memory whose value changes during program e)ecution.
+he type o- data stored in the variables can be speci-ied with datatype.
S!nta%$ datatype variable3nameNvalue
E%a"ple$ int )NM9"C