!"# *$% & ' &() 5++.,-./0%$%1+2'31+4&031"+' *6 *%89:1+,+ ()!8;4< 0$& 7' Public class Hello { /** * My first Java program */ public static void main( string[ ] args) { // prints the string Hello world on screen System.out.println("Hello world"); } } &!+4= *@$Hello>?8!
E#C" C%BD*AB>?8 8." C G@$F! @4I-J!@$0ABpublicH!>?8,&K+L#&>?8M$>?8!&08 O0%PG%'>?8C+L-81K+L#D*%' C 0N'$ *Q+R+S/?: C &0T"8!++B5+K+L# *@$ U- 7CVP&0%"&B M0'I8P Q0P&M&' I8P!+4! Public class Hello { Q+ -;M".&W'"PQ+'8%X Public class Hello { %'& T+SB&B M$ Public class Hello { /** * My first Java program */ T+S *+F0N'$ 8C) &C$%'& 8@$&5+X
*8 &C$%'Y%.ZT &8C) T+SC+88!'& 8@$!. @$0[)C!'8'. Public class Hello { /** * My first Java program */ public static void main( string[ ] args) { *+%80N'$ * main'%@$hello>?8'7"abb *%' 3&<main&' *CVPmain'7 applet&%\'$. *+%8^;]+: 0'&88!J.< "& R+S75+&B M_ // prints the string Hello world on screen %+/!+8!+ 0'!'System.out.println()'$_ *%8`X *%$. Hello>?8main'a+ #I8P_
8&; *%$..java# +..& *b >?8F^\"%/ * ']+;<public>?8 *c +%80+d- "P.1@$0@$Hello.public *Hello.java8!+B'7[)>?878+%8[)T+S&-+$ *e *&8X &8T+S *+F0N'$0'&8&C$%'Y% *b &+g.zt!'5! % fr+s&;%m *c * & T+S3 C++T+S7;$". *b CT+S7;$ *c %$h)7;$ *e C++T+S7;$
M"#.!CB 00P!'.j!8iiC0N'$ - T+S"%/ // This is a C++ comment style or a single line comment CT+S7;$ L! +;B5+<%XT+S"%/ T+S3!C T+S!'+-./*/ 03/* 8+F0N'$ /* this is an example of a C style or multiline comment */. T+S& h)7;$ ]:C& &C$%'Y% T+Sh)7;$C *' F0W html 7 "P./** T+S3 (javadoc)7;$!c" 0W & *+%8!++B*/ @;gm!%xt+s"7;$!fct+s%. % 5+&,&"@?/&%T+S!!+%k. *8 +%, "'T+S&'+ /?:+' "P&-+$ /** This is an example of special java doc comments used for \n generating an html documentation. It uses tags like: @author Florence Balagtas @version 1.2 */
80@;g;/ ^\"%/ *$. ;"4L+.$7 CM System.out.println("Hello world"); U-. I8P!+%k.03I8P 8;/7C(+ 7 *%8&% 0F9() 7"%/ ;/$ *0 8 G&IDH @W %'+;/ *@$CU-!+ (white space) 4&l B ^\"%/ U- public static void main( String[ ] args ){ System.out.println("Hello"); System.out.println("world ); } &8;/U- 8&<.% 8+ <".&W'^I8P8+'').U- public static void main( String[ ] args ){ ;/"P public static void main( String[ ] args ) + &B M&'
{ 1+U- 7"+8;/& ' C@$'W G%8&7.8 8!D+%8,' public static void main( String[ ] args ) { System.out.println("Hello"); System.out.println("world"); } &$% B***>?81'1+2'&8%'""%/ *%%8 $%C^\Hello, main, System, outm &?\ *%' " C8@$%B"!f%'Case-sensitive&$% *%'+"LhelloHello&$% MC0nC@?/1;N49CL %M $% 7X8%;N49*3 $ I.underline ( _ ) *80N'$5+NH/C"$%7&*% o5 \*% &+-8.-8C%.&$% class, public,void *Q+R+S'+ 0%P&+-8.-8 O &8+2' 8.% ^\"%/ f+%8z)'o5 9C^9>?8&&
ThisIsAnExampleOfClassName 7X89C9!+48@$'W +2''&& 4 ^\"%/ f+%8z)' thisisanexampleofvariablename thisisanexampleofmethodname "8+#& G0+L%XC8D) %X&+2' 9C&B 249!+419!+4&%\'$ @243 +%8Z)'o5 chararray, filenumber, classname +%85+#+2'3& 0nC.Z)'C _write _read &+-8.-8 CH +'$Y% M$8%'+2' *0!++B(+# 1>?81'"%/ &+-8.-8C+.. *+%80N'$" &+2' &+-824C'+4C
Integer Floating-point Boolean Character string R+TH/ %5+.&// <% '88 ' integerr+th 8 FF&a4R+TH/ 0/ o @&%;/ o 05&%;/ o ". R+TH/C0N'$QWL
0/&,& S.p+" 8, 12% @&%;/& 8 0+# 07, 0457% 05&%;/& 8 0X0x+# 0xc, 0Xb, 0x4a3% Floating-pointG&/D%5+./ *% &/() / 0 8.-/.H '$H 8@+-! Standard: 154.0325 Scientific: 1540325e-4, 1.540325e2 True: False: G'$jCD@$ G '$jcd@$ Boolean<% % '%'+M <% Characters&'88
*+F 5H '$'887M Unicode&'88 *L$'+ r&'88/.!5,8'+ bq&'88/._."' 80C!(Unicode)".'88'$_ *80N'$&," C&'88G.Dh)&'88 +6.T(')".single quote!+ "P'88C0N'$& _ *+6.0N'$'a'H a'88c^\"%/ (-$7 '88newlineM'88%h)&'88_ 0"&'88&8&-+$ 1backslash newlinem& '/n''88^\"%/ carriagereturnm& '\r''88 backspace"."f '88& '\b''88 string&' "P 8,% 0 8 '88CFL &' *8Tdouble quotes!+8!+ "P Q'C4\"%/ "Hello world!!!" "this is an example to represent string values" +4&03
@$0!++B03& @49rs (+#: " C Boolean (for logical values) Character (for textual) Byte Short Integer Long (integer) double float (floating point) True, False G<%& D!+4 G%'& 8& D'88 @ 08 R+TH GR+THDo5 G&//D% GBooleanD<%&03 6@49 Boolean&03 ^\"%/ Boolean result = True; &+40ABresult Boolean3C&+2'm ^\ *@$00"P True char&'88&03 * BUnicode&'88C'887M$&'88&03 'singlequote!+ TH 03! *
^\"%/ 'a' //The letter a ' \t ' // a TAB doublequotessinglequote\fh)&'88(.& ' \' ' \\ single quote ' \" ' \\ double quotes +%80N'$\'88C H D.'%+47"%/ string8%x *Q+%8N'8"P$ M ()!1G%'>?8 '*% '88!%X8%%860f' *=<>?8"%/ 0;+4&03"%/ 'double quotes!+ TH ' String message = " Hello world! "; String' *% ^\"%/ R+TH& +4&03 D'@10 8 L$ R+TH 2 \\ decimal value 077 \\ an octal value G05&%;D505G@&%;
0x45b1a \\ a hex value *@%& int&03s (+#: 1R+TH3 + Long&03H "PLl9"5 +. 10l 10L +%80C^+R+TH&03& &0C byte short int long @+ r @+ bq @+ ec @+ qv ^BH _c t c t _b _c bu c bu _b _c eb c eb _b _c qe c qe _b
03C0N'$&.% 8X*.+HL7X89C0N'$longAB,% *@)$+ 1&/ "PN[+)"PC0N'$H %5+.& +4&03 +403"%/ double1s (+#: &/3 *+F % CCL5+/%'+&/ e or E \\. &0H? f or F d or D \\ float \\ double ^\ 3.14 //A simple floating-point value (a double) 6.02E23 //o5 &/7 2.718F // A simple float size value 123.4E+306D // *@$0H9 D "5 8o5 7 w#c&0c &/&03 *Q+%85+#"P^B@;gC@$0$^%8PC
*0N'$7"80+d& "PC8@$&0f+2'7 +2' +2'8&3"P&-+$ 8*03_ *?/1%80+d %%8&+#&wF!+C +2'*Q$"& _ *'% +2' "P +4Z'+2'"?/ +%8?/CH +2' <data type> <name> [=initial value]; &+'[ ]!+ 'T4 x+'9<>!+ 'T $ "PT+SQP%&0$& 'W U& 1. public class variablesamples { 2. public static void main (string [ ] Args) { 3. // declare a data type with variable name 4. // result and Boolean data type 5. Boolean result; 6. 7. // declare a data type with variable name 8. // option and char data type 9. char option; * Q+%8
10. option='c'; // assign c to option 11. // declare a data type with variable name 12. // grade, double data type and initialized 13. // to 0.0 14. double grade=0.0; 15. } 16. } "P "+4+2'8&.% +4"P 1+%8AB+2'8!yT +.@$'W *+ [+) &0%%8A+H8Q$%BD*+%8Z)'N+H$+2'& (."P8+ '&+2'+F^\"%/ G% "P & "C 7L%f+% grade"p1+%80+d"cp *+%8Z)'"P +%8AB+2'7 CM double exam=0; double quiz=12; double grade=18; @+T1'NFd8'49 m L +2'"?/ double exam=0, double quiz=12, double grade=18; "%/ +2' Q+%80N'$C&" CQ+[)+2'7`XY% System.out.println( ); System.out.print( );
+%8"%/ +2'!'& &."%/ C& 1 public class OutputVariable { 2 public static void main( String[] args ){ 3 int value = 10; 4 char x; 5 x = A ; 6 7 System.out.println( value ); 8 System.out.println( The value of x= + x ); 9 } 10 } (."%/ C!'! 10 The value of x=a System.out.print( )System.out.println( )'$& M710!++B&0`XCB System.out.println( )'$ *%8C W'. S&,5+Xp+0`XCB System.out.print( )'$ *%8 +%8"P&0.! 'W U& Example 1: System.out.print("Hello "); System.out.print("world!"); *****************************************************
Out put is: Hello world! Example 2: System.out.println("Hello "); Syetem.out.println("world!"); ******************************************************* Out put is: Hello World! +4&+2'3&+2' +2'3 +4&+2'_ 3&+2'_ +4&+2' longint%+4 +2'_ *%%80+dY 901+9H _ 3&+2' *%%80+dY 9&l >P8%'+2'_ -H&08%B1%%80Y 9C&,"L _ * +2'7.O1+%8"?/[)>?87C&+2''_ %BD*@$0' F>?8".C8+%8 BJ+3C3 )7!' \ 8+%8@$E7+2'"P&-+$
G >?8C. Qstringint&03 +2'+%8s ^\"%/ int num=10; // primitive type string name="hello"; // reference type ^-$&>P.L+6*".&Y 9@+BSC *,80!+%k.1Y 9&,-./A-')3 ;$T&,-./_ &< &,-./_ <%&,-./_ :&,-./_!%X 8B+z8%%8&+#H@4C,-./! *%8./&&% @4X ;+X 8 1%88,-./,-./
;$T&,-./ *Q+%8&"P^B@;gC^F$+4 %88;$T&,-./ 8 7C4\_ 1 public class ArithmeticDemo { 2 public static void main(string[] args){ 3 //a few numbers 4 int i = 37; 5 int j = 42; 6 double x = 27.475; 7 double y = 7.22; 8 System.out.println("Variable values..."); 9 System.out.println(" i = " + i); 10 System.out.println(" j = " + j); 11 System.out.println(" x = " + x); 12 System.out.println(" y = " + y); System.out.println("Adding..."); 13 System.out.println(" i + j = " + (i + j));
14 System.out.println(" x + y = " + (x + y)); 15 //subtracting numbers 16 System.out.println("Subtracting..."); 17 System.out.println(" i - j = " + (i j)); 18 System.out.println(" x - y = " + (x y)); 19 20 //multiplying numbers 21 System.out.println("Multiplying..."); 22 System.out.println(" i * j = " + (i * j)); 23 System.out.println(" x * y = " + (x * y)); 24 25 //dividing numbers 26 System.out.println("Dividing..."); 27 System.out.println(" i / j = " + (i / j)); 28 System.out.println(" x / y = " + (x / y)); 29 //computing the remainder resulting from dividing 30 // numbers 31 System.out.println("Computing the remainder..."); 32 System.out.println(" i % j = " + (i % j)); 33 System.out.println(" x % y = " + (x % y)); 34 35 //mixing types 36 System.out.println("Mixing types..."); 37 System.out.println(" j + y = " + (j + y)); 38 System.out.println(" i * x = " + (i * x)); 39 } 40 } ********************************************************************* & Variable values... i = 37
j = 42 x = 27.475 y = 7.22 Adding... i + j = 79 x + y = 34.695 Subtracting... i - j = -5 x - y = 20.255 Multiplying... i * j = 1554 x * y = 198.37 Dividing... i / j = 0 x / y = 3.8054 Computing the remainder... i % j = 37 x % y = 5.815 Mixing types... j + y = 49.22 i * x = 1016.58,-./7&-./"%/ &//7R+TH/78''L 8!C(+#O!* &//7+'1' F8
&//7 ;%.S: &//1 ;$T./ *0+g;$T{z$ 85 &,-./ G D,5,-./ G D,8,-./ (5 97a+,-./85 &,-./ *%(8 ^BC&;/^\"%/ count = count + 1 ; count ++ ; 85,&,-./L-./ } ^
*0P"-./CB ;%85 &,-./ (8i(5 97-./' 1' F8 -./C;F *0+g;$T+2'{z$ int i = 10; int j = 3; int k = 0; k = ++j + i; // will result to k = 4+10 = 14 ;$T-./&+4' 1' F8 -./CB F (8i(5 97+2'"PCB 0ga+ int i = 10; int j = 3; int k = 0; k = j++ + i; // will result to k = 3+10 = 13 ^\"%/ * ^\"%/ 8_5 &,-./C0N'$&'L 0$8+, 8 &F 8_5 &,-./;/+.~ *% QW '9 0 &< &,-./ "P"+&< 080&< &,-./ *%%8"+
%BooleanCLH C! (True/False)* %P'+,-./!L-./&0T d^]:c +%8C^\ 8U& 1 public class RelationalDemo{ 2 public static void main(string[] args){ 3 //a few numbers 4 int i = 37; 5 int j = 42; 6 int k = 42; 7 System.out.println("Variable values..."); 8 System.out.println(" i = " +i); 9 System.out.println(" j = " +j); 10 System.out.println(" k = " +k); 11 //greater than
12 System.out.println("Greater than..."); 13 System.out.println(" i > j = "+(i>j));//false 14 System.out.println(" j > i = "+(j>i));//true 15 System.out.println(" k > j = "+(k>j));//false 16 //greater than or equal to 17 System.out.println("Greater than or equal to..."); 18 System.out.println(" i >= j = "+(i>=j));//false 19 System.out.println(" j >= i = "+(j>=i));//true 20 System.out.println(" k >= j = "+(k>=j));//true 21 //less than 22 System.out.println("Less than..."); 23 System.out.println(" i < j = "+(i<j));//true 24 System.out.println(" j < i = "+(j<i));//false 25 System.out.println(" k < j = "+(k<j));//false 26 //less than or equal to 27 System.out.println("Less than or equal to..."); 28 System.out.println(" i <= j = "+(i<=j));//true 29 System.out.println(" j <= i = "+(j<=i));//false 30 System.out.println(" k <= j = "+(k<=j));//true 31 //equal to 32 System.out.println("Equal to..."); 33 System.out.println(" i == j = " + (i==j));//false 34 System.out.println(" k == j = " + (k==j));//true 35 //not equal to 36 System.out.println("Not equal to..."); 37 System.out.println(" i!= j = " + (i!=j));//true 38 System.out.println(" k!= j = " + (k!=j));//false 39 } 40 } Q!&
Variable values... i = 37 j = 42 k = 42 Greater than... i > j = false j > i = true k > j = false Greater than or equal to... i >= j = false j >= i = true k >= j = true Less than... i < j = true j < i = false k < j = false Less than or equal to... i <= j = true j <= i = false k <= j = true Equal to... i == j = false k == j = true Not equal to... i!= j = true k!= j = false
<%&,-./ 8&-./"%/ Boolean7<%&,-./ *%F Boolean7"%/ +' <%,-./3( &<% 4 ]<% <%& 4 ]<%& ^&T&!y+ 0 8 CH <%&,-./-8: X1 op X2 Boolean;/"00"X2X1 8 &F(&.CL5+op.*@ gx% +2'X^9 *b *c *e *v *u *q * 8w4m.&0%"H?: f0"b 8'$&^ *% X2X1a+8C!L.I9 &&&& '$^
&&&"+-HN *@+,%&L+49f%L+./08 C]: && C00;/^\"%/ B$1 ''$jcf%8 Cexp11&&,-./ exp1f8xgexp2 " D*,+ False @$1 exp2cy% H8H91 ''$ * C-./1+'"F C;+.&,-./1 _ *%8 &&&&,-./L-./&0TC4\ 1 public class TestAND { 2 public static void main( String[ ] args ){ 3 int i = 0; 4 int j = 10; 5 boolean test= false;
6 // demonstrate && 7 test = (i > 10) && (j++ > 9); 8 System.out.println(i); 9 System.out.println(j); 10 System.out.println(test); 11 // demonstrate & 12 test = (i > 10) & (j++ > 9); 13 System.out.println(i); 14 System.out.println(j); 15 System.out.println(test); 16 } 17 } dh 8B<! "X*. C1&&-./8<j++;/8+%8_ *@$0 CFalse"PC(+#i>10^;/ &,-./& '$^
&,-./!+ -HN,-./L+49f%80N'$O$ C&0+C,-./ *@+!+%X! Q+, YCH Y;/F exp1 exp2 B$1 + True"PL+H%8 Cexp1',-./ *F True;/fexp2 C" exp2exp1;/' 1+'"F C(+#f,-./ *%8 C1 +%81@$00d8& B< 1'W U& 1 public class TestOR { 2 public static void main( String[] args ){ 3 int i = 0; 4 int j = 10; 5 boolean test= false; 6 //demonstrate 7 test = (i < 10) (j++ > 9); 8 System.out.println(i); 9 System.out.println(j);
10 System.out.println(test); 11 //demonstrate 12 test = (i < 10) (j++ > 9); 13 System.out.println(i); 14 System.out.println(j); 15 System.out.println(test); 16 } 17 } @$CH 8! "X*. Cf@$,-./&8<j++;/8+%8_ *@$0 CTrue"PC(+#i<10 @$00P^& '$^@.!
L8 @$CW%1C&T^& C&+' * CGFalseD@$&,GTrueD@$-./C C(+#-./^&T&,-./L-./8+%8 *+F C+'"F +%88B<! 'W U& 1 public class TestXOR { 2 public static void main( String[ ] args ){ 3 boolean val1 = true; 4 boolean val2 = true; 5 System.out.println(val1 ^ val2); 6 val1 = false; val2 = true; 7 System.out.println(val1 ^ val2); 8 val1 = false; val2 = false; 9 System.out.println(val1 ^ val2); 10 val1 = true; val2 = false; 11 System.out.println(val1 ^ val2); 12 } 13 } dl 8!!y+,-./L-./
;/7"FP"P8&: *%88"FP7!,-./ * @ g+2' Q0P,-./!& '$^d 'W U& 8B<7QC _ 1 public class TestNOT { 2 public static void main( String[] args ){ 3 boolean val1 = true; 4 boolean val2 = false; 5 System.out.println(!val1); 6 System.out.println(!val2); 7 } 8 } false true @$dl 88B<!_?::<%,-./ "5.H "FP$8%B! *@$F$,-./7 _ *w#:&;/"%/
@$CH?:,-./&+F8 '$ exp1? exp2 : exp3 True@$!L."P&+'8@$Boolean;/7exp1% * False?:,-./&+' 0F +'"%/ exp2;/1 ''$jcexp1f * 0F +'"%/ exp3 ;/1 ''$jcexp1f * Q+%8=:^\"%/ &8B<'W U& 1 public class ConditionalOperator { 2 public static void main( String[] args ){ 3 String status = ""; 4 int grade = 80; 5 //get status of the student 6 status = (grade >= 60)?"Passed":"Fail"; 7 //print status 8 System.out.println( status ); 9 } 10 } 8B<!&+'@W_ Q%P'W?:,-./M$+'"FC &0T CL _
&+#H!+C,-./ C1 0'NF!C(+#8<. a+!+;c^*!++b,-./&% @4"P:8%8 @$&,-./&% @4 @4&0. ;/3 1.. [ ] ( ) 2. ++ --! ~ 3. * / % 4. + - 5. << >> >>> <<< 6. < > <= >=
7. ==!= 8. & 9. ^ 10. && 11. 12.?: 13. = m ^'L_ * L@4C1^C<$78;/Gb &'.8@4C!+6#;/'&'+ @4I ;/Gc * QC#C&0+k+#;/$ I 0'NFa4< 8C CH ";/! ;/&@4&'L ;/f;$t&,-./ C.F$FC5+#& *+%80N'$5'#C089:0$H
4!0"+ a4<&h? &0@49C++1C0N'$&j];:DT+S G%' &+-8.-81F$%1U- 1;/ G'&'881G!+4 D<%1%5+.1R+THD string, character, boolean, floating point, integer a+ boolean, char, byte, short, int, long, float, +4&03 double G"%/ `X1+41"?/D+2' System.out.print( )System.out.println( )& +43 < &,-./15 _8&,-./1;$T&,-./D,-./ G,-./@41G?:D:,-./1<%&,-./1& ~~~~~~ www.java4every1.wordpress.com0+w blogsofmine@gmail.como? @ >.+. X&.%Ca%8@$/'55/"F%C"# *CTfo? jcp%p "'W