¥»ºô¶¥H¥´³yµL»Ùê¾\Ū¬°¥Ø¼Ð¡A¥i¥H¥Î¥ô¦óÂsÄý¾¹¨ÓÆ[¬Ý¥»ºô¶
Javaªº¸ê°T¨ä¹ê¤w¸g°÷¦h¤F, ¤£¦ý¦³³\¦h®ÑÁÙ¦³¦U¦¡¦U¼Ëªººô¯¸, ·F¹Àµ§ªÌÁ٧ˤ@ÓJavaµ§°O©O? ¦Ñ¹ê»¡¥¿¦]¬°¸ê°T¤Ó¦h¤Ï¦Ó®e©ö§Ë¤£²M»y¨¥ªº¥þ»ª, ¤@¤è±¤]¬O¥»¤H¬°·Ç³ÆSCJP¦Ò¸Õ©Ò¾ã²z, §Æ±æ°µ¨ì´£ºõ®²»â, ¦r¦r¯]¿Y, ¨S¦³¼o¨¥¡C
·PÁ´¿¸g«ü¾ÉªºSCJP¦Ñ®v : ¦¿¸qµØ ¥ý¥Í
¦bSun Microsystemsªº Java»y¨¥¥Õ¥Ö®Ñ¸Ì´£¨ì¡A Java»y¨¥¦³¥H¤U¯SÂI
À³¸ÓÁÙ¥i¥[¤J¥H¤U¯SÂI
Java Development Kit 5.0 ©x¤è¦w¸Ë»¡©ú¡AY»Ýnstep by stepªºª©¥»½Ð°Ñ¦Ò¡GJava Programmingµ{¦¡µo®iÀô¹Ò«Ø¸m«ü«n
| HelloWorld.java | asciiªºjavaì©lÀÉ |
|
javac HelloWorld.java |
½sĶ(compiler) |
| HelloWorld.class | ²£¥Íjavaªºbytecode |
|
java HelloWorld |
VM(Virtual Machine)¥i°õ¦æ¦b¦UºØ¥¥x
|
| Hello, World | µ{¦¡°õ¦æµ²ªG |
/** ²Ä¤@¤äJavaµ{¦¡
¥u¦³µù¸Ñ¥i¥H©ñ¦b package °Ï¤§«e
*/
//package «Å§i°Ï //¥iµL¦¹«Å§i°Ï¡AY¥¼«ü©wpackage«h¹w³]package¬°¥Ø«e¥Ø¿ý
package exam.test;
//import ±Ôz°Ï //¥iµL¦¹±Ôz°Ï¡A¹w³]¤@©w·| import java.lang;
import java.awt.Button; //¤Þ¤J«ü©wªºclass
import java.util.*;
//¤Þ¤J«ü©wªºpackage
//class, interface, enum ©w¸q°Ï
//int x; // µo¥Í½sĶ¿ù»~
public class HelloWorld
{
public static void main( String[] args ) // µ{¦¡°õ¦æ¶i¤JÂI
{
System.out.println( "Hello World " ); //¦]¬°¹w³] import ¤F java.lang , ©Ò¥H¥Nªí³oÓ System.out.println ¦b¨ä¤§¤U
//b = 200; // µo¥Í½sĶ¿ù»~
tmp obj = new tmp();
obj.i = 6;
System.out.println( obj.i );
}
}
class tmp
{
int i = 5;
}
½Ð¥H¤å®Ñ½s¿è¾¹Áä¤J¤W¤èµ{¦¡°Ï¶ô¤º®e¡A¨Ã¯S§Oª`·N¤U¦C´XÂI
¼¶¼gµ{¦¡®É±`¹J¨ìn¬°Ãþ§O¡A¨ç¼Æ¡AÅܼơA±`¼Æ©R¦Wªº±¡ªp¡AJava ¤¤ªº©R¦W¥²¶·²Å¦X¨äÃѧO¦r(Identifier)ªº³W½d
©µ¦ù¾\Ū¡GJava Programming Style Guide , Code Conventions for the Java Programming Language(Javaµ{¦¡½XºD¨Ò½Ķ)
| classÃþ§O | html»yªk |
|---|---|
|
import java.awt.*;
import java.applet.*; public class appletest extends Applet { ¡@ //¤¸¥ó°Ï String s1 = ""; ¡@ButName=this.getParameter("ButtonName"); //±µ¦¬°Ñ¼Æ ¡@public void init() //¥iµø¬°«Øºc¨ç¼Æ ¡@{ ¡@¡@s1=s1+"init()->"; ¡@} ¡@public void start() //¥iµø¬°mainµ{¦¡¶i¤JÂI ¡@{ ¡@¡@s1=s1+"start()->"; ¡@} ¡@public void stop() ¡@{ ¡@¡@s1=s1+"stop()->"; ¡@} ¡@public void destroy() ¡@{ ¡@¡@s1=s1+"destroy()->"; ¡@} ¡@public void paint(Graphics g) //¸ü¤J®É«µeApplet©Îcall repaint() ¡@{ ¡@¡@ g.drawString("Hello World!", 25, 25); ¡@} } |
<html>
<head> </head> <body> ¡@<applet code="appletest.class" width=500 height=150> ¡@¡@<param name = "ButtonName" value = "B1"> ¡@¡@alt="±zªºÂsÄý¾¹¤£¤ä´©Applet" ¡@</applet> </body> </html>
/*
|
µ§ªÌªº«ØÄ³¬Oªì¾Ç¨Ï¥Î¯Â¤å¦rªº½s¿è¾¹¡A¨ä¤¤µ§ªÌ°¾¦nªº¬O
Crimson
¡A¤é«á¥i§ï¥Î¤õ¬õªºEclipse¡C
¥H¤U°²³]SDK¤w¦w¸Ë¦bd:\jdk¡A±µ¤U¨Ó¬°¤j®a¤¶²Ð¦p¦ó³]©wCrimson»PUltraEdit§@¬°½s¼gjavaªºIDEÀô¹Ò ¡C
| Primitive Types / simple type | ½d³ò | wrapper class /refrence type / composition type |
|---|---|---|
| byte (1byte) | -128 ~ 127 | Byte |
| short (2bytes) | -32768 ~ 32767 | Short |
| int (4bytes) | -2147483648 ~ 2147483647 | Integer |
| long (8bytes) | -9223372036854775808 ~ 9223372036854775807 | Long |
| float (4bytes) | +-3.4028237*10+38 ~ +-1.30239846*10-45 | Float |
| double (8bytes) | +-1.76769313486231570*10+308 ~ 4.94065645841246544*10-324 | Double |
| char (2bytes) | Unicode characters | Character |
| boolean (1bytes) | true©Îfalse | Boolean |
| String |
| ±`¼Æ |
|---|
| ¼ÆÈ |
| ¾ã¼Æ¡Gfinal int a=10 |
| ¯BÂI¡Gfinal double d1=1.23 (¹w³]) |
| final float d2=1.23f |
| ¦r¤¸¡Gfinal char var = 'A' |
| final char var = '\u0041' |
| ¦r¦ê¡Gfinal String var = "hello" |
| ¥¬ªL¡Gfinal boolean flag = true |
| ¦Û°ÊÂ૬ | ±j¨îÂ૬ | ||
|---|---|---|---|
|
double d;
d=123; |
d¬°double«¬ºA
123 ¬°int«¬ºA ¤pÂà¤j¡A¨t²Î¦Û°ÊÂ૬ |
int a;
a= (int) 123.123; |
a¬°int«¬ºA
123.123 ¬°double«¬ºA ¤jÂà¤p¡A»Ý±j¨îÂ૬ ¡A§_«h·|½sĶ¿ù»~ |
| °ò¥»¸ê®Æ«¬ºAÂà¥]¸ËÃþ§O | ¥]¸ËÃþ§OÂà°ò¥»¸ê®Æ«¬ºA | ||
|---|---|---|---|
| Integer i = new Integer(10); Float f = new Float(10.1); |
§Q¥Îjava.langªºwrapper class¡A¶Ç¤Jprimitive type¡A¶Ç¦^¹ïÀ³wrapper class ªº¹ê¨Ò |
Integer.valueOf(i); Double.valueOf(f); |
§Q¥Îjava.langªºwrapper class¡A¶Ç¤Jwrapper class ªº¹ê¨Ò¡A¶Ç¦^¹ïÀ³ªº primitive type |
public class DataType
{
public static void main(String args[])
{
int i; //Åܼƫŧi¥H¸ê®Æ«¬ºA¶}ÀY¡A«á±µÅܼƦWºÙ
i=3; //µ¥¸¹ºÙ¬°«ü©w±Ôz(Assignment Statement)¡A¥ªÃä¬OLvalue¥NªíÅܼƦWºÙ¡A¥kÃä¬ORvalue¥Nªí¹Bºâ¦¡(Expressions)
System.out.print("oringinal i is ");
System.out.println(i);
i = 4; // 4 ¦b¦¹ºÙ¤§¬°Integer Literal
System.out.printf("i be changed to %d\n\n",i); //Java 1.5°_¤ä´©printf()
final int j = 3;
System.out.print("oringinal j is ");
System.out.println(j);
// j = 4; //¹ï±`¼Æ«ü©wÈ·|³y¦¨½sĶ¿ù»~
System.out.printf("i be changed to %d\n\n",j);
int a1 = 100;
long n1 = a1; // ¦Û°ÊÂ૬¦¨¥\
¤p¼ÆÈÂà¤j¼ÆÈ( int Âà long )
long n2 = 100;
//int a2 = n2; // ¦Û°ÊÂ૬¥¢±Ñ
¤j¼ÆÈÂà¤p¼ÆÈ ( long Âà int )
int a2= (int)n2; // ±ý±N¤j¼ÆÈÂà¤p¼ÆÈ»Ý¨Ï¥Î ±j¨îÂ૬ ( long Âà int )
int a3 = (int)123.23; // double Âà int
int a4 = (int)123.23F; // float Âà int
byte a5 = 'A'; // char Âà byte
double d = 123.3;
//a1 = a1 + d; //¦Û°ÊÂ૬¥¢±Ñ
a1 += d; // ±Ä¤£©ú½T«¬ºAÂà´«¦¨¥\
/*°Ñ¦Ò«¬ºAÂàì¥Í«¬ºA*/
//a2 = "123"; // ¦Û°ÊÂ૬¥¢±Ñ (String Âà int)
//a2 = (int)"123"; // ±j¨îÂ૬¥¢±Ñ (String Âà int)
a2 = Integer.parseInt( "123" ); //§Q¥ÎÀRºAª«¥óªº¤èªk
}
}
Tip:¦p¦ó´M§äAPI ? 1.¶Ç¤J¬°°ò¥»¸ê®Æ«¬ºA 2.¦^¶Ç String «¬ºA 3.¤@©w¬Ostatic 4.APIªº¸ÑÄÀ¬°©Ò»Ý
String Ãþ§O¤¤¤w¸g´£¨Ñ¤F±N°ò¥»¸ê®Æ«¬ºAÂà´«¦¨ String ªºÀRºA(static)¤èªk¡A¦³¤U¦C´XºØ
double d = 123.35;
String str = String.valueOf(i);
Tip:¦p¦ó´M§äAPI ? 1.¶Ç¤J¬° String «¬ºA 2.¦^¶Ç °ò¥»¸ê®Æ«¬ºA 3.¤@©w¬Ostatic 4.APIªº¸ÑÄÀ¬°©Ò»Ý
n±N String Âà´«¦¨°ò¥»¸ê®Æ«¬ºA¡A¤j¦h»Ýn¨Ï¥Î°ò¥»¸ê®Æ«¬ºAªº¥]¸ËÃþ§O(wrapper class)ªºÀRºA(static)¤èªk¡A¦ý¶·ª`·Nªº¬O¡AYÂà´«¥¢±Ñ®É·|¥á¥X¨Ò¥~¡GNumberFormatException
try
{
String str = "3000";
int i = Integer.parseInt(str);
}
catch (NumberFormatException e)
{
System.out.println(" Âà´«¾ã¼Æ®É¥¢±Ñ!! " + e);
}
| = |
int a,b,c,d;
a=10; a=b=c=10 //right association |
| +=,-=,*=,/=,%= |
int a=100 ;
a=a+d;
//½sĶ¿ù»~,¦]«¬ºA¤£²Å
|
| *,/ |
int a=100, b=22, c;
double d=12.3; c=a*b; c=a/b; c=a*d; //½sĶ¿ù»~,¦]«¬ºAÅܤj¤F |
| ++,-- |
int a=10;
b=a++; //b=10 int a=10; c=++a; //c=11 |
¤ñ¸ûªºµ²ªG¤@©w¬° boolean «¬ºA
| ==,!= | Equality Operators |
| >,>=,<,<= | Relational Operators |
¨äoperands¥²¶·¬° boolean«¬ºA, ¤ñ¸ûªºµ²ªG¤]¤@©w¬° boolean «¬ºA
| !,&&,|| |
¨äoperands¥²¶·¬° byte, short ,int, long charµ¥
| ~, &, |, ^ |
~10 => 0101 =>5
|
¨äoperands¥²¶·¬° byte, short ,int, long charµ¥
| <<, >>,>>> |
10 << 2 => 1010 << 2 => 101000 => 40 //¥kÃäbits¶ñ0
-1>>>1 => 11......1>>>1 => 01.....1 => 2147483647
//-1¥k²¾«á¸É0
|
public static void main(String [] args) {
int a = -10 ;
System.out.println("a = " + a); //-10
System.out.println("a = " + Integer.toBinaryString(a)); //11111111111111111111111111110110
System.out.println("a = " + (a >> 1)); //-5
System.out.println("a = " + Integer.toBinaryString(a >> 1)); //11111111111111111111111111111011
System.out.println("a = " + (a >>> 1)) ;//2147483643
System.out.println("a = " + Integer.toBinaryString(a >>> 1)); //01111111111111111111111111111011
}
| 2Ó±Ôz¥y,¤À¼g2¦æ |
int a;
a=10; |
| 2Ó±Ôz¥y,¥u¼g1¦æ |
int a; a=10; |
| 1Ó±Ôz¥y,¤À¼g¦h¦æ |
int a
= 200; |
| ³æ¤@ | Âù¦V | ¦h¦V | |
|---|---|---|---|
| »yªk |
if (expression)
¡@¡@statement |
if (expression)
|
switch (expression)
|
|
³Æµù |
expression¥²¶·¬°boolean«¬ºA |
expression¥²¶·¬°boolean«¬ºA |
|
| »yªk |
for(
init_Statement
; condition
;
incre_statement
)
¡@¡@ statement |
|
|---|---|---|
| ½d¨Ò |
for( int x=1,y=1; a<=5; x+=1,y+=2)
¡@¡@System.out.println( "x="+x+"y="+y) ; |
int a=1;
for( ;a<=5; ) { ¡@¡@System.out.println( "Hello World" ) ; ¡@¡@ a++; } |
| ³Æµù | init_Statement»P incre_Statement ¬Ò¥i³æ¿W¦s¦b | |
| »yªk |
while(
expression
)
¡@¡@statement |
|
|---|---|---|
| ½d¨Ò |
a=sum=0;
while( a<=100 ) { ¡@¡@a=a+1; ¡@¡@sum=sum+a; } System.out.println( "sum="+sum) ; |
a=sum=0;
while( a<=100 ) ¡@¡@sum=sum+a++; System.out.println( "sum="+sum) ; |
| »yªk |
do
¡@¡@statement while( expression ) ; |
|
|---|---|---|
| ½d¨Ò |
int a=1;
int sum=0; do { ¡@¡@sum=sum+a; ¡@¡@a=a+1; } while( a<= 100) ; System.out.println( "sum="+sum) ; |
int a=1;
int sum=0; do ¡@¡@sum=sum+a++; while( a<= 100) ; System.out.println( "sum="+sum) ; |
¦h»Pbreak©Îcontinue¦X¥Î,¥i¸õ¥X¦h¼h°j°é(multi-level loop)
Ãþ§O¥]§t¤F
[«Ê¸Ë] [×¹¢¦r] class Ãþ§O¦WºÙ [extends ¤÷Ãþ§O] [implements ¤÷¤¶±Ãþ§O]
public class j92070303
{
¡@¡@
//¦¨ûÅܼÆ,¨t²Î·|µ¹ªìÈ(int=0;String="";object=null;char='\u0000')
¡@¡@ static w;
¡@¡@
[«Ê¸Ë] [×¹¢¦r] ¸ê®Æ«¬ºA ÅܼÆ1[=ªìÈ][,ÅܼÆ2,.......];
¡@¡@ string name="peter", blood="A";
¡@¡@ int a;
¡@¡@
//¦¨û¨ç¼Æ
¡@¡@ [«Ê¸Ë] [×¹¢¦r] ¦^¶Ç¸ê®Æ«¬ºA ¨ç¼Æ¦W([¶Ç¤J°Ñ¼Æ¦C])
¡@¡@int max(int a, int b)
¡@¡@{
¡@¡@¡@¡@
//©w¸q°Ï°ìÅܼÆ,¨t²Î¥¼µ¹ªìÈ¡A¬GYª½±µ¤Þ¥Î·|¿ù»~
¡@¡@¡@¡@int x=100 ;
¡@¡@¡@¡@
//¹ê§@°Ï
¡@¡@¡@¡@a=b;
¡@¡@¡@¡@System.out.println(x) ;
//°Ï°ìÅܼÆa
¡@¡@¡@¡@System.out.println(this);
//¦¨ûÅܼÆa
¡@¡@¡@¡@
//return ¦^¶ÇÈ
¡@¡@¡@¡@return a;
¡@¡@}
¡@¡@public static void main(String[] p)
¡@¡@{
¡@¡@¡@¡@System.out.println("Hello") ;
¡@¡@}
¡@¡@¡@...
}
¥i°w¹ïclass»P¦¨û(ÅܼƻP¨ç¼Æ)§@«Ê¸Ë
privateªº¦¨ûÅܼƶȴ£¨Ñ
¦P¤@ÓÃþ§O
ªº¦¨û¨ç¼Æ§@¦s¨ú¡A´Nºâ¬O¦P¤@ÓÀɮצý¤£¦Pclass¤]¤£¥i¦s¨ú¡A¤£¦Ppackage¤£¥i¦s¨ú
privateªº¦¨ûÅܼƦp¦óÅý¨ä¥Lclass¦s¨ú©O? §Q¥Î¦P¤@ÓÃþ§O¥B«Å§i¬°publicªº¦¨û¨ç¼Æ§@¦s¨ú
protected¬°¦b¦PÃþ§O¡A¦Ppackage¤~¥i¦s¨ú¡A¦b¤£¦Ppackage,¤lÃþ§OÄ~©Ó«á¤~¥i¥H¦s¨ú
protected¬°¦b¦P¤@¤÷Ãþ§O(superclass)¤U¤~¥i¥H¦s¨ú
| ¦¨ûÅÜ¼Æ | ª«¥óÅÜ¼Æ | °Ï°ìÅÜ¼Æ |
|---|---|---|
|
|
|
|
public class test {
|
||
·í°õ¦æ java xxx.class
| »¡·í¸ü¤J®É©Î¸Óclass¨ã¦³¹êÅé®É·|¥ß§Y°õ¦æ static block (static initializer) ªº³¡¤À¡A¥B¶È°õ¦æ¤@¦¸ |
class person
{ ¡@static String s; ¡@static ¡@{ ¡@¡@s="hello"; ¡@} } |
| class | ¦¨û¨ç¼Æ |
|---|---|
|
¸Óclass¤£¥i³Q«Ø¥ß¹êÅé(new),
¨äª«¥óÅܼƥi°Ñ¦Ò¤lÃþ§O¹êÅé
abstract class myShape
class myCircle extends myShape
|
|
|
class person
{ ¡@public String name; ¡@public char blood; ¡@Public void set blood( char c ) ¡@{ ¡@¡@blood=c; ¡@} } |
class person
{ ¡@public String name; ¡@public char blood ; ¡@Public void set blood( person this , char blood ) ¡@{ ¡@¡@ this.blood = blood ; ¡@} } |
|
public class j92072711
{ ¡@Public static void main(String para[]) ¡@{ ¡@¡@person Obj1=new person(); ¡@¡@Obj1.setBlood('O'); ¡@¡@person Obj2=new person(); ¡@¡@Obj2.setBlood('A'); ¡@} } |
public class j92072711
{ ¡@Public static void main(String para[]) ¡@{ ¡@¡@person Obj1=new person(); ¡@¡@Obj1.setBlood( Obj1 ,'O' ); ¡@¡@person Obj2=new person(); ¡@¡@Obj2.setBlood( Obj2 ,'A' ); ¡@} } |
|
public class demo
{ ¡@public static void main(String[] p) ¡@{ ¡@¡@throw new RuntimeException; //½sͮɤ£·|¿ù»~ ¡@¡@new FileReader("c:\\ccc"); //½sͮɷ|¿ù»~ ¡@} } |
public class demo
{ ¡@public static void main(String[] p) ¡@{ ¡@¡@throw new RuntimeException //½sͮɤ£·|¿ù»~ ¡@¡@try ¡@¡@{ ¡@¡@¡@new FileReader("c:\\ccc") ¡@¡@} ¡@¡@catch (IOException e) ¡@¡@{ ¡@¡@} ¡@} } |
| »yªk | ½d¨Ò |
|---|---|
|
try
{ ¡@¡@¡@µ{¦¡½X°Ï¶ô } catch(¨Ò¥~Ãþ§O¡@ÅܼÆ) //®·®»1 { ¡@¡@¡@¨Ò¥~³B²z } catch(¨Ò¥~Ãþ§O¡@ÅܼÆ) //®·®»2 { ¡@¡@¡@¨Ò¥~³B²z } finally { ¡@¡@ ¤@©w·|°õ¦æ¨ìªºcode } Y¨Ò¥~¥¼³Q®·®»³B²z«h¥æ¥Ñ¨t²Î |
import java.io.*;
public class test { ¡@public static void main(String[] p) ¡@{ ¡@¡@int x,y=0; ¡@¡@try ¡@¡@{ ¡@¡@¡@if(y==0) ¡@¡@¡@¡@throw new ArithmeticException(); ¡@¡@ //¦Û¦æ±±¨î¨Ò¥~ ¡@¡@¡@else ¡@¡@¡@¡@x=10 / y; ¡@¡@¡@¡@¡@¡@¡@¡@¡@¡@¡@ //new ArithmeticException(¦ý¥Ã»·¤£·|µo¥Í¦¹¨Ò¥~) ¡@¡@¡@System.out.println("x="+x); ¡@¡@¡@FileWriter FW=new FileWriter("c:\\c"); ¡@¡@} ¡@¡@catch(IOException e) ¡@¡@¡@¡@¡@¡@¡@¡@¡@ //e = new ArithmeticException(µL®·®»¨ì ) ¡@¡@{ ¡@¡@¡@System.out.println("IOException e catch"+ e.getMessage() ); ¡@¡@} ¡@¡@catch(ArithmeticException e) ¡@¡@¡@¡@¡@ //e= new ArithmeticException(¸É®»¨ì) ¡@¡@{ ¡@¡@System.out.println("ArithmeticException e catch"+e.getMessage() ); ¡@¡@} ¡@¡@catch(RuntimeException e) ¡@¡@¡@¡@ //ArithmeticExceptionªº¤÷Ãþ§O(»Ýª`·N¤£¯à»P¤lÃþ§O¤¬´«¦ì¸m) ¡@¡@{ ¡@¡@System.out.println("RuntimeException e catch"+ e.getMessage() ); ¡@¡@} ¡@¡@finally ¡@¡@{ ¡@¡@¡@System.out.println("µ´¹ï·|°õ¦æ"); ¡@¡@} ¡@} } |
| »yªk | ½d¨Ò |
|---|---|
|
try
{ ¡@¡@¡@fun1(); } catch(¨Ò¥~Ãþ§O¡@ÅܼÆ) //®·®»1 { ¡@¡@¡@¨Ò¥~³B²z } catch(¨Ò¥~Ãþ§O¡@ÅܼÆ) //®·®»2 { ¡@¡@¡@¨Ò¥~³B²z } finally { ¡@¡@ ¤@©w·|°õ¦æ¨ìªºµ{¦¡½X } Y¨Ò¥~¥¼³Q®·®»³B²z«h¥æ¥Ñ¨t²Î void fun1() throws ¨Ò¥~ªºclass { ¡@¡@fun1()ªºµ{¦¡½X¥i¯à·|¦³¨Ò¥~ } |
import java.io.*
public class test { ¡@¡@static void fun1() throws ArithmeticException //«ü©ú¥á¥X¨Ò¥~ ¡@¡@{ ¡@¡@¡@int x,y=1; ¡@¡@¡@if(y==0) ¡@¡@¡@¡@throw new ArithmeticException(); ¡@ //¦Û¦æ±±¨î¨Ò¥~ ¡@¡@¡@else ¡@¡@¡@¡@x=10 / y; ¡@ //new ArithmeticException(¦ý¥Ã»·¤£·|µo¥Í¦¹¨Ò¥~) ¡@¡@¡@System.out.println("x="+x); ¡@¡@} public static void main(String[] p) ¡@¡@{ ¡@¡@¡@try ¡@¡@¡@{ ¡@¡@¡@¡@fun1(); ¡@¡@¡@} ¡@¡@¡@catch(Exception e) ¡@¡@¡@¡@¡@¡@¡@¡@¡@ //Ãi¤H°µªk ¡@¡@¡@{ ¡@¡@¡@¡@System.out.println("Exception e catch"); ¡@¡@¡@} ¡@¡@¡@finally ¡@¡@¡@{ ¡@¡@¡@¡@System.out.println("µ´¹ï·|°õ¦æ"); ¡@¡@¡@} ¡@¡@} } |
Java ±N I/O ¤À¬°°ª¶¥ I/O »P§C¶¥ I/O ¡A°ª¶¥ I/O ¦b¨Ï¥Î¤W´£¨Ñ§ó¦hªºÅª¼g¤èªk¡A¦pŪ¼g int ¡B double ¡B String ªº¸ê®Æ«¬ºA¡A¦Ó§C¶¥ªº I/O ¤j³¡¥÷¥u´£¨Ñ write ¡B read ªº byte[] ¦s¨ú¡A¦]¬°µ{¦¡¤j³¡¥÷ªº¸ê®Æ³£¬O¥H¦r¦ê©Î¨ä¥¦¥Dn«¬ºA¸ê®Æ¨Ó¹Bºâ¡A¦]¦¹§C¶¥ªº I/O ¦b¨Ï¥Î¤W¤£§Q©óµ{¦¡³]p¡A©Ò¥H Java ±N³\¦h¦n¥Îªº¤èªk¥þ³¡¶°¦X¦¨°ª¶¥ I/O; ´«¨¥¤§¡A§C¶¥ I/O ªº¥Dn¤u§@¬Ot³d»P´CÅé¸ê®Æ§@¦s¨ú¡A°ª¶¥ I/O Ãþ§O¥Dn§@¸ê®Æ«¬ºAªºÂà´«¤Î´£¨Ñ¤@¨Ç¯S®íªº¥\¯à¡C¦b¨Ï¥Î Java I/O ®ÉnÂÔ°Oªº¤@Ó«nì«h¬O¡A¦b«Ø¥ß¤@Ó I/O ¤§«e¥²»Ý¥ý¥Î§C¶¥ I/O Ãþ§O¨Ó¦s¨ú´CÅé¸ê®Æ ( ¦pÀɮשΠpipe) ¡A¤§«á¦A¨Ï¥Î°ª¶¥ I/O ¨Ó±±¨î§C¶¥ I/O Ãþ§Oªº°Ê§@¡A³oºØ¤@¼h¤S¤@¼hªº¬[ºcºÙ I/O Chain ¡C
File ¥i°w¹ï¥Ø¿ý»PÀÉ®×§@¥Î
File fileObj=new File( String directoryPath, String filename );
ÀÉ®×/¥Ø¿ý¦s¦b¡@: renameto(), delete(), lists()
ÀÉ®×/¥Ø¿ý¤£¦s¦b¡@:mkdir(), createnewfile()
¥Hbyte(byte[])¬°Åª¨ú³æ¦ì¡GInputStream, OutputStream
¥Hdouble byte(char[])¬°Åª¨ú³æ¦ì¡GReader,Writer
| Base class | Non Buffer subclass | Buffer subclass |
|---|---|---|
|
InputStream
InputStream ¬O«Å§i±q¬Y¯S©w¨Ó·½Åª¨ú¿é¤JªºÃþ§O
|
FileInputStream, PipedInputStream | BufferedInputStream(InputStream¤lÃþ§O¹êÅé) |
| OutputStream | FileOutputStream, PipedOutputStream | BufferedOutputStream(OuputStream¤lÃþ§O¹êÅé) |
| Reader | FileReader, PipedReader | BufferedReader(Reader¤lÃþ§O¹êÅé) |
| Writer | FileWriter, PipedWriter | BufferedWriter(Writer¤lÃþ§O¹êÅé) |
| FileInputStream,FileOutputStream½d¨Ò | FileReader,FileWriter½d¨Ò |
|---|---|
|
¼ÒÀÀcopy
import java.lang.*;
import java.io.*; public class demo import java.io.*; public class demo { ¡@public static void main(String[] p) ¡@{ ¡@¡@try ¡@¡@{ ¡@¡@¡@byte[] b=new byte[1]; ¡@¡@¡@FileInputStream FIS=new FileInputStream( p[0] ); ¡@¡@¡@FileOutputStream FOS=new FileOutputStream( p[1] ); ¡@ ¡@¡@¡@while(FIS.read(b)!=-1) ¡@¡@¡@¡@FOS.write(b); ¡@¡@¡@FIS.close(); ¡@¡@¡@FOS.close(); ¡@¡@} ¡@¡@catch(Exception e) ¡@¡@{ ¡@¡@¡@System.out.println( e.getMessage() ) ¡@¡@} ¡@} } |
import java.lang.*;
¡@ import java.io.*; public class demo { ¡@public static void main(String[] p) ¡@{ ¡@¡@try ¡@¡@{ ¡@¡@¡@char[] c=new char[1]; ¡@¡@¡@FileReader FR=new FileReader(p[0] ); ¡@¡@¡@FileWriter FW=new FileWriter(p[1] ); ¡@¡@¡@while(FR.read(c)!=-1) ¡@¡@¡@{ ¡@¡@¡@¡@c[0]=Character.toUpperCase(c[0]); ¡@¡@¡@¡@FW.write(c); ¡@¡@¡@} ¡@¡@¡@FR.close(); ¡@¡@¡@FW.close(); ¡@¡@} ¡@¡@catch(Exception e) ¡@¡@{ ¡@¡@¡@System.out.println( e.getMessage() ) ¡@¡@} ¡@} } |
| BufferedInputStream,BufferedOutputStream½d¨Ò | BufferedReader,BufferedWriter½d¨Ò |
|
import java.lang.*;
import java.io.*; public class demo { ¡@public static void main(String[] p) ¡@{ ¡@¡@try ¡@¡@{ ¡@¡@¡@String rStr; ¡@¡@¡@FileReader FR=new FileReader("c:\\config.sys"); ¡@¡@¡@BufferedReader BR=new BufferedReader(FR); ¡@¡@¡@FileWriter FW=new FileWriter("c:\\config.dot"); ¡@¡@¡@BufferedWriter BW=new BufferedWriter(FW); ¡@¡@¡@while(rStr=BR.readline()!=null) ¡@¡@¡@{ ¡@¡@¡@¡@BW.write(rStr,0,rStr.length()); ¡@¡@¡@¡@BW.newLine(); ¡@¡@¡@} ¡@¡@¡@BR.close(); ¡@¡@¡@BW.close(); ¡@¡@¡@FR.close(); ¡@¡@¡@FW.close(); ¡@¡@} ¡@¡@catch(Exception e) ¡@¡@{ ¡@¡@¡@System.out.println( e.getMessage() ) ¡@¡@} ¡@} } |
public class j93110401
{
¡@public static void main ( String[] args )
¡@{
¡@¡@Thread thisThread = Thread.currentThread();
¡@¡@System.out.println( "thisThread.getName()="+thisThread.getName()
) ;
¡@}
}
¦P¨B°Ï¶ô(Synchronized block)
¦P¨B¤èªk(Synchronized method)
| AWT | Swing | |
|---|---|---|
| Class¦W | java.awt.* | javax.swing.* |
| ªí³æ¦W | Frame | JFrame |
| ¤¸¥ó-«ö¶s | Button | JButton |
| ¤¸¥ó-¼ÐÃD | Label | JLable |
| ¤¸¥ó-µe¥¬ | Canvas | |
| ¤¸¥ó-®Ö¨ú¤è¶ô | Checkbox | JCheckBox |
| ¤¸¥ó-³æ¿ï¶s | Choice | JRadioButton |
| ¤¸¥ó-¼ÐÃD | Label | JLable |
| ¤¸¥ó-¦Cªí | List | JList |
| ¤¸¥ó-±²°Ê¶b | Scrollbar | |
|
Ä~©ÓÃö«Y»P
±`¥Î¤èªkÄÝ©Ê |
|
|
| ½d¨Ò |
import java.awt.* ;
public class j92083101 extends Frame
|
import java.awt.*;
// Frame
import javax.swing.*; //JFrame public class j93111103 extends JFrame { //¤¸¥ó°Ï static j93111103 Form1; Button B1; JButton JB1; public j93111103() // ªí³æ & ¤¸¥ó ªì©l¤Æ { // ¥Î this ¸ò¥Î Form1 ³£¬O¦P¤@Óªí³æª«¥ó¹êÅé JPanel ContentPane = (JPanel )this.getContentPane(); //¨ú±o ContentPane ¥Î Container getContentPane() ContentPane.setLayout( null ); // ¨ú®ø¤¸¥ó¦b ContentPane °t¸mªº¤è¦¡ this.setTitle( "first Form" ); //== new Frame( "first Form" ) this.setCursor( new Cursor( Cursor.HAND_CURSOR ) ); // setCursor(Cursor cursor)--> //this.setBackground( Color.RED ); // setBackground(Color c) --> Color.RED == new Color( 255,0,0 ) ContentPane.setBackground( Color.RED ); B1 = new Button( "B1" ); B1.setBounds( 50,50, 60,25 ); ContentPane.add( B1 ); // ¤¸¥ón¥[¤J JFrame ªº ContentPane ¤~¦æ JB1 = new JButton( "JB1" , new ImageIcon( "Tips.gif" ) ); JB1.setBounds( 50,80, 100,100 ); ContentPane.add( JB1 ); // ¤¸¥ón¥[¤J JFrame ªº ContentPane ¤~¦æ this.setBounds( 100,100, 300,300 ); // ³o¸Ìn¥Î JFrame ªº®Ø¬[(this) this.setVisible( true ); // ³o¸Ìn¥Î JFrame ªº®Ø¬[(this) } public static void main( String[] dd ) { Form1 = new j93111103(); } } |
| °ª«×°_ºâ | ±q¼ÐÃD¦CªºÀY¶}©l | ±qµe±³»ºÝ¶}©l |
| WindowListener | WindowAdapter |
|---|---|
|
import java.awt.*;
import java.lang.*; import java.awt.event.*; public class j92083101 extends Frame { ¡@Button B1; ¡@public j92083101() ¡@{ ¡@¡@this.setLayout(null); ¡@¡@B1=new Button("B1"); ¡@¡@B1.setBounds(50,30,60,25); ¡@¡@this.add(B1); ¡@¡@this.addWindowListener(new myWindowListener()); ¡@¡@this.setBackground(Color.red); ¡@¡@this.setBounds(100,100,300,300); ¡@¡@this.setVisible(true); ¡@} ¡@class myWindowListener implements WindowListener ¡@{ ¡@¡@public void windowOpened(WindowEvent e){} ¡@¡@public void windowClosing(WindowEvent e) ¡@¡@{ ¡@¡@¡@System.exit(0); ¡@¡@} ¡@¡@public void windowClosed(WindowEvent e){} ¡@¡@public void windowIconified(WindowEvent e){} ¡@¡@public void windowDeiconified(WindowEvent e){} ¡@¡@public void windowActivated(WindowEvent e){} ¡@¡@public void windowDeactivated(WindowEvent e){} ¡@} ¡@public static void main(String[] args) ¡@{ ¡@¡@j92083101 Form1=new j92083101(); ¡@} } |
¨Ï¥ÎWindowAdapter¥i¥H¤£¥Î¹ê°µ¨S¦³¥Î¨ìªº¤èªk
import java.awt.*;
import java.lang.*; import java.awt.event.*; public class j92083101 extends Frame { ¡@TextField custno; ¡@Button B1; ¡@public j92083101() ¡@{ ¡@¡@this.setLayout(null); ¡@¡@B1=new Button("B1"); ¡@¡@B1.setBounds(50,30,60,25); ¡@¡@this.add(B1); ¡@¡@this.addWindowListener(new myWindowListener()); ¡@¡@this.setBackground(Color.red); ¡@¡@this.setBounds(100,100,300,300); ¡@¡@this.setVisible(true); ¡@} ¡@class myWindowListener extends WindowAdapter ¡@{ ¡@¡@public void windowClosing(WindowEvent e) ¡@¡@{ ¡@¡@¡@System.exit(0); ¡@¡@} ¡@ } ¡@public static void main(String[] args) ¡@{ ¡@¡@j92083101 Form1=new j92083101(); ¡@} } |
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class EchoServer extends Frame implements Runnable
{
¡@Label ipLabel,messLabel;
¡@TextField ip,mess;
¡@Button sndButton;
¡@List messList;
¡@static int portNum = 8888;
//«ü©w¶Ç°e»P±µ¦¬ªºport number
¡@public EchoServer()
//«Øºc¤l:²£¥Íµe±
¡@{
¡@¡@this.setLayout( null );
//¤£¨Ï¥Î Layout Manager
¡@¡@ipLabel = new Label("¥ØªºIP");
¡@¡@ipLabel.setBounds(5,32,45,25);
¡@¡@this.add( ipLabel );
¡@¡@ip = new TextField("127.0.0.1");
¡@¡@ip.setBounds( 50,32,180,25);
¡@¡@this.add( ip );
¡@¡@messLabel = new Label("°T¡@®§");
¡@¡@messLabel.setBounds(5,60,45,25);
¡@¡@this.add( messLabel );
¡@¡@mess = new TextField("Echo test...");
¡@¡@mess.setBounds( 50,60,180,25);
¡@¡@this.add( mess );
¡@¡@sndButton = new Button("¶Ç°e");
¡@¡@sndButton.setBounds( 235,60,50,25);
¡@¡@sndButton.addMouseListener( new myMouseAdapter() );
¡@¡@this.add( sndButton );
¡@¡@messList = new List( 12 );
¡@¡@messList.setBounds( 10,90,280,200);
¡@¡@this.add( messList );
¡@¡@this.addWindowListener(
¡@¡@¡@new WindowAdapter()
¡@¡@¡@{
¡@¡@¡@¡@public void windowClosing(WindowEvent e)
¡@¡@¡@¡@{
¡@¡@¡@¡@¡@System.exit(0);
¡@¡@¡@¡@}
¡@¡@¡@}
¡@¡@);
¡@
setBackground(Color.lightGray);
¡@
this.setTitle("EchoServer ¶Ç°e»P±µ¦¬");
¡@
this.setBounds( 100,100,300,300);
¡@
this.setVisible( true );
}
class myMouseAdapter extends MouseAdapter
// Mouseªº¨Æ¥ó³B²z, ¶È§@click
{
¡@public void mouseClicked(MouseEvent e)
// «ö¤U sndButton «ö¶s
¡@{
// °e
¡@¡@try
¡@¡@{
¡@¡@¡@InetAddress lcIP = InetAddress.getLocalHost();
//¥D¾÷¦WºÙ/IP
¡@¡@¡@String sendStr = lcIP.getHostAddress()+"=>" + mess.getText().trim();
¡@¡@¡@DatagramPacket DP =
¡@¡@¡@¡@¡@¡@new DatagramPacket( sendStr.getBytes(),
¡@¡@¡@¡@¡@¡@sendStr.getBytes().length,
¡@¡@¡@¡@¡@¡@InetAddress.getByName( ip.getText().trim() ),
¡@¡@¡@¡@¡@¡@portNum );
¡@¡@¡@DatagramSocket DS = new DatagramSocket();
¡@¡@¡@DS.send( DP );
¡@¡@¡@DS.close();
¡@¡@¡@messList.add( "¤w°e¥X"+mess.getText().trim() );
¡@¡@}
¡@¡@catch(Exception re){}
¡@}
}
public void run()
//¦h°õ¦æºüªºrun¤èªk
{
¡@try
¡@{
¡@¡@while( true )
¡@¡@{
// ¦¬
¡@¡@¡@byte[] buf = new byte[200];
//³]©w½w½Ä°Ï¤j¤p
¡@¡@¡@DatagramPacket DP = new DatagramPacket( buf , buf.length);
//³]©w±µ¦¬ºÝªºUDP packet
¡@¡@¡@DatagramSocket DS = new DatagramSocket( portNum );
//«Ø¥ß¶Ç°eºÝªºUDP socket
¡@¡@¡@DS.receive( DP );
//±µ¦¬UDP«Ê¥]
¡@¡@¡@DS.close();
¡@¡@¡@messList.add( new String( buf ).trim() );
¡@¡@¡@Thread.sleep(100);