>("' "78/4'9"+ "%8 :;"1'(<""='G@$'1(EC/6>'?'.'@' RSB6:(1/+G:52@$ &,n#,1?E4(7/='DH'6F %B9I29">'?'.'@' I"A5C2/0M#<++-78"l'" <#C2/0M#H'6 %B 1'( :(1')<#(%$19I2='&,B61='4"5H'D,@<:( "A-."E<#D,@5='"A"1'(:(> DH'6F @AL%1'( <:#6I"A5C2/0M# I"A5C2/0M#<++/2'6/A6 <#<""='='&,B6 9%B I29"1'((,+C2/0M#C2'<#<&56W# RSB6"78/4'9"+ "%8.:;"1'(<""='1'(C%$"!:(<1(0+78/6D2"-7B/1='4"5H'C2/0M#DH'6F <# <&56W##,-LEC/61'(:(0@#W#
?+'@0 A ='4(7/C2/@'0 %B&'0'(*C%$"9"!:(<1(0>'?'.'@'.D2/6:;"='4(7/C2/@'09" (M:<++95(M:<++4"SB6C/6:(> DH'6F4#H'"%8 x /00K"DEX x V x %$E@A(E5w V x &,n#,1?E<$1=' x IH/6@H'6X x C2/0M#H'6 %B
* /00K"DE7/C2/@'0 <%B (1/$MH>'$9"!:(<1(0RS6B /0G-#/(E.G0H<:#C2/@'0"%8 942:;"&H@"4"SB6C/6!:(<1(0 1#H'@7/C2/@'0"%.8 G0H0%W#DH/1'( ='6'"C/6!:(<1(0 /00K"DEC%$"G@2-7B//LA+'$!:(<1(0 -7B/942WM2/H'"C2'9.!:(<1(06H'$$A6B CS8" <#IH@$ ='942 1'(<12GC<#:(,+:(36!:(<1(0:;"G:G526H'$CS8"
>'?'.'@'1='4"5(M:<++C/61'(C%$"/00K"DEG@2&'0(M:<++5,6"%8 PQ /00K"DE&='4(,+C2/@'0+(( 5, 5%$@ .9I2(7B/640'$ "='4"2'C2/@'0 %B D2/61'(C%$"RSB6./$MH&@H "G4"C/6+(( ,51KG52 Q /00K"DE&='4(,+C2/@'0D,86
Uka .(A0B D2"52@$(7B/640'$ <#&A8"&3552@$(7B/640'$ /00K"DE (M:<++"%&8 '0'(*&(2'6:;"GJ#E/1&'(G52!5$9I2!:(<1(0javadoc.exe
("' $! <&561'(C%$"/00K"DE9"(M:<++DH'6F
/* This program is to show how to write comments */ public class CommentDemo { // Main method public static void main(String args[]) { /** This is a comment for documentation */ System.out.println("Document"); } }
YWJICAPAJB V 7/I7B/ %BD,86CS8"9">'?'.'@'RSB6/'.:;"I7B/C/6#'&I7/B C/6D,@<:(I7B/C/6 0L/54(7/I7B/C/6H'6 %B RSB6.D2/6:;"G:D'011'(D,86I7B/5,6"%8 x V .D2/6CS8"D2"52@$/,1C(Nq`q`4(7/ H'","8 x V %B:(1/+G:52@$D,@/,1C(0'11@H'4"SB6D,@ D,@/,1C(4#,6.'1D,@<(1 ",8".D2/6:;"D,@/,1C(C2'6D2"4(7/:;"D,@#Cm*S6s H'",8" x V .D2/6G0HD(61,+%$E @A(E5 V 9">'?'.'@'.*7/@H'D,@/,1?(-A0-E94nH<#D,@/,1?(-A0-E#K1DH'61," X5,6",8"V %BI7B/ myVariable.
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
x MyVariable x _MyVariable x $x x This_is_also_a_variable D,@/$H'6C/6V %BG0H*M1D2/6 x x x x x
My Variable 9pns a+c
Hello’World public
'') + B'(0+ ? <"@ '6:ZA+D, A %B9I29"1'(D,86I7B/.0%C2/1='4"55,6"%8 x 1'(D,86I7B/#'&
–
.CS8"D2"52@$D,@/,1?(-A0-E94nH<#2@D'052@$D,@/,1?(-A0-E#K1 4(7/D,@#C !5$.9I2D,@/,1?(-A0-E94nH&='4(,+/,1?("='C/6
– –
@(:;"='"'0 D,@/$H'6IH"
Sample,
HelloWorld,
Student
4(7/
GraduateStudent:;"D2"
x 1'(D,86I7B/D,@<:(
–
.CS8"D2"52@$D,@/,1?(-A0-E#K1 /,1?("='C/6
– –
@(:;"='"'04(7/:;"I7B/&,8"F D,@/$H'6IH"x,
id, name
!5$.9I2D,@/,1?(-A0-E94nH&='4(,+
4(7/thesisTitle:;"D2"
x 1'(D,86I7B/0L/5
–
.9I24#,11'(5%$@1,+1'(D,68 I7B/D,@<:(
–
D,@/$H'6IH"getName,
setName4(7/ showDetails:;"D2"
x 1'(D,86I7B/H'6 %B
–
.9I2D@, /,1?(-A0-E94nH ,86405 V X
– –
@(:;"='"'0 D,@/$H'6IH"
<#.<$1='!5$9I2(7B/640'$
MINIMUM, MIN_GPA :;"D2"
" +0 %$E@A(E57/I7B/ 0%B %@'040'$-A)?9">'?'.'@' /0G-#/(EC/6>'?'.'@'. C2'9.@'040'$<#='&,6B .%B D2/65='"A"1'(&='4(,+%$E @A(E5'?'.'@'G521='4"5 %$E@A(E5DH'6FG@25,6<&569"D'('6 %BQP $! %$E @A(E5 %B9I29">'?'.'@' abstract
continue
for
new
switch
assert
default
goto
package
synchronied
boolean
do
if
private
this
break
double
implements
protected
throw
byte
else
import
public
throws
case
enum
instanceof
return
transient
catch
extends
int
short
try
char
final
interface
static
void
class
finally
long
strictfp
volatile
const
float
native
super
while
%$E@A(E54#H'"%8G0H&'0'(*."='0'D,86:;"V G52 RSB6.'1%$E@A(E5C2'6D2".4K" @H'%$E @A(E5 31D,@.:;"D,@/,1?(-A0-E#K1<#.0%$% E @A(E5 goto<# const:;"%$E@A(E5 %B G0HG52D(61,+='&,B6959">'?'.'@'&H@"='@H' true<# falseG0HG52:;"%$E @A(E59">'?'
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
.'@'
null
RSB6:;"C2/0M#H'6 %BC/6
& #'@"+' A >'?'.'@'0%&n , #,1?E<$1='DH'6F &%B '0'(*"='G:9I2C%$"9"!:(<1(0G525,6D'('6 %BQ
$!$4"2' %BC/6(7B/640'$DH'6F 9%B I29">'?'.'@'
#'@"+' A
&
;
/' /0 A 1C+D
EF
A&1+ G0& A CD)1if,for H
I
A&"+'"& A D
!
D)D'/ /@#'@& G0* &D)D '/" '*
JK
/#*' A A 1C + 1+D#*')1 A + 1+D # +D G0 &+ 1+D)/0 A ( / 1C )1 if,while & for H (0+'#*' L M'?0
- )11 !:(<1(0>'?'.'@'&'0'(* %B.0%IH/6@H'6-7B/<$1=' :(!$ 4(7/='&,B6DH'6F >'$9"!:(<1(0G52 !5$IH/6@H'6.0%C"'5 H'G(1KG52 ,86"%8/0G-#/(EC/6>'?'.'@'.G0H "='&H@" %B :;"IH/6@H'60'1%$B @C2/61,+C"'5C/6!:(<1(0G+ !E 25 IH/6@H'6.IH@$ ='942(M:<++ C/6!:(<1(0R/(E5!255M6H'$CS8"RSB6(M:<++C/6IH/6@H'6:(1/+52@$ x IH/6@H'615%$E\X+"%$E+/(E5
x < K:15%$Ed+"%$E+/(E5 x 1'(CS8"+(( ,5940H15%$E} +"%$E+/(E5
4 # 1 C2/0M#H'6 %B7/=' 9%B I2<&56C2/0M# %B :;"D,@#C D,@/,1C( C2/@'0 4(7/H' '6 D((1RSB69">'?'.'@'G521='4"5C2/0M#H'6 %BG@2t:(> 5,6"%8 PQ D((1d Q D,@/,1C(X X cQ D,@#C.='"@"DK0 uQ D,@#C )"A$0 tQ C2/@'0 RSB6(M:<++C/61'(C%$"C2/0M#H'6 %B<#:(> C/6I"A5C2/0M#.1#H'@*S69"4,@C2/ DH/G:
)0 #"> >'?'.'@':;">'?' %BD2/6(+3I"A5C2/0M#/$H'6I,5." V 1#H'@7/C2/0M# %B :;"D,@<:(4(7/H'6 %B 31D,@ :%B ('1Z/$M9H "!:(<1(0 .D2/60%1'(:(1') <#.D2/6(+3I"A5C2/0M#52@$&0/ !5$I"A5C2/0M#9">'?'.'@'<+H6:;"&/6:(> 94nHF 7/ PQ I"A5C2/0M#<++-78"l'" V Q I"A5C2/0M#<++/2'6/A6 XV I"A5C2/0M#<++-78"l'"7/I"A5C2/0M# %B1='4"5G@29">'?'.'@' RSB6>'?'.'@' 1='4"5G@2 zI"A57/ boolean, char, byte, short, int, long, float <#double5,6<&569"D'('6 %BQcRSB6 ,86zI"A5&'0'(*<+H6//1G52:;"u:(> 7/ PQ I"A5C2/0M#D((1X7/I"A5boolean Q I"A5C2/0M#/,1C(b 7/I"A5char cQ I"A5C2/0M#D,@#C.='"@"DK0 7/I"A5 byte,short,int <# long
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
uQ I"A5C2/0M#D,@#C )"A$07/I"A5float<#double $!%C"'5<#IH@6H'C/6I"A5C2/0M#<++-78"l'"C/6>'?'.'@' )0 # 0EF
)1 1
& +&/
&false
boolean
true
char
.
NOP6666QLRNOPSSSSQ
byte
4
[ $4LR\ $3
short
.
[%$I3.4LR\%$I3.3
int
%$
[$% LR\$% [
long
.,
[$.%LR\$.%[
float
%$
[%!,6]\%4LR\%!,6]\%4
double
.,
[ !46]\%64LR\ !46]\%64
#'" TUVWXYZ
=]]]3-,^VU_`ZabZWV^VXU c`X9dVU_aXVUd! =]]]3-,YXPe`ZabZWV^VXU c`X9dVU_aXVUd!
I"A5C2/0M#<++/2'6/A67/I"A5C2/0M#/7B"F G%B 0H9IHI"A5C2/0M#<++-78"l'"I"A5C2/0M# 6,8 &/6:(> .0%@'0
)0 #' 9">'?'.'@'I"A5C2/0M# boolean 7/I"A5C2/0M#D((1!5$C2/0M#I"A5D((1:;" C2/0M# %B:(1/+52@$H'&/6H'7/.(A6<# .K RSB6D(61,+ true<# falseH'6 %B4(7/D,@ <:( 0%B %I"A5C2/0M#:;" boolean .0%H':;"95H'4"SB6>'$9"&/6H'"%8 H'",8" ='@H' true <# false<02.G0H9IH$% E @A(5E 9">'?'.'@''?'.'@':;">'?' %BD2/6(+3I"A5C2/0M# /$H'6I,5."5,6",8".S6&'0'(*9I2I"A5C2/0M# boolean 9"1(% %BD2/61'(H'C2/0M#I"A5D((1
H'",8"!5$G0H/"3n'D9429I2I"A5C2/0M#/7B"F RSB61(%"%8.'?'R%4(7/ jj %B /"3n'D942<:#6C2/0M#I"A5D,@#C.='"@"DK0942:;"C2/0M#H'6 %BI"A5D((1G52 D,@/$H'6IH"='&,B6 boolean flag = true; :;"1'(:(1')D,@<:( flag 9420%I"A5C2/0M#:;" boolean !5$1='4"59420%H' :;"true
)0 #' 9">'?'.'@'I"A5C2/0M# char 7/I"A5C2/0M#D,@/,1C( !5$C2/0M#I"A5D,@/,1C(9I2 -7B/<&56D,@/,1C(4"SB6D,@RSB69">'?'.'@'.*M11K+/$M9H "(M:C/60'D(l'"YXVRSB6.0% C"'5P
+ADC2/0M#H'6 %BRSB6:;"D,@/,1C(<++YXV./$MH9"(7B/640'$!5$. CS8"D2"52@$&,n#,1?E \ <#D'052@$#Cl'"&A+41 UbVX d !5$.0%H' D,86'?'DH'6FG52 6,8 405
t`tc
D,@ D,@/$H'6IH" D,@/,1C(<++ YXV &='4(,+>'?'G $.0%H'D,86
.:;"1'(:(1')D,@<:( letter 9420%I"A5C2/0M#:;" char !5$0%H':;" \ummuPRSB60%H' H'1,+D,@/,1?(A &='4(,+(4,& N\ OO %B9I29"(++/0-A@D/(E @,B G: D,@/,1C(<++ YXV . 1='4"5H'9"IH@6 mmmm{*S6 mmgg{942&/5#2/61,+(4,&N\ OOPzD,@<(1"/1.'1"%8 ('&'0'(* %B.1='4"5C2/0M#H'6 %BI"A5D,@/,1C(!5$1='4"5D,@/,1C(>'$9"(7B/640'$ ‘ ’/' AIH"‘x’,‘1’#‘$’ D,@/$H'6IH"='&,B6 char letter = ‘A’; .:;"1'(:(1')D,@<:( letter 9420%I"A5C2/0M#:;" char !5$0%H':;" D,@/,1?(AIH"5%$@1,+='&,6B 1H/"4"2'"%8 >'?'.'@'&'0'(* %B.C%$"C2/0M#H'6 %B %B:;"/,1C(-A)?DH'6FG52 !5$9I2 &,n#,1?E "='4"2'D,@/,1?(>'?'/,61o?DH'6F/' AIH"{7//,1C(&='4(,+1'(CS8"+(( ,5 940H:;"D2"D,@/,1C(-A)? "%B A$09I2 @,B G:.:;"G:D'0D'('6 %BQu
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
$!,D,@/,1C(-A)? %B"A$09I2 @,B G: '
ZIAGNWJ
& +
[\]^
[\D<<<_^
_>G`XS>GJ
[\C^
[\D<<
a>]
[\I^
[\D<<
cJQEAIJ
[\B^
[\D<<
KJCDBI
[\\^
[\D<<-d^
_>G`XE>XH
[\^^
[\D<<%^
OAIeEJfDNCJ
[\^^^
[\D<<^
RND]EJfDNCJ
" )0 ##A* 9">'?'.'@'I"A5C2/0M# byte` short` int <# long 7/I"A5C2/0M#D,@#C .='"@"DK0 !5$C2/0M#I"A5"%8/7 C2/0M# %B :;".='"@"DK095F9" '6AD)'&D(E !5$ <%B DH# I"A5.0%C"'59"1'(1K+C2/0M#G0H H'1," ='9420I% H@6C/6C2/0M#DH'61," 5,6 %B1#H'@0'<#2@9" D'('6 %BQcRS6B !5$ ,B@G:>'?'.'@'.1='4"5942#C.='"@"DK00%I"A5C2/0M#:;"int 1'(C%$"C2/0M#H'6 %B %B :;"C2/0M#I"A5D,@#C.='"@"DK095F .G0H0%1'(9I2 (7B/640'$`X IH"Pm`mmm.D2/6C%$":;"Pmmmm!5$>'?'.'@'.&'0'(*C%$" H'6 %BC2/0M#G52&'0<++5,6"%8 PQ #Cl'"&A+7/1'(C%$"#C.='"@"DK0 ,B@G:/' A IH"qPP<#vtc
:;"D2" Q #Cl'"<:57/1'(C%$"#C.='"@"DK0 %BCS8"D2"52@$#C m <#2@D'052@$D,@#C D,86'?'.'@'1='4"5C"'5C/6"78/ &%B ='4(,+4"H@$@'0.=' <#IH@69"1'(1K+C2/0M#942 &/5#2/61,+I"A5C2/0M#",8"!5$/,D!"0,DA/' A IH"='&,6B int x = 4;
byte b = 4;
:;"1'(:(1')D,@<:( x 9420%I"A5C2/0M#:;" int 0%C"'5C/6"78/ %B9" 4"H@$@'0.=' c +AD <#D,@<:( b 9420%I"A5C2/0M#:;" byte 0%C"'5C/6"78/ 9%B " 4"H@$@'0.='z+AD*S6<02@H'D,@<:( ,86&/6.1K+H'(AB0D2":;"u407/"1,"'?'.'@'&'0'(*1='4"5C2/0M#H'6 C%B /6D,@#C.='"@"DK09420I% "A5C2/0M#:;"longG52 !5$9&HD@, /,1?(4(7/aDH/ '2 $/' A IH" x 40'$*S6#Cl'"&A+ 0%B %H' H'1,+<#0%I"A5C2/0M#:;" x mvva40'$*S6#Cl'"<:5 %B0% H' H'1,+
c<#0%I"A5C2/0M#:;" x mbeNN a40'$*S6#Cl'"&A+41 %B0%I"A5C2/0M#:;"
& )0 ##f+ C2/0M#I"A5D,@#C )"A$07/#C.='"@".(A6 d RSB69"(++/0-A@D/(E .1K+C2/0M#:(> "%8 !5$<+H6.='"@"+AD %B 1K+C2/0M#:;"&/6&H@"7/&H@" %B :;"@'0#/%$5 C/6D,@#C <#&H@" %B :;".='"@"#C$11='#,6 b !5$&H@" %B :;"@'0 #/%$5C/6D,@#C.1K+H'9"IH@6D,86'?'.'@'1K+#C.='"@".(A6D'00'D(l'" O}}} vtu RSB6.<+H6#C .='"@".(A6//1:;" X <# V d X !5$ %BD@, #C<++ X .9I2"78/ %B4"H@$@'0.='.='"@" c +AD <+H6:;"&H@" %B :;"@'0#/%$5C/6 D,@#C.='"@" u +AD <#&H@" %B:;".='"@"#C$11='#,6.='"@" z +AD 5,6<&569"(M: %B QP &H@"D,@#C<++ V d X .9I2"78/ %B4"H@$@'0.='.='"@"
u +AD <+H6:;"&H@" %B :;"@'0#/%$5C/6D,@#C.='"@"tc+AD<#&H@" %B :;".='"@"#C$11='#,6.='"@"PP+AD
$! .='"@"+ADC/6 <#b &='4(,+D,@#C<++ X
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
>'?'.'@'1='4"5I"A5C2/0M#D,@#C )"A$0G@2&/6I"A57/ float <# double !5$ I%B "A5C2/0M# float .1K+C2/0M#C"'5 c +ADD'00'D(l'"<++ X &H@"C2/0M#I"A5 double .1K+C2/0M#C"'5
u +ADD'00'D(l'"<++ V d X >'?'.'@'1='4"5C2/0M#I"A5D,@#C )"A$0942:;"#C %B0% (7B/640'$.35 )"A$0 /' AIH" cQPu 4(7/ cQm "/1.'1"%8$6, &'0'(*C%$"/$M9H "(M:<++C/6#C$11='#,6&A+ b G52!5$9I2D,@/,1?( } 4(7/ (+3.='"@" %B :;"#C$11='#,6&A+/' A IH"
Qm}c 4(7/ qv C2/0M#H'6 %BI"A5D,@#C )"A$0 !5$ ,B@G:.*M11='4"59420%I"A5C2/0M#:;" double '?'.'@'&'0'(*1='4"5C2/0M#H'6 9%B 420I % "A5C2/0M#:;" float G52 !5$9&H D,@/,1?(4(7/gDH/ 2'$/' A IH"QvPzg4(7/cQPu :;"D2" "/1.'1"%8>'?'.'@'.1='4"5C2/0M#H'6 %BI"A5D,@#C )"A$0 0%B %/,1?( 4(7/ V DH/ 2'$@H':;"C2/0M#H'6 %B 0%B I% "A5C2/0M#:;" double /' AIH" cQPu
" ""# 1 C2/0M# %B 1K+/$MH9"!:(<1(0IH"C2/0M# %B :;"3#,1?C/6//:.K3#,1?C/6 #'& <#C2/0M#9"0L/5 .<+H6:;"&/6:(> 7/D,@<:(<#H'6 %B RSB6D,@<:(7/C2/0M# %B &'0'(*:#%$B "<:#6H'G529"!:(<1(0!5$9I2='&,B61='4"5H' &H@"H'6 %B7/C2/0M# %B 1='4"5H'G52-%$6(,865%$@<#G0H&'0'(*:#%$B "<:#6H'G529"!:(<1(0 ,86D,@<:(<# H'6 .%B D2/60%1'(:(1')I7B/<#I"A5C/6C2/0M# -7B/ %B.D(%$0"78/ 9%B "4"H@$@'0.=' &='4(,+1K+C2/0M# ='&,B69"1'(:(1')D,@<:(C/6>'?'.'@'0%(:M <++5,6"%8 [modifier] dataType variableName[,variableName];
!5$ %B x
modifier
7/%$E@A(E5(+3 3 &0+,DADH'6FC/6D,@<:(IH" XX V &H@"1(% G%B 0H(+3.*7/@H':;"V
x
dataType7/I"A5C2/0M#
x variableName7/I7B/C/6D,@<:( %B :;"G:D'011'(D,68 I7B/ D,@/$H'6IH"='&,B6 int amount; :;"1'(:(1')D,@<:(amount 9420%I"A5C2/0M#:;"int double x; :;"1'(:(1')D,@<:(x9420I% "A5C2/0M#:;"double "/1.'1"%8>'?'.'@'$,6/"3n'D 942&'0'(*:(1')I7B/D,@<:( %B :;"I"A5C2/0M# 5%$@1,"G524#'$FD,@<:(>'$9"='&,B65%$@1,"D,@/$H'6IH" float price, wholeSalePrice; :;"='&,B6:(1')D,@<:( price <# wholeSalePrice 9420I% "A5C2/0M#:;" float
" A 'A&0 1 D,@<:( %B0%1'(:(1')I"A5C2/0M#<#2@&'0'(* .%B 1='4"54(7/:#%B$"<:#6H'G52!5$ 9I2='&,B61='4"5H'RSB60%(M:<++5,6"%8 variableName = expression;
!5$ %B x
variableName7/I7B/D,@<:(
7/"A-."ERSB6:;"W##,-LE %BG52.'11'(='"@C2/@'0 %B/'. :(1/+G:52@$H'6 C%B 2/0M# D,@<:( <#D,@5='"A"1'( DH'6F RSB6 "A-."E/'.:;""A-."E 9%B 42W##,-LE:;"C2/0M#H'6 I%B "A5D,@#C.='"@"DK0 D((1 D,@#C )"A$0/,1C(4(7/C2/@'0 6,8 "%8CS8"/$MH1,+I"A5C2/0M#<#='&,B6 9%B I2 D,@/$H'6DH/G:"%<8 &561'(9I2='&,6B 1='4"5H'&='4(,+D,@<:( %BG52 ='1'(:(1')I"A5 C2/0M#G@2<#2@ x
expression
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
x = 1; radius = 3.14; c = ‘a’; y = x+4*3;
='&,B61='4"5H'.&,B66'"942(7/B 6/0-A@D/(E ='6'"&/6C,8"D/"7/ ='"@4' W##,-LEC/6"A-."E<#2@1K+W##,-LE %BG52G@29"D,@<:(/' A IH"='&,B6 amount = 121+14; .0%C,8"D/"7/='"@4'W##,-LERSB6.G52H':;" Pct <#2@1K+W##,-LE %BG52#69"D,@ <:(amount ='&,B61='4"5H'.D2/60%I7B/D,@<:(/$MH '652'"R2'$07/1'(9I2='&,B6IH" 1 = x; :;"1'(9I2='&,B6 %BG0H*M1D2/6 -('H'6 9%B 5FG0H&'0'(* .%B 1K+W##,-LE %BG52.'1 "A-."EH'C/6x G52 "/1.'1"%8('&'0'(* .%B "='I7B/D,@<:( %B.1='4"5H'0':(1/+9""A-."EG52 /' A IH" x = x+1; :;"='&,B6 %B942"A-."E-AB0H'C/6D,@<:( x %B0%/$MH<#2@G:/%1P<#2@"='W##,-LE %BG521K+ G@29"D,@<:(xIH"5A0/' A IH"*2'x 0%H':;"t.1#'$:;"
('&'0'(* %B.:(1')<#1='4"5H'(AB0D2"C/6D,@<:( >'$9"='&,B65%$@1,"G52 !5$0%(:M <++='&,6B 5,6"%8 [modifier] dataType variableName = expression [,variableName = expression];
/' AIH" int
amount = 123; float price = 12.0f; double x = 4.0, y = 2.5;
!:(<1(0 %B Q :;"D,@/$H'6 %B<&561'(:(1')D,@<:(<#1'(9I2='&,B61='4"5H' C/6D,@<:( %B0I% "A5C2/0M#DH'6FRSB6!:(<1(0"%.8 942W##,-LE5,6<&569"(M: %BQ
("' $!$D,@/$H'61'(9I2='&,B61='4"5H'
public class VariableAssignDemo { public static void main(String args[]) { int x,y; boolean b1; float z = 3.414f; /* The program will not be compiled successfully if a character f is missing */ double w; x = 5; y = 4; b1 = (x > y); w = x * 3.2; = " + x + " y = " + y); System.out.println("x System.out.println("b1 = " + b1); System.out.println("z = " + z + " w = " + w); } }
$!$W##,-LE G%B 52.'11'((,"!:(<1(0 %BQ
" 1 1'(:(1')H'6 9%B ">'?'.'@' ='G52!5$1'(9&H%$E @A(E5 :(1')I7B/!5$0%(:M <++5,6"%8
final
4"2'='&,B6
[modifier] final dataType CONSTANT_NAME = expression;
!5$ %B
7/%$E@A(E5(+3 3 &0+,DADH'6FC/6H'6 %B IH"XXV
x
modifier
x
dataType7/I"A5C2/0M#C/6H'6
%B
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
x
CONSTANT_NAME
7/I7B/C/6H'6 %BRSB6!5$ @,B G:"A$09I2D,@/,1?(-A0-E94nH <#<$1='52@$(7B/640'$
x
expression
7/"A-."E %B942W##,-LE:;"I"A5C2/0M# &%B /5#2/61,+I"A5C2/0M#
C/6H'6 %B D,@/$H'6IH"='&,B6 final int
MINIMUM = 4; final double MIN_GPA = 2.00;
:;"1'(:(1')H'6 %B MINIMUM9420%I"A5C2/0M#:;" int!5$0%H':;"u<# H'6 %BMIN_GPA9420%I"A5C2/0M#:;"double!5$0%H':;"Qmm H'6 .%B 1='4"5H'G52-%$6(,865%$@ !5$.D2/60%1'(1='4"5H'1H/" %B.0%1'( (%$19I26'"RSB607B/1='4"5H'<#2@.G0H&'0'(*:#%B$"<:#6H'G52 !:(<1(0 %B Qc :;"D,@/$H'6 <%B &561'(:(1')H'6 %B<#1'(9I2H'6 %B RSB6.942 W##,-LE5,6<&569"(M: %BQc
("' $!%D,@/$H'6<&561'(:(1')H'6 %B public class ConstantDemo { public static void main(String args[]) { final int MAXIMUM = 10; final double MIN_GPA; System.out.println("Maximum is " + MAXIMUM); MIN_GPA = 2.00; System.out.println("Minimum GPA is " + MIN_GPA); MIN_GPA = 3.00; //illegal
// } }
$!%W##,-LE G%B 52.'11'((,"!:(<1(0 %BQc
"" ""# 1 D,@<:(<#H'6 %BRSB6:(1')CS"8 .&'0'(*9I26'">'$9"+#K/1='&,B6 %B:(1') H'",8"!5$>'?'.'@'<+H6D,@<:(<#H'6 %B :;"&/6:(> 7/ PQ D,@<:(4(7/H'6 %B %B :;"3#,1?C/6//:.K4(7/3#,1?C/6#'& Q D,@<:(4(7/H'6 %B %B/$M9H "+#K/1C/60L/5 %B (%$1@H'H'6 >%B '$9" X X4(7/D,@<:(>'$9"X d D,@<:(4(7/H'6 %B %B :;"3#,1?C/6//:.K4(7/3#,1?C/6#'& 7/ D,@<:(4(7/6 %B %B:(1')>'$9"+#K/1C/6#'& RS6B /$MH"/10L/5C/6#'& D,@<:(4(7/ H'6 %B:(> "%8.0%C/+CD9I26'"/$MH>'$9"#'& !5$ %B 31F0L/59"#'&&'0'(* (%$19I2G52&='4(,+D,@<:(:(> "%8.*M11='4"5H'(AB0D2"942!5$/,D!"0,DA5,6D'('6 %BQt
$!-H'(A0B D2" *%B M11='4"5942/,D!"0,DA )0 #
1
boolean
false
byte
0
short
0
int
0
long
0L
float
0.0f
double
0.0
char
‘\u0000’
#
null
&='4(,+H'6 %B4(7/D,@<:( %B/$M>H '$9"+#K/1C/60L/5 .0%C/+CD1'(9I26'"/$MH >'$9"+#K/1 H'",8" !:(<1(0 %B Qu <&56D,@/$H'6D,@<:( %B :;"3#,1?C/6//:.K<# D,@<:( %B :;"D,@<:(>'$9" !5$!:(<1(0"%08 %D@, <:(i %B :;"3#,1?C/6//:.K&H@"D,@ <:( j <# k 9"0L/5 method1() :;"D,@<:(>'$9" <#D,@<:( j 9"0L/5 method2()1K:;"D,@<:(>'$9"/%1D,@4"SB6('G0H&'0'(* %B.(%$19I2D@ , <:(k"/10L/5 method1()G52 "/1.'1"%D 8 ,@<:( j 9"0L/5 ,86&/6.*7/@H':;"D,@<:("#D,@1,"
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
("' $!,D,@/$H'6<&56C/+CDC/6D,@<:( public class VariableScopeDemo { public int i; // object variable public void method1() { int j = 4; // local variable int k = 2; // another local variable } public void method2() { int j = 0; //local variable System.out.println(i); // calling an object variable i //
System.out.println(k);
// illegal
} }
D,@<:( %B :;"3#,1?C/6//:.K.&'0'(*1K+ <#:#%B$"<:#6H' %B 1K+G@29" 4"H@$@'0.='G52 D('+95 %B//:.K$,6*M1/2'6/A69"!:(<1(0 D,@<:(>'$9".:;"D,@<:( %B 1='4"59"+#K/1C/60L/5RSB6.0%C/+CD1'(9I26'"/$MH>'$9"+#K/1 %B1='4"5CS8" H'",8"D,@ <:(:(> "%.8 *M1&(2'6CS8"07B/0%1'((%$19I26'"0L/5 D%B ,@<:(:(1')/$MH <#.*M1#+ A86 07B/&A8"&351'( ='6'"C/60L/55,6",8"9"+'6(,86.(%$1D,@<:(:(> "%8@H'D,@<:(I,B@('@ d D,@<:( %B:(1')/$M9H " C/60L/5 1K.,5/$MH9"D,@<:( :(> "%8IH"1," !:(<1(0 %B Qt <&56D,@/$H'6C/61'(:(1')<#9I26'"D,@<:(>'$9"<#D,@<:( C/6//:.K #'& ScopeExample .0%D,@<:(C/6//:.K i %B*M1&(2'6CS8"07B/0%1'( &(2'6//:.KC/6#'&<#.1K+/$MH9" U k 5,6<&569"(M: %B Qu <#07B/0%1'( (%$19I20L/5 method1()<# method2()D,@<:(>'$9" i<# j.*M1&(2'6CS8"<# *M1#+ A86G:07B///1.'10L/55,6<&569"(M: %BQu
("' $!- D,@/$H'6<&56D,@<:(>'$9"<#D,@<:(C/6//:.K public class ScopeExample { private int i=1; public void method1() { int i=4, j=5; this.i = i+j; method2(7); } public void method2(int i) { int j=8; this.i = i+j; } }------------------------------------------------------ public class TestScoping { public static void main(String args[]) { ScopeExample scope = new ScopeExample(); scope.method1(); } }
$!, 1'(1K+H'C/6D,@<:(9"4"H@$@'0.='
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
>'?'.'@'1='4"5942D2/60%1'(1='4"5H'(AB0D2"9421,+D,@<:( :%B (1')G@21H/" .%B &'0'(*(%$19I26'"G52 9"1(%C/6D,@<:(C/6//:.K4(7/D,@<:(C/6#'&>'?'.'@'. 1='4"5H'(AB0D2"942!5$/,D!"0,DA5,6D'('6 %B Qt'$9".D2/60%='&,B69" 1'(1='4"5H'(AB0D2"/6 0A|",8".1A5C2/WA5-#'59"C,8"D/"/0G-#E } D,@/$H'6IH"='&,B6 public void demoMethod(){
int x = (int)(Math.random()*10); int y,z; if(x > 5){ y = 6; } z = x+y;
}
. ='942!:(<1(0G0H&'0'(*/0G-#EWH'"G52 "7B/6.'1H' y 0%!/1'& %B.G0H*M1 1='4"5H'(AB0D2"1H/"9I26'"
& 0A' "A-."E9">'?'.'@'/'..:(1/+52@$C2/0M#H'6 %B D,@<:(4(7/H'6 %BDH'6F!5$ .0%D,@5='"A"1'(DH'6FG@2-7B/='"@4'W##,-LE %B:;"I"A5C2/0M#DH'6FD,@5='"A"1'(9">'?' .'@'<+H6//1:;"u:(> 7/ D,@5='"A"1'( '6AD)'&D(EbP0/16;81/5hB80,1=0e D,@5='"A"1'(<++&,0-,"LEb:83,1/=7,3hB80,1=0e D,@5='"A"1'( '6D((1)'&D(Eb`=^/5,3hB80,1=0e D,@5='"A"1'(<+++ADbp/1@/K8hB80,1=0e ,86"%8D,@5='"A"1'( '6AD)'&D(E<#D,@5='"A"1'(<+++AD .942W##,-LE:;"C2/0M# I"A5.='"@"DK04(7/.='"@" )"A$0 &H@"D,@5='"A"1'(<++&,0-,"LE<#D,@5='"A"1'( '6 D((1)'&D(E.942W##,-LE:;"C2/0M#I"A5D((1
& 0A' @f D,@5='"A"1'( '6AD)'&D(E&'= 4(,+>'?'.'@'.:(1/+G:52@$(7B/640'$ j` q``<#5,6<&569"D'('6 %BQ
$!.D,@5='"A"1'( '6AD)'&D(E & +
& +
+1
\
'
a+b
-
#
a-b
*
@
a*b
/
&
a/b
%
f''&
a%b
D,@5='"A"1'(.942W##,-LE:;"#C.='"@" )"A$0*2'D,@*M15='"A"1'( V D,@95D,@4"SB6:;"C2/0M#I"A5.='"@" )"A$0 &H@"1(% D%B ,@1( =' ,86&/6D,@:;"C2/0M#I"A5 .='"@"DK0D,@5='"A"1'(.942W##,-LE:;"H'.='"@"DK01#H'@7/PQm.G52W##,-LE:;"mQt &H@"P.G52W##,-LE:;"m D,@5='"A"1'( 9I21,+D,@*M15='"A"1'( %B :;"D,@#C.='"@"DK0 !5$.942W##,-LE :;"D,@#C.='"@"DK0 %B :;")?C/61'(4'(/' AIH"vc.G52W##,-LE:;"PD,@5='"A"1'( &H@"94nH.D2/60%D,@*M15='"A"1'(&/6D,@ $1@2"D,@5='"A"1'( j <# q %B/'.0%D@, *M1 5='"A"1'(D,@5%$@G52IH"c4(7/juQm:;"D2" D,@/$H'6DH/G:"%8 :;"1'(1='4"5H'!5$0%D@, 5='"A"1'(/$MH9""A-."E='&,6B int i = 34+2; double d1 = 34.0–0.2; long l = 300*30; int j = 1/2; double d2 = 1.0/2.0; byte b1 = 20%3;
0A'"+1 >'?'.'@'G521='4"5D,@5='"A"1'(<++$H/ X -7B/9I2< "D,@ 5='"A"1'( '6AD)'&D(E %BD2/61'(:#%B$"<:#6H'C/6D,@<:( '652'"R2'$C/6='&,B6 1='4"5H' /' AIH" ='&,B6 x = x+1; RSB6:;"='&,B6 %BD2/61'(-AB0H'C/6 x CS8"/%1 P &'0'(*C%$"940H!5$9I2D@, 5='"A"1'(<++$H/G5256, "%8
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
x += 1; C2/5%C/61'(C%$"='&,B6!5$9I2D@, 5='"A"1'(<++$H/7/.IH@$ ='942!:(<1(0 ='6'" G52(K@CS8">'?'.'@'0%D,@5='"A"1'(<++$H/tD,@7/j`q``<#5,6<&56 9"D'('6 %BQv $!3D,@5='"A"1'(<++$H/ & +
+1
& +
+=
x += 3;
x = x + 3;
-=
x -= 3;
x = x – 3;
*=
x *= 3;
x = x * 3;
/=
x /= 3;
x = x / 2;
%=
x %= 3;
x = x % 3;
0A' 1"##0 1 >'?'.'@'$,60%D,@5='"A"1'(<++$H//%1&/6D,@7/D,@5='"A"1'(-AB0H' X %B9I2(7B/640'$ jj <#D,@5='"A"1'(#5H' VX %B9I2 (7B/640'$qqD,@5='"A"1'( ,86&/6D,@9I29"1'(-AB0H' %#P4(7/#5H' #% P D,@5='"A"1'( ,86&/6&'0'(*9&HG@2C2'64"2'4(7/C2'64#,6D,@<:(1KG52D,@/$H'6IH" x++7/x = x+1 ++x7/x = x+1 x--7/ x = x-1 --x7/x = x-1 D,@5='"A"1'(-AB0H'<#D,@5='"A"1'(#5H'&'0'(*9I21+, D,@<:( ,86 %B0I% "A5C2/0M# :;"D,@#C.='"@"DK0<#D,@#C.='"@" )"A$0 1'(@'6(7/B 640'$G@252'"4"2'D,@<:(.0% W#942!:(<1(0 ='1'(-AB04(7/#5H'1H/"<#[email protected] ='='&,B6C/6"A-."E",8" &H@"1'(@'6 (7B/640'$G@252'"4#,6D,@<:(.0%W#942!:(<1(0 ='1'(-AB04(7/#5H'4#,6.'1 ='='&,B6C/6 "A-."E",8"
!:(<1(0 %B Q
<&56D,@/$H'6C/61'(9I2D@, 5='"A"1'(-AB0H' !5$1'(:(%$+ $% + W##,-LE %BG52.'11'(@'6D='<4"H6C/6(7B/640'$ jj G@252'"4"2'<#52'"4#,6D,@<:( RS6B W##,-LE %BG52.:;"5,6<&569"(M: %BQt ("' $!.D,@/$H'61'(9I2D,@5='"A"1'(-AB0H' class IncrementDemo { public public static void main(String args[]) { int x; int y; x = 5; y = x++; System.out.println("x = "+x+" y = "+y); y = ++x; System.out.println("x = "+x+" y = "+y); } }
$!-W##,-LE G%B 52.'11'((,"!:(<1(0 %BQ
& 0A'" G D,@5='"A"1'(<++&,0-,"LE9I29"1'(:(%$+ %$+H'C/6C2/0M#I"A595F&/6H' !5$. 942W##,-LEC/61'(:(%$+ %$+:;"C2/0M#H'6 %BI"A5D((1 >'?'.'@'1='4"5D,@5='"A"1'( <++&,0-,"LEG@2
D,@7/````<#5,6<&569"D'('6 %BQz
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
$!4<&56D,@5='"A"1'(<++&,0-,"LE & +
& +
+1
g##G
<
+'1
3<4
true
<=
+'1&1'
3<=4
true
>
''1
3>4
false
>=
''1&1'
3>=4
false
==
1'
3==4
false
!=
? 11'
3!=4
true
I"A5C2/0M# %B."='0':(%$+ %$+.D2/6:;"I"A5C2/0M# %B&/5#2/61,"/' A IH" 1'( :(%$+ %$+D,@#C1,+D,@#C D,@/,1C(1,+D,@/,1C( 4(7///:.K1,+//:.K :;"D2" D,@/$H'6IH" ‘x’ > ‘y’ 342 <= 431.50 “Test” == “\test”
D,@*M15='"A"1'( .%B "=':(%$+ %$+/'.:;"D,@<:(4(7/"A-."E1KG52
("' $!31'(9I2D,@5='"A"1'(<++&,0-,"LE
public class BooleanDemo { public static void main(String args[]) { int x = 5; int y = 4; boolean b1; b1 = (x != y); System.out.println("x not equal y is " + b1); System.out.println("y less than 0 is " + (y < 0)); } }
$!.W##,-LE G%B 52.'11'((,"!:(<1(0 %BQv
&" 0A''f D,@5='"A"1'( '6D((1)'&D(E .9I21,+D,@*M15='"A"1'( %B :;""A-."ED((1)'&D(E 4(7/I"A5C2/0M# boolean D,@5='"A"1'(:(> "%.8 942W##,-LE:;"C2/0M#H'6 I%B "A5D((1 !5$>'?'.'@'1='4"5D,@5='"A"1'( '6D((1)'&D(EG@2
D,@5,6<&569"D'('6 %BQs D,@5='"A"1'( 31D,@.D2/60%D,@*M15='"A"1'(&/6D,@ $1@2"D,@5='"A"1'( %B :;"D,@ 5='"A"1'( 9%B I29"1'(1#,+H' RSB6D2/61'(D,@*M15='"A"1'(-%$64"SB6D,@ D'('6 %B QPm *S6 D'('6 %B QPc <&56W##,-LE %B :;"C2/0M#H'6 %BI"A5D((1RSB6G52.'11'(5='"A"1'( %B:;"1'( 1#,+H'`Ni`[h <#}bX q[hH' '6D((1 D,@/$H'6C/6"A-."E 9%B I2D,@5='"A"1'( '6D((1)'&D(E0%5,6"%8 (7>6) & (2<1) .G52W##,-LE0%H':;"false (7>6) | (2<1) .G52W##,-LE0%H':;"true !(7>6) .G52W##,-LE0%H':;"false
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
$!5D,@5='"A"1'( '6D((1)'&D(E & +
& +
g
'# 1'
hh&h
bcR 1'
EE&E
iK 1'
j
MkGEDXA?JliK 1'
$! 6W##,-LEC/61'(1#,+H' '6D((1 NS
gNS
true
false
false
true
$! W##,-LEC/61'(NiH' '6D((1 NS
NS
NShNS
true
true
true
true
false
false
false
true
false
false
false
false
$! $W##,-LEC/61'([hH' '6D((1 NS
NS
NSENS
true
true
true
true
false
true
false
true
true
false
false
false
$! %W##,-LEC/61'(}bX q[hH' '6D((1 NS
NS
NSjNS
true
true
false
true
false
true
false
true
true
false
false
false
D,@5='"A"1'( %B0% (7B/640'$xx<#33(%$1@H'D,@5='"A"1'( '6D((1)'&D(E <++K6=015/052/1!5$ %B xx:;"1'(PcAH' '6D((1RS6B . 0) || (x++ < -5) ) { System.out.println(x); }
W##,-LEC/6 x .0%H':;" 9 "7B/6.'1
||
.G0H(%$1='&,B69""A-."E %B&/6
int x = 10; if ( (x > 0) | (x++ < -5) ) { System.out.println(x); }
W##,-LEC/6 x.0%H':;"
&& 0A'" D,@5='"A"1'(<+++AD:;"D,@5='"A"1'( 9%B I21+, C2/0M#I"A5D,@#C.='"@"DK0 -7B/ .,51'(1,+C2/0M#IA6+AD4(7/-7B/#7B/"+AD!5$0%(7B/640'$DH'6F5,6<&569"D'('6 %B QPu<# D'('6 %BQPt
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
$! ,D,@5='"A"1'(-7B/.,51'(1,+C2/0M#IA6+AD & +
& +
m
dNLSEJLJIC
h
bcR
E
iK
j
niK
$! -D,@5='"A"1'(-7B/#7B/"+AD & +
& +
oo
XAeIJWBAeHCXHAPC
ooo
DIXAeIJWBAeHCXHAPC
pp
EJPCXHAPC
(7B/640'$yIxI3<#z9I29"1'(.,51'(C2/0M#IA6+ADIH"z7/999 %B =' 1'( {h: 1,+ 99 .0%H':;" 9 &H@"(7B/640'$ ||I ||| <# }} :;"D,@ 5='"A"1'(-7B/#7B/"+AD!5$.9I21,+I"A5C2/0M#.='"@"DK0 %B :;" int4(7/ long!5$D,@ *M15='"A"1'( %B :;".='"@"+AD .%B #7B/"G:.:;")?C/61'(4'(52@$ <# % &='4(,+ I"A5C2/0M#int<#longD'0#='5,+5,6",8"='&,B6 int x = 8; System.out.println (x >> 32);
40'$*S61'(#7B/"+ADC/6D,@<:(x G:9+ADG0H9IH+AD (7B/640'$ || :;"1'(#7B/"+AD!5$-A.'('.'1(7B/640'$ RSB6*2'+AD '6R2'$ :;"H'1K.9&HH'< "
$||7/$~% $||7/$~$ $}}7/$
&- #A0 A 0A' 1(% %B"A-."E95F0%D@, 5='"A"1'(0'11@H'4"SB6D,@ >'?'.'@'..,5#='5,+@'0&=',n C/6D,@5='"A"1'(-7B/='"@4'W##,-LED'0#='5,+@'0&=',nC/6D,@5='"A"1'( !5$0%#='5,+ @'0&=',n5,6<&569"D'('6 %BQP
$! .#='5,+@'0&=',nC/6D,@5='"A"1'( UE&)W
)(!
EU ''
SaM(b:1=`e
vvIIvIIyIbJ,1,1]B8e
aM(S b`1=:e
I~I
aM(S b`1=:e
vI
aM(S b`1=:e
}}I||I|||
aM(S b`1=:e
}I|I}I|I/7K1,758=?
%
aM(S b`1=:e
I
#
aM(S b`1=:e
x
$
aM(S b`1=:e
z
*
aM(S b`1=:e
9
aM(S b`1=:e
xx
aM(S b`1=:e
SaM(b:1=`e
SaM(b:1=`e
IvIII~II}}I ||I|||IxIzI
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
D,@/$H'6IH"='&,B6 x = 2+3*4-(7+2); .='"@4'W##,-LED'0#='5,+5,6"%8 PQ ='"@4'W##,-LEH'vj ='942G52 x = 2+3*4-9
Q ='"@4'W##,-LEH'cu ='942G52
x = 2+12-9
cQ='"@4'W##,-LEH'jP ='942G52
x = 14-9
uQ='"@4'W##,-LEH'Puqt ='942G52
x = 5
- '"#)0 # "A-."E '6AD)'&D(E9">'?'.'@'/'.0%D,@*M15='"A"1'( '6AD)'&D(E %B0I% "A5 C2/0M#:;":(> DH'6F>'?'.'@'G521='4"5942W##,-LEC/6"A-."E:;"5,6"%8 x 9"1(% %BD,@*M15='"A"1'( ,86&/6D,@0%I"A5C2/0M#:;" double 407/"1," . =' 942G52W##,-LE %B0I% "A5C2/0M#:;"double x 9"1(% %BD,@*M15='"A"1'( ,86&/6D,@0%I"A5C2/0M#:;"float407/"1,". ='942 G52W##,-LE %B0I% "A5C2/0M#:;"float x 9"1(% %BD,@*M15='"A"1'( ,86&/60%I"A5C2/0M# D%B H'61," >'?'.'@'.0%4#,11'( <:#6I"A5C2/0M# X 5,6"%8 – *2'D,@*M15='"A"1'(D,@4"SB60%I"A5C2/0M#:;" double D,@*M15='"A"1'(/%1 D,@4"SB6.*M1<:#69420I% "A5C2/0M#:;"double!5$/,D!"0,DA – *2'D,@*M15='"A"1'( 6,8 &/6G0HG520%I"A5C2/0M#:;" double
.'14#,11'(C2'6D2".4K"G52@H'W##,-LE %BG52.'11'(='"@"A-."EAD)'&D(E .0% I"A5C2/0M#:;"int:;"/$H'6"2/$5,6",8"='&,6B DH/G:"%8 byte b1, b2, b3; b1 = 2; b2 = 4; b3 = b1+b2;
// illegal
.S6:;"='&,B6 G%B 0H*M1D2/6 "7B/6.'1 b1+b2 .942H'W##,-LE 0%B %I"A5C2/0M#:;" int RSB6G0H&'0'(*1='4"5H'9421,+D,@<:( 0%B I% "A5C2/0M#:;"byteG52
- '"# #D A 'A&0 1 >'?'.'@'1='4"5942='&,6B 1='4"5H'.D2/60%I"A5C2/0M#C/6D,@<:( '652'"R2'$<# I"A5C2/0M#C/6"A-."E '652'"C@'&/5#2/61,"/' A IH" int i = 4; double x = 3.0;
9"1(% %BD,@<:(<#"A-."E0%I"A5C2/0M# %B'?'.'@'. ='1'(<:#6I"A5C2/0M# ,86&/6I"A5942&/5#2/61,"!5$1'(<:#6I"A5C2/0M#0%&/6(M:<++7/ PQ 1'(<:#6C2/0M# %B1@2'6CS8" V X 7/1'(<:#6.'1I"A5C2/0M# %B 0%C"'5#K11@H'G::;"I"A5C2/0M# 0%B %C"'594nH1@H' Q 1'(<:#6C2/0M# %B<+#6 X 7/1'(<:#6.'1I"A5C2/0M# %B 0%C"'594nH1@H'G::;"I"A5C2/0M# %B0C% "'5#K1#6 RSB6/'.0%W#942&%$@'0 #/%$5C/6C2/0M#+'6&H@"G: >'?'.'@'1='4"5C"'5C/6I"A5C2/0M#DH'6F %B&'0'(*<:#6C2/0M#9421@2'6CS8"G52 5,6 <&569"(M: %BQvRSB60%4#,11'(5,6"%8 x I"A5C2/0M#D,@#C.='"@"DK0&'0'(*<:#6942:;"I"A5C2/0M#D,@#C )"A$0G52 x I"A5C2/0M#float&'0'(*<:#6942:;"I"A5C2/0M#doubleG52 x I"A5C2/0M#D,@#C.='"@"DK00%C"'5(%$61,".'1"2/$G:0'15,6"%8 byte o short o int o long x I"A5C2/0M#char&'0'(*<:#6942:;"I"A5C2/0M#intG52 x I"A5C2/0M#boolean.G0H0% @'0&,0-,"LE1,+I"A5C2/0M#<++-78"l'"/7B"F
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
$!31'(<:#6I"A5C2/0M# 9"='&,6B 1='4"5H' *2'W##,-LEC/6"A-."E:;"I"A5C2/0M# 0%B %C"'5#K11@H'I"A5C2/0M# C/6D,@<:( >'?'.'@'. ='1'(<:#6C2/0M#942:;"I"A5C2/0M#C/6D,@<:(5,61#H'@!5$ /,D!"0,DA/' A IH"='&,B6 int i = 4; long l = i;
"A-."Ei.*M1:(,+I"A5C2/0M#.'1int942:;"long!5$/,D!"0,DA 4(7/='&,B6 double x = 3; "A-."E %B0%H':;"c.*M1:(,+I"A5C2/0M#.'1int942:;"double!5$/,D!"0,DA 9"1(% %B='&,6B 1='4"5H'0%I"A5C2/0M#C/6D,@<:( 0%B %C"'5#K11@H' I"A5C2/0M#C/6 "A-."E >'?'.'@'.G0H&'0'(*<:#6I"A5C2/0M#C/6"A-."E942:;"C"'5 %B #K1#6 H'1,+I"A5 C2/0M#C/6D,@<:(!5$/,D!"0,DA
("' $!4!:(<1(0 0%B C% /2 WA5-#'59"1'(<:#6I"A5C2/0M#
public class PromotionDemo { public static void main(String args[]) { int i; long l; float fl = 4.2f; i = 4; l = i; fl = i; double x = fl; fl = 4.2; //illegal } }
- aqSJG>XCAIe >'?'.'@'.&'0'(* ='1'(<:#6I"A5C2/0M# 942:;"I"A5C2/0M# %B0C% "'5#K1#6G52 !5$9I2@LA %1'( %B (%$1@H' XRSB60%(M:<++5,6"%8 (targetType) expression
!5$ %B x targetType7/I"A5C2/0M# %BD2/61'( 1'(9I2 X .IH@$ ='942!:(<1(0 %B0%='&,B6RS6B .=':;"D2/6<:#6I"A5C2/0M# 9420%C"'5#K1#6&'0'(*/0G-#EWH'"G52
4(7/D,@/$H'6='&,B6 int x; double y = 1.25; x = (int)y;
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
. ='1'(<:#6"A-."E y %B0%H'PQt9420%I"A5C2/0M#:;" int %B0%H'P
class TypecastingDemo { public public static void main(String args[]) { byte b1 = 4; byte b2 = 3; byte b3; b3 = (byte) (b1 + b2); float f1; f1 = (float) 3.2; } }
4 )0 #" D,@<:(4(7/H'6 %B %B:(1'):;"I"A5C2/0M#/7B"F RSB6G0H9IHI"A5C2/0M#<++-78"l'" . :;"I"A5C2/0M#<++/2'6/A6RSB61K/7 //:.K9">'?'.'@'!5$<+H6//1:;"&/6<++7/ PQ I"A5C2/0M# %B:;"#'& Q I"A5C2/0M# %B:;"/($E D,@/$H'6C/6I"A5C2/0M# %B :;"#'&7/ I"A5C2/0M# String !5$ String G0H9IH I"A5C2/0M#<++-78"l'"
I"A5C2/0M#<++/2'6/A6.0%@LA %1'(1K+C2/0M#9"4"H@$@'0.=' %B
:;"1'(:(1')D,@<:( 0%B %I"A5C2/0M#<++-78"l'" int %BI7B/ x<#9421K+H':;"v <#:;"1'(:(1')D,@<:(I"A5C2/0M#<++/2'6/A6 RSB61K7///:.K s C/6#'& String <#9421K+H':;"C2/@'0@H' Thana RSB6D,@<:( ,86&/6D,@/'..0%1'(1K+C2/0M#9" 4"H@$@'0.='5,6<&569"(M: %BQzRSB6H' %B1K+9"D,@<:(x.:;"H'C2/0M# %B:;"H'v&H@"H' C/6D,@<:(s.:;"D='<4"H6/2'6/A69"4"H@$@'0.=' %B9I21K+C2/@'0@H'Thana
$!4D,@/$H'6C/61'(1K+C2/0M#9"4"H@$@'0.=' 1'(:(1')D,@<:( 4(7///:.K C/6I"A5C2/0M#<++/2'6/A6.:;"-%$61'( :(1')I7B/D,@<:( 4(7///:.K '?'.'@'. 1='4"5D='<4"H6/2'6/A6(AB0D2"9420%H':;" null !5$/,D!"0,DA RSB6"78/ 9%B "4"H@$@'0.='-7B/ 1K+C2/0M#DH'6F C/6D,@<:(I"A5C2/0M#<++/2'6/A6.*M1&(2'6CS"8 07B/0%1'((%$19I2='&,6B new /' AIH"='&,B6 Date d; :;"='&,B69"1'(:(1')D,@<:(4(7///:.Kd942:;"#'&I"A5Date %B1='4"5 G@29"NO<#.G52H'9"4"H@$@'0.='5,6<&569"(M: %BQs1 <#='&,B6 d = new Date(16, 8, 2002);
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
.:;"='&,6B 9"1'(./6"78/ 9%B "4"H@$@'0.='-7B/1K+3#,1?C/6//:.K %BI7B/d %B0%/$MHcD,@7/day`month<#yearRSB60%I"A5C2/0M#:;"int5,6<&569"(M: %BQsC
E'F
EF $!5D,@/$H'6C/61'(1K+C2/0M#9"4"H@$@'0.='
4 # String String :;"#'&
%B1='4"5G@29" NO D,@<:( %B0%I"A5C2/0M#:;" 1K7///:.KI"A54"SB6RSB6String0%C2/
String
:;"//:.K 0%B %'H 6 %BC2/0M#RSB61K7/C2/@'095F (7B/640'$V d “ ”D,@/$H'6IH"
String
%B/$MH>'$9"
“This is a java course”
x
String
:;"//:.K &%B '0'(**M1&(2'6CS8"<#1='4"5H'G52!5$G0H.=':;"D2/6 9I2='&,B6newD,@/$H'6IH"('&'0'(*9I2='&,B6 String s = “Thana”;
!5$G0H.=':;" %B.D2/69I2='&,B6
String s = new String(“Thana”);
9"1(% %BG0H9I2='&,6B new >'?'.'@'.1='4"5D='<4"H6/2'6/A69"4"H@$@'0.=' C/6C2/@'0 %B(+39"(7B/6 (“ ”)!5$-A.'('.'1\ @H'0%C2/@'0 5A0/$MH4(7/G0H 4'10%1K.9I2D='<4"H6/2'6/A6 %BR8='1," '?'.'@'.&(2'6C2/@'0940H<#./6"78/ %B9"4"H@$@'0.='&0/ !:(<1(0 %B QPm<&56D,@/$H'61'(&(2'6<#1='4"5H'//:.KI"A5String ,869"1(% 9%B I2<#G0H9I2='&,6B new //:.K sP <# s2 .0%C2/0M#9" 4"H@$@'0.=':;"D='<4"H6/2'6/A6 %B5%$@1,"&H@"s3.0%C2/0M#9"4"H@$@'0.=' :;"D='<4"H6/2'6/A6 %BDH'61,""7B/6.'10%1'(&(2'6C2/@'0CS8"0'940H5,6<&569"(M: %BQPm ("' $! 6:;"C/6:(1')<#&(2'6//:.KI"A5String public class StringDemo { public static void main(String args[]) { String s1 = "Thana"; String s2 = "Thana"; String s3 = new String("Thana"); } }
$! 6D,@/$H'61'(1K+C2/0M#I"A5String9"4"H@$@'0.='
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
String
:;"//:.K %B :#%B$"H'G0HG52 d dyX 1'(1='4"5H' 9421,+//:.KI"A5 String 940H :;"1'(:#%$B "D='<4"H6/2'6/A69" 4"H@$@'0.='C/6//:.K5,61#H'@ '$9"D='<4"H6 /2'6/A65A0D,@/$H'6IH"='&,B6
x
String s1; s1 = “Thanachart”; s1 = “Somchai”;
.0%W# ='942D='<4"H6/2'6/A69"4"H@$@'0.='C/6//:.Ks1:#%B$"G:5,6<&56 9"(M: %B QPP RSB6:;"D,@/$H'6C/61'(:#%B$"D='<4"H6/2'6/A69"1'(1K+C2/0M#I"A5 String9"4"H@$@'0.='
x
$! D,@/$H'61'(:#%B$"D='<4"H6/2'6/A69"1'(1K+C2/0M# String
:;"//:.K %B0D% @, 5='"A"1'( 9%B I29"1'(I7B/0C2/@'0&/6C2/@'0 C2'52@$1,"!5$9I2(7B/640'$j/' AIH" String s1 = “Hello”+“ World”;
D,@5='"A"1'(9"1'(I7B/0C2/@'0&'0'(*9I2I7B/0C2/0M#I"A5 String 1,+D,@ *M15='"A"1'( %B :;"I"A5C2/0M#/7"B F %BG0H9IHI"A5 String G52 RSB6>'?'.'@'. <:#6I"A5C2/0M#5,61#H'@942:;"I"A5String!5$/,D!"0,DA/' AIH"='&,B6
String s1 = “This”; String s2 = s1+ “ is a test ”; String s3 = s1+4;
. ='942G52C2/@'0C/6D,@<:( s2 :;" “This :;"“This4”
is a test” <# s3
D,@5='"A"1'(-7B/I7B/0C2/@'0.0%D,@5='"A"1'(<++$H/ 9%B I2(7B/640'$ j -7B/I7B/0C2/@'0 <#2@1='4"5H'9"//:.KC/6#'& String !5$9I2I7B/5A0 /' AIH"='&,B6 String s1 = “This”; s1 += “ is a test”;
:;"='&,B6I7B/0C2/@'0C/6//:.K s1 5A01,+C2/@'0 @%B H' “ is a test” <#2@1K+#69"//:.K s1 IH"5A0 !5$ ='942 s1 0%C2/@'0:;" “This is a test”
4 #Math +,-, PHQ G521='4"59420%#'& Math %B/$MH9"<-1K. java.lang RSB6.0% 0L/5DH'6F9"1'(.,51'(1,+J]61EI,"4(7/='&,B6 '6AD)'&D(EDH'6F #'& Math :;"#'& <++ final <#0L/5 13 0L/5.:;"0L/5C/6#'& b0%%$E @A(E5 static /$MH RSB6.1#H'@*S69"+ %B
e 1'((%$19I20L/54#H'"%8 ='G52!5$G0H.=':;"D2/6&(2'6//:.K
RSB61'((%$19I2 Math.E.0%H':;"#$$$$<#Math.PI.0%H':;" z
tc 0L/5/7B"F9"#'&Math %B&=',n0%5,6"%8
static int abs(int x);
static long abs(long x);
static float abs(float x);
static double abs(double x);
static double acos(double x);
static double asin(double x);
static double atan(double x);
static double atan2(double x, double y);
static double ceil(double x);
static double cos(double x);
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
static double exp(double x);
static double floor(double x);
static double log(double x);
static int max(int x, int y);
static long max(long x, long y);
static float max(float x, float y);
static double max(double x, double y);
static int min(int x, int y);
static long min(long x, long y);
static float min(float x, float y);
static double min(double x, double y);
static double pow(double x, double y);
static double random();
static double rint(double x);
static int round(float x);
static long round(double x);
static double sin(double x);
static double sqrt(double x);
static double tan(double x);
% A /"#/ !:(<1(0/0-A@D/(E!5$ @,B G: .D2/60%1'(/H'"C2/0M#C2'-7B/9I29"1'(:(0@#W# <#.0%1'("='W##,-LE %BG52.'11'(:(0@#W#//10'<&56 C+@"1'(9"1'(/H'"C2/0M#C2' <#<&56W##,-LE(%$1@H' /A"-3D/' E-3D >'?'.'@'0%@AL%1'(4#'$@AL%1'(9" 1'(.,51'(1,+/A"-3D/' E-3D <#0%#'&4#'$#'& %B 1%B$@C2/61,+C+@"1'(/A"-3D/' E-3D '?'.'@'0%//:.K %B:;"/A"-3D/' E-3D0'D(l'"&'0 //:.K7/System.in`System.out<#System.err x //:.K System.in :;"//:.K 0%B % 0L/5&='4(,+1'(/H'"C2/0M# '6/3:1(E /A"-3D0'D(l'"RSB6!5$ ,B@G:1K7/%$E+/(E5 x //:.KSystem.out :;"//:.K %B0% 0L/5&='4(,+1'(<&56C2/0M#//1 '6 /3:1(E/' E-3D0'D(l'"RSB6!5$ ,B@G:7/./>'-
x //:.K System.err :;"//:.K 0%B %0L/5&='4(,+1'(<&56C2/WA5-#'5 //1 '6/3:1(E %B9I29"1'(<&56C2/WA5-#'5RS6B !5$ @,B G:.1='4"5:;" ./>'-
%
System.out.println()
//:.K System.out 40'$*S6//:.K %BI7B/ out RSB6:;"3#,1?C/6#'& System//:.K I %B 7B/ out:;"//:.KC/6#'& PrintStream %B0%0L/5 %B1%$B @C2/6 1,+1'(<&56W#/$MH4#'$0L/5
:;"I35='&,6B 9"1'(-A0-EH'C/6D,@<:( x %B0I% "A5C2/0M#:;" int //1 '6./>'!5$!:(<1(0.<:#6I"A5C2/0M# int 942:;" String <#[email protected]/@'0//10' !:(<1(0 %B QPP <&56D,@/$H'6C/61'(9I2='&,B6 System.out.print() <# System.out.println() -7B/<&56C2/0M#I"A5DH'6F //1 '6./>'-5,6<&569"(M: %B QP ("' $! 1'(9I2='&,B6-7B/-A0-EC2/@'0DH'6F//1 '6./>'-
public class PrintDemo { public static void main(String args[]) { int x = 3, y = 4, z = 5; System.out.print("x = "+x); System.out.println(" y = "+y); System.out.println("z = "+z); } }
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
$! $W##,-LE %BG52.'11'((,"!:(<1(0 %BQPP
% ' # dNLL>IWrAIJ 0L/5 main() %B:;"D='<4"H6(A0B D2"1'( ='6'"C/6!:(<1(0.'@':($31DE .0% :;" String args[]RSB640'$*S6D,@<:(/($E .1#H'@*S69"+ %B z args %B0I% "A5C2/0M#:;" String RSB6&'0'(*(,+ %B&H6WH'"0'.'1 XV G52 !5$0%D,@/$H'6C/61'( ='6'"5,6(M: %B QPcRSB6:;"1'(1='4"5942 args[0]0%H':;" Tommy <#D,@<:( args[1] 0%H':;" boy !:(<1(0 %B QP :;"D,@/$H'61'(:/"/A"-3D '6 XV<#2@<&56W#//1 '6./>'- ("' $! $!:(<1(0<&561'(:/"/A"-3D '6XV class Sample { public public static void main(String args[]) { System.out.println("My name is "+ args[0] + " and I am a " + args[1]); } }
$! %W##,-LE %BG52.'11'((,"!:(<1(0 %BQP
/& x /00K"DE 7/C2/@'0 %B< (1/$MH>'$9"!:(<1(0 RSB6/0G-#/(E.G0H<:# C2/@'0"%8942:;"&H@"4"SB6C/6!:(<1(0 x V 7/I7B/ %BD,86CS8"9">'?'.'@'RS6B /'.:;"I7B/C/6#'&I7B/C/6D,@<:(I7B/ C/60L/54(7/I7B/C/6H'6 %BRSB6.D2/6:;"G:D'011'(D,68 I7B/ x 1'(D,86I7B/9">'?'.'@' – &='4(,+#'&"A$0CS8"D2"52@$D,@/,1?(-A0-E94nH – &='4(,+0L/5<#D,@<:("A$0CS8"D2"52@$D,@/,1?(-A0-E#K1 – *2'I7B/ D%B ,86CS8"0%0'11@H'='"A$0CS8"D2"='940H52@$D,@/,1?(-A0-E94nH – D2/6G0HD(61,+%$E @A(E5 x %$E@A(E5 7/I7B/ 0%B %@'040'$-A)?9">'?'.'@' /0G-#/(EC/6>'?'.'@'. C2'9.@'040'$<#='&,6B .%B D2/65='"A"1'(&='4(,+%$E @A(E5'?'.'@'<+H6:;"&/6:(> 7/I"A5C2/0M#<++-78"l'" <# I"A5C2/0M#<++/2'6/A6 x I"A5C2/0M#<++-78"l'" %B9I29">'?'.'@'0% 6,8 405 $ I"A57/ char, short, int, long, float, double <#boolean
byte,
x C2/0M# %B 1K+/$MH9"!:(<1(0<+H6:;"&/6:(> 7/D,@<:(<#H'6 %B RSB6D,@<:( 7/C2/0M# &%B '0'(*:#%$B "<:#6H'G529"!:(<1(0!5$9I2='&,6B 1='4"5H' &H@" H'6 %B7/C2/0M# %B1='4"5H'G52-%$6(,865%$@<#G0H&'0'(*:#%B$"<:#6H'G529" !:(<1(0 x D,@<:( %B0%1'(:(1')I"A5C2/0M#<#2@&'0'(* .%B 1='4"54(7/:#%B$"<:#6H'G52 !5$9I2='&,B61='4"5H' x 1'(:(1')H'6 9%B ">'?'.'@' ='G52!5$1'(9&H%$E@A(E5 :(1')I7B/ x H'6 %B4(7/D,@<:( %B/$M>H '$9"+#K/1C/60L/5 >'$9"+#K/1 H'",8"
final
4"2'='&,B6
.0%C/+CD1'(9I26'"/$MH
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
x D,@5='"A"1'( %B9I29">'?'.'@'0% ,86405<++7/ – D,@5='"A"1'( '6AD)'&D(EvIII~IIvIII~IIvv<# – D,@5='"A"1'( '6D((1)'&D(EIxxIxI<# – D,@5='"A"1'(<++&,0-,"LE}I}I|I|I<# – D,@5='"A"1'(<+++ADyIxIIzI||I|||I}} x 1(% %B"A-."E95F0%D@, 5='"A"1'(0'11@H'4"SB6D,@ >'?'.'@'..,5#='5,+ @'0&=',nC/6D,@5='"A"1'( -7B/='"@4'W##,-LED'0#='5,+@'0&=',nC/6 D,@5='"A"1'( x @'0'?'.'@'!5$<+H6//1:;"&/6<++7/ I"A5C2/0M# %B:;"#'&<#I"A5C2/0M# %B :;"/($E x C2/0M# %B1K+9"4"H@$@'0.='C/6I"A5C2/0M#<++/2'6/A6 .:;"D='<4"H6/2'6/A6 %B 1K+C2/0M#9"4"H@$@'0.='
String:;"I"A5C2/0M#<++#'&G0H9IHI"A5C2/0M#<++-78"l'"
x #'& Math .0%0L/5DH'6F AD)'&D(EDH'6F
9"1'(.,51'(1,+J]61EI,B"4(7/='&,B6 '6
x ='&,B6 %B9I29"1'(<&56W##,-LEC/6C2/0M#9">'?'.'@'7/='&,6B System.out.println()
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6