Java Learn
Java Learn
" # $ %#
Sun Microsystems
%& ' $ ! "# $
$( )"' ' Sun
." +)$ " , - "*)"Write once, Run Everywhere"
"Java 3 1' 2 + 0 / )"' & 0 -
$ 3 6 5 JVM ! 4 )Virtual Machine"
$ !+ 9 : ;& 0 7 ' !4 2 2 8
95 " 46 9 5 $ 8 + ' $ +' +
!+ 9 $ ) '0 8 + ' *) ) *) < "
@ *) $ " " ?=> Linux ( !+ 9 2 Windows
<8 + ' &
? ' & A$ !+ / 4 9 $ 5
35 7 Web - 9 & D+ 8 ' BC 9> '
FD+ % (Java Applets) ' @ +E' ' @ +5 9 5
- 7D! 3$ 9 "# 9 5 35 ' G /(
. B 0
+ + - $ C C ' 8 ' $ '
& + " 9 -' $ & H % I A
.? 2 ) $
J "4 ' !+ B $ +) !4 C
J B K4 C 0 +L 4 J @ +E' 7
B 0 9 "4 M N M) , & B#
< D $+ 8 ' E % & (Garbage Collector) D+
[email protected] !" # $ %#
[email protected] !" # $ %#
[email protected] !" # $ %#
Java Applet -2
" " E' W + +T S! !4 ! *& ' V +) V +
$ ' HTML / "4 $=4 2 " > Applet 8 + ' &
)HTML . U / 2 " server " 4 9 Applet 8 + '
52 + & HTML / $4 " ( " Applet @ +5
. " F " $=4 X @
: - # Java Application -3
+B $ O 4G ' B ' ,I + ' 2' 0'
+ +T 8 ' "W +) + +R ' S" ' $
@ 0' *) " " 5 , + > +) S3 ) M
CLIENTSERVER . ' ' K 4 ) DISKTOP '
[email protected] !" # $ %#
x*y*z
[email protected] !" # $ %#
[email protected] !" # $ %#
&/ 0
8-bit two's complement
byte
!
Byte-
length
integer
Short integer
short 16-bit two's complement
" #$% & %' ()* # + , - . /0&1
2- 34
Integer
int 32-bit two's complement
34 " #$% & %' ()* # + , -
! 2-
Long integer
long 64-bit two's complement
34 " #$% & %' ()* # + , -
! 2-
"" + &/ 0
Single-precision floating point
float 32-bit IEEE 754
34 " #$% & %' ()* #$45 6 )7*
! 2-
Double-precision floating point
double 64-bit IEEE 754
34 " #$% & %' ()* #$45 6 )7*
! 2-
[email protected] !" # $ %#
Import java.io.*;
Public class Count {
Public static void countChars(Reader in)
throws IOException
{
int count = 0;
[email protected] !" # $ %#
Z $> ++
int[] arrayOfInts;
arrayOfInts[j] = j;
[email protected] = !" # $ %#
+ #2 !+ $ +D3) >
Strings
< # G $"' / G B $ 2+ 0' V + ' 2' V + &
B > 0 ? @ " 4A
[email protected] !" # $ %#
!"
* ) * +, , - +*$ $ %& ' ($ #
4 2 3 & 0 - . / 0 1 0
7 3 # &'
Z $ " ) ++
9+ G
@ 4G try-catch-finally, throw
)
break, continue, label: , return
D 4
[email protected] !" # $ %#
# &4 =4 B + G + " $3
* Y B3+ * X &5 $ + => 8 + '
<& =
8 + ' @ 4 / + )B + G + Z; 0
<B# &5
+ , C %+ " I +) ;& 2 + + "' -
$ ' ' D4 , ;& > G , O G' )
N '' + $ + < G % ) $ $> O
<$ !D
[email protected] !" # $ %#
if - else @ ' ? 0
if ( ' )
Statement
else
Statement
[email protected] !" # $ %#
int testscore;
char grade;
[email protected] !" # $ %#
int month;
switch (month) {
case 1: System.out.println(“January”); break;
case 2: System.out.println(“February”); break;
case 3: System.out.println(“March”); break;
case 4: System.out.println(“April”); break;
case 5: System.out.println(“May”); break;
case 6: System.out.println(“June”); break;
case 7: System.out.println(“July”); break;
case 8: System.out.println(“August”); break;
case 9: System.out.println(“September”);
break;
case 10: System.out.println(“October”); break;
case 11: System.out.println(“November”);
break;
case 12: System.out.println(“December”);
break;
}
[email protected] !" # $ %#
for ? 0
statements
Z $> &4I+
int i;
...
// @ ' # +
...
[email protected] !" # $ %#
while ( ' )
statement
Z $> ++
while (X != 10) {
X++;
& + 0 do while
do {
statements
} while (booleanExpression);
[email protected] !" # $ %#
int c;
...
do {
c-- ;
...
} while (c != -1);
6!
break A/ 0
+ ' , D @C - & /# G O 4) D %+
[email protected] !" # $ %#
breakToHere: someJavaStatement
break breakToHere;
<
[email protected] = !" # $ %#
[email protected] !" # $ %#
superclass -G /! 9 0
subclass ' /! 9 0
<extends 0 >
Z =
ZS )
Z*) 7 ! MF -G /! N M ' /! N M *) ) +
[email protected] !" # $ %#
Z_ '
< /! -G /! 9 5 super $
Z_ 4
< > + Z is a U D
Z_ "
U 0 %& ' /! N 9 5 -G /! N $
< exeptionZ *) @ +> +" 8+ casting U B3+ &5 ^ * casting
Z_ '
[email protected] !" # $ %#
Zfinalizer Pa
?super.finalizerA
[email protected] !" # $ %#
[email protected] !" # $ %#
"
public MyClass{
…..
try{ int x= myMethod() ; }
catch(Excrption e){ ….. }
…..
}
[email protected] !" # $ %#
; 34 9 = D !4
=> a /! N M9 * #$ @ 5 ClassCastException :
.2( ' " 2( +') " a /! FD+ F " " /! 9 5...
2 + ' D! + N M C4 ArrayStoreException :
. D! 2' + - +
2 O = null ( 2 # N M $ $ NullPointerException :
data member .$ ;" + 5 ) +' ) method $ ; new )
[email protected] !" # $ %#
"
\try
? D 4 / D! V + A > ) ) @ +> "# " +
& D+ $ ++ Catch $ + H +' 2 &D+ @ +> *) " & &5
finally
cStatment1
cStatment2
cStatment3
cStatment4
[
"
catch(IOException e){
try 2+ / O *& @ +> 8 "
2( '6 " ) ) IOException ) + A ! ' "" V + @ +> 8
$ + > " C= @ R &4 + O
finally
$ + IOException V + try / & @ +> &5
. catch
' @ +> /& method $ ) /! 5 ) << catch % + E
throws
) 4 "4 ,"( (
$ ' V + @ +> 5 5 <<< + > 4 "4
. 2 RuntimeException
[email protected] !" # $ %#
"
catch(Exception e){}
"
} " G % *& " finally{
: =
.@ +> /&# 2+ 8 + Statment2; & D+ ) N D'
> ' catch $ K 95 $ + > H +' Statment1; & D+ $! E
= ) + & finally 95 $ + > (= @ +>
Statment3;
Statment4;
.8 + ' @C ) " + ;& 9 ; ' + - & D+ *
[email protected] !" # $ %#
Z @ +> $ ( * Throwable /! + N MG
[email protected] = !" # $ %#
"
"
class class1\
method1 () { method2[c?A
[email protected] !" # $ %#
class class2\
method2e \ ?A
"
[email protected] !" # $ %#
[email protected] !" # $ %#
"
Attributes D! 7 ! 0' ! O +
/ " ;& ) + Shadows ) Public $> $ ""
* (D O 4) > FD+' ,"
Y <<< K( !4 " /( (D 3 ( !+ FD+ 9
9 - "" *& " "' K( !4 /( ;& / "" +
;& 3 ," !+ " ) + 0 ( *)
D )- *) Module ) Namespace O 9 /
) ) ' f ++ @ 5 3 ! M
Default "4 ' 3 !4 "" O 4) 3 )
!+ 7 ! "+ Private ) Public $> $ ! "" "4
( 3 "4 "+ Shadows ) Overloads "4
[email protected] !" # $ %#
>G N' : O +
K( !4 N ' 9 * K4 G / + )
"
Interface IPerson
End Interface
' N' / ++ )
"
Interface ISomeMath
[email protected] !" # $ %#
End Interface
K4 G / " / + )
"
Interface IPhonebook
Inherits IPerson
Sub ShowInformations?A
End Interface
"
Implements IPhonebook
[email protected] !" # $ %#
End Get
End Property
<<<<<<<
End Class
"
Class SomeTest
Implements IPerson
Implements ISomeMath
Get
End Get
End Property
<<<<<<<
Implements ISomeMath.AddNumbers
[email protected] !" # $ %#
<<<<<<<
End Class
"
Interface IPhonebook
Interface IPersone
End Interface
Sub ShowInformations?A
End Interface
[email protected] !" # $ %#
"
Class test
Implements IPhonebook
Implements IPhonebook.IPersone
<<<<<<<
End Class
[email protected] !" # $ %#
[email protected] = !" # $ %#
F
<< +@ FI - $ ) $ 4"R ' %+
[email protected] !" # $ %#
" 7'!
"
import java.ioca<
class BRRead\
char cc
InputStreamReader(System.inc??
[email protected] !" # $ %#
c = (char) br.readc?A
System.out.println(cc?
$ / ' +" ' $4"+ ) " + "+ " ' $ 4"5 0' "
char +
) +# / 95 ) $ >" $ )
/ $=4 " ' ,@
Z"
import java.ioca<
class BRRead\
String cc
InputStreamReader(System.inc??
System.out.println(”Enter charactersc?h
c = br.readLinec?A
System.out.println(cc?
[email protected] !" # $ %#
< string + $ ) =+
"
import java.util.Scannerc
class IN
[email protected] !" # $ %#
G 9
public H ' +/ "'
+" =>
[email protected] !" # $ %#
protected 9 5 ) I+
> ' K 4 2+ public UU ' 2 ' J &
& extends 0 "+ class protected $ / ' + "+ =>
protected + &5 3 ) > 0 )B F=
[email protected] !" # $ %#
JBULDER 7 3 #
[email protected] !" # $ %#
[email protected] !" # $ %#
[email protected] = !" # $ %#
[email protected] !" # $ %#
[email protected] !" # $ %#
[email protected] !" # $ %#
H 3
[email protected] !" # $ %#
[email protected] !" # $ %#
[email protected] !" # $ %#
[email protected] !" # $ %#
- 7 9 8 + ' ! 4 @ +5 =+ $ "'
8 + ' 7D G F C' 3
ZV ' @"' 7
1
[email protected] !" # $ %#
[email protected] = !" # $ %#
8 + ' & D+ .
[email protected] !" # $ %#
[email protected] !" # $ %#
[email protected] !" # $ %#
[email protected] !" # $ %#
PZ $ 9 % 3 2 ' #- 7 9 8 + ' + 5
[email protected] !" # $ %#
[email protected] !" # $ %#
command )- >$ > )"' 9 5 - &) 8 + ' & D+ % & " '
: , ! F " 95 $ !
[email protected] !" # $ %#
[email protected] !" # $ %#
import javax.swing.*;
import java.util.ArrayList;
int i = 1;
int c90 = 0, c80 = 0, c70 = 0, c60 = 0, failed = 0;
ArrayList<Integer> listC90 = new ArrayList<Integer>();
ArrayList<Integer> listC80 = new ArrayList<Integer>();
ArrayList<Integer> listC70 = new ArrayList<Integer>();
ArrayList<Integer> listC60 = new ArrayList<Integer>();
ArrayList<Integer> listFailed = new ArrayList<Integer>);
while(i != 0 ) {
String s = JOptionPane.showInputDialog("Enter number");
int x = Integer.parseInt(s);
if (x == 0)
i = 0;
if ( x >= 90 && x <= 100) {
listC90.add(x);
c90++;
} else if ( x >= 80 && x < 90) {
listC80.add(x);
c80 ++;
} else if ( x >= 70 && x < 80) {
listC70.add(x);
c70 ++;
} else if ( x >= 60 && x < 70) {
listC60.add(x);
c60 ++;
} else if ( x < 60) {
listFailed.add(x);
failed ++;
}
}
System.out.println("A "+ c90 + " - " + listC90);
System.out.println("B "+c80 + " - " + listC80);
System.out.println("C "+c70 + " - " + listC60);
System.out.println("D "+c60 + " - " + listC80);
System.out.println("F "+failed + " - " + listFailed);
}
}
[email protected] !" # $ %#
import javax.swing.JOptionPane;
sum=sum+i;
coun+=1;
}
avg=sum/coun;
String msg=String.format("the sum is:%d",sum);
String msg2=String.format("the Count is:%d",coun);
String msg3=String.format("the Average is:%f",avg);
JOptionPane.showMessageDialog(null,msg);
JOptionPane.showMessageDialog(null,msg2);
JOptionPane.showMessageDialog(null,msg3);
System.exit(0);
}
}
[email protected] = !" # $ %#
import javax.swing.*;
class PrintOdd{
public static void main(String[] args){
String s = JOptionPane.showInputDialog("enter a positive integer");
int n = Integer.parseInt(s);
for(int i=0; i<=n; i++)
if(i%2 == 1)
System.out.println(i);
}}
- Person /+! / P)
?nameA ) ?idA # K4 $ •
G # ("' #"" ; 3# / •
K4 7 setName / •
class Person{
private int id;
private String name;
public Person(int id1, String
name1){
id = id1;
name = name1;
}
public void setName(String n){
name = n;
}
}
[email protected] !" # $ %#
[email protected] !" # $ %#
import javax.swing.*;
class PrintInverse{
public static void main(String[] args){
String s = JOptionPane.showInputDialog("enter a sentence");
for(int i=s.length()-1; i>=0; i--)
System.out.print(s.charAt(i));
}}
[email protected] !" # $ %#
" # $ % !
$ &
[email protected] !" # $ %#