Short S1 = 1, S1 = (Short) S1 1; // Correct SHORT S1 = 1, S1 = S1 1; // Error Short S1 = 1, S1 = 1; // Correct
String a = "abc"; string b = "abc"; a == b; // This is the address of the comparison object a.Equals (b); a.equalignoreCase (b); // ignore the comparison of cases
// Different or (^) and (&&) or (||) is not (!)
// << >> >>>
5 functions that automatically run when Java Applet calls, OLESTROY
Public class untricled1 {public static void main (string args []) {integer [] a = new integer [] {new integer (1), new integer (2), new integer (3), new integer (4)}; INT i; for (i = 0; I <4; i ) system.out.println (a [i] .tostring ());}}
Import java.util. *; // vector class library
Public class untricled1 {public static void main (string args []) {Vector v = new vector (); int i; for (i = 1; i <3; i ) {v.addelement (new integer (i)); } For (i = 1; i <3; i ) {v.addeElement ("string" i);} system.out.println (Integer.Parseint (v.elementat (0) .tostring ()) Integer. PARSEINT (V.Elementat (1)); v.size (); // Returns Int v.remove (0); //v.remove (1); //v.remove (2); //v.remove(3); system.out.println (v.elementat (0) .tostring ());}}
Import java.util.vector;
Public class array {int Num; public array (int num) {this.Num = Num;} void print () {system.out.println ("Integer (" Num ")");} public static void main (String Args []) {INTEGER [] a = new integer [5]; for (int i = 0; i Public static void main (string args []) {dataonly d = new data iONLY (); D. = 47; D.f = 1.1F; D.B = true; system.out.println (d.i);}} Bitsetimport java.util. *; Stackimport java.util. *; Public class united1 {public static void main (string "args) {stack sta = new stack (); bitset bs = new bitset (); int i; integer [] a = new integer [10]; for (i = 0 i HashtableImport java.util. *; Class united1 {public static void main (string [] args) {hashtable ht = new hashtable (); ht.put ("one", new integer (1)); HT.PUT ("long", new long (999999999) Ht.Put ("Pi", New Float (3.1415926)); Integer A = (Integer) Ht.Get ("one"); long b = (long) ht.get ("long"); float c = Ht.get ("pi"); system.out.println (a.intValue ()); system.out.println (B.longValue ()); system.out.println (c.floatvalue ()) }} Small integrated import java.util. *; Public class untric = new vector (); arrival hand ht = new hashtable (); haSHSet HS = New hasht (); hashset hs = new arrivallist (); hashset hs = new hashset (); LinkedList ll = new linkedlist (); for (i = 0; i <10; i ) {Myint Myint = new myint (); al.add (myint); hs.add (myint); ll.add (MyInt); Myint.setname ("int" i); Myint.SetValue (i); vec.add (myint); ht.put (MyInt.getName (), new integer (Myint.getValue ()));} TestSearch TestSearch = new testSearch (); testSearch.findInHashtable ( "int3", ht); testSearch.findInHashtable ( "intdfsdf", ht); testSearch.findInVector ( "int5", vec); testSearch.findInVector ( "intfsdfsdf", vec); testSearch .findInLinkedList ( "int8", ll); testSearch.findInLinkedList ( "intfdsg", ll); testSearch.findInHashSet ( "int6", hs); testSearch.findInHashSet ( "intfsdfsdf", hs); testSearch.findInArrayList ( "int2" , Al); TestSearch.findinarrayList ("intfsdgsdg", Al }} Class testsearch {public void FindinLinkedList (string a, limitedlist ll) {INT i; int flag = 0; for (i = 0; i Public void findinvector (string a, vector vec) {INT i; int flag = 0; for (i = 0; i Public string getname () {return name;} Public void setvalue (int value) {this.value = value;} Public int getValue () {return value;}} Error handling import java.util. *; Public class untricled1 {public static void main (string [] args) {try {system.out.println (5/0);} catch (exception e) {system.out.println (E.getMessage ());}} } Import java.util. *; import java.io ioException; // for ioException, no use for this Public class untricted1 {public static void main (string [] args) {untitled1 u = new unTID1 (); try {u.bb ();} catch (exception ex) {ex.printstacktrace ();}} public void BB "THROWS EXCEPTION {aa aa = new aa (); try {aa.sssssss (0);} catch (myexception e) {system.out.println (E.getMessage ()); system.out.println (E.TOSTRING ()); System.out.println (E.GeterRcode ()); throw new exception ("from catch");} catch (exception e) {system.out.println (E.getMessage ()); system.out .println (e.tostring ());} finally {system.out.println ("finally");}}} Class aa {public void sssss (int i) throws myexception, Exception {//system.out.println 5/0); if (i == 0) {throw new myException ("cannot be zero", 1002);} else {throw new exception ("i <> 0");}} / * Catch (IOException E) {system.out.println (e.tostring ()); system.out.println (E.getMessage ()); E.PrintStackTrace ();} * /} Class myexception extens exception {private int errcode = 1002; public int geterrcode () {system.out.print ("ErrorCode:"); Return Errcode;} Public myexception () {} Public myException (String Msg, int errcode) {super (msg); this.ERRCODE = Errcode;}} // IOIMPORT JAVA.IO. *; Public class untricled1 {public static void main (string [] args) {mylistfile nf = new mylistfile (); nf.listfile ();}} Class myListfile () {file path = new file ("d: //"); //path.getParent Gets the last directory name, Path.getName Gets the current directory name string [] stratay; strRay = path. List (); for (int i = 0; i Public class united1 {public static void main (string [] args) {readtxtFile RTF = new readxtFile (); rtf.readfile ();}} class ReadTxtFile {public void readFile () {DataInputStream in = null; try {in = new DataInputStream (new FileInputStream ( "d: //1.txt")); // BufferedInputStream in = new BufferedInputStream (System.in); Online reading the output current // BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); this () // FileInputStream in = new FileInputStream with readLine ( "d: /1.txt"); // BufferedInputStream in = new BufferedInputStream (New FileInputStream ("D: //1.txt"))); // can read Chinese BufferedReader in = New FileReader ("D: //1.txt");} Catch (FilenotFoundException EX1) {} Int b; Try {while ((b = in.read ())! = - 1) System.out.Println ((char) b); // readline () determines whether return to null} catches (ooException ex) {} } Write file import java.io. *; Public class united1 {public static void main (string [] args) {writetxtFile WTF = new WRITETXTFILE (); wtf.writefile ();}} Class WritetXTfile {public void writefile () { PrintWriter out = null; try {out = new printwriter ("D: //1.txt");} catch (ioException ex) {} out.print ("fdsg"); out.close (); } Import java.io. *; public class Untitled1 {public static void main (String [] args) {WriteTxtFile wtf = new WriteTxtFile (); wtf.writeFile ();}} class WriteTxtFile {public void writeFile () {BufferedReader in = new BufferedReader (new InputStreamReader (System .in); printwriter out = null; try {out = new printwriter ("D: //new.txt"); out.println (in.readline ()); in.close ();} Catch (IOException EX) {} out.close ();}} Zipimport java.util.zip. *; Import java.io. *; Public class untricled1 {public static void main (string args []) {zipfileControl zfc = new zipfileControl (); zfc.zipfileWrite (); zfc.zipfileread ();}} class ZipFileControl {public void zipFileWrite () {try {ZipOutputStream out = new ZipOutputStream (new FileOutputStream ( "e: //abc.zip")); BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); ZipEntry fileInZip = New Zipentry ("1.txt"); OUT.PUTNEXTENTRY (fileinzip); int b; while ((b = in.read ())! = -1) {Out.write ((char) b);} OUT. CLOSE ();} catch (exception e) {E.PrintStackTrace ();}} public void zipFileRead () {try {ZipInputStream in = new ZipInputStream (new FileInputStream ( "e: //abc.zip")); ZipEntry fileInZip; fileInZip = in.getNextEntry (); System.out.println (fileInZip.getName ( ))); System.out.println ((char) in.read ());} catch (exception e) {E.PrintStackTrace ();}}} Multi-thread // This example can be used inside, external class, runnableImport java.awt. *; Import java.awt.event. *; Import java.applet. *; public class Untitled1 extends Applet {private int count = 0; private Button onOff = new Button ( "Toggle"), start = new Button ( "Start"); private TextField t = new TextField (10); private boolean runFlag = true; Public void init () {add (t); start.addActionListener (new startl ()); add (start); onoff.addactionListener (new onoffl ()); add (onoff);} mythread mythread = new mythread (); Class Startl Implements ActionListener {Public Void ActionPerformed (ActionEvent E) {Mythread.SettextField (T); Mythread.SetCount (0); mythread.start ();}} Class Onoffl Implements ActionListener {Public Void ActionPerformed (ActionEvent E) {Runflag =! Runflag; Mythread.Setflag (Runflag);}} public static void main (String [] args) {Untitled1 applet = new Untitled1 (); Frame aFrame = new Frame ( "Counter1"); aFrame.addWindowListener (new WindowAdapter () {public void windowClosing (WindowEvent e) {System.exit (0);}}); AFRAME.Add (applet, borderlayout.ceter); Aframe.SetSize (300, 200); applet.init (); applet.Start (); Aframe.SetVisible (TRUE);}} Class Mythread Extends Thread {Private Boolean Runflag = true; private int count = 0; private textfield t; public void setflag (Boolean Runflag) {this.Runflag = runflag;} Public void settextfield (Textfield T) {this.t = t;} Public void setcount (int count) {this.count = count;} Public void Run () {while (TRUE) {THREAD.CURRENTTHREAD (). Sleep (100);} Catch (InterruptedException E) {} if (Runflag) {T.Settext (Integer.tostring (Count ));} }}} // Runnable Practice Package test; import java.awt. *; Import java.awt.event. *; Import java.applet. *; public class Counter1 extends Applet implements Runnable {private int count = 0; private Button onOff = new Button ( "Toggle"), start = new Button ( "Start"); private TextField t = new TextField (10); private boolean runFlag = PUBLIC VOID INIT () {add (t); start.addactionListener (new startl ()); add (start); onoff.addactionListener (new onoffl ()); add (onoff);} public void Go () { Thread twrd = new thread (this); thrd.start ();} public void run () {//; while (true) {try {thread.currentthread (). Sleep (100);} catch (interruptedException E) { } if (runFlag) t.setText (Integer.toString (count ));}} class StartL implements ActionListener {public void actionPerformed (ActionEvent e) {go ();}} class OnOffL implements ActionListener {public void actionPerformed (ActionEvent e) {Runflag =! Runflag;}} public static void main (string [] args) {counter1 applet = new counter1 (); Frame aFrame = new Frame ( "Counter1"); aFrame.addWindowListener (new WindowAdapter () {public void windowClosing (WindowEvent e) {System.exit (0);}}); aFrame.add (applet, BorderLayout.CENTER); Aframe.setsize (300, 200); applet.init (); applet.Start (); Aframe.SetVisible (TRUE);}} Import java.util. *; Class adder {Int a, b; adder (int A, int b) {this.a = a; this.b = b;} public void add () {system.out.println (this.a this.b) }} Class mythread extends thread {vector vec = new vector (); mythread () {this.start (); Void Push (Adder a) {vec.addelement (a); Public void Run () {while (True) {for (int i = 0; i ) {while (! vec.isempty ()) {() VEC.FIRSTELEMENT ()). Add (); vec.remove (0);}}}}}}} Public class untricled1 {public static void main (string args []) {mythread myt = new mythread (); for (int i = 0; i <10000; i ) {adder add = new adder (i, 1); MYT. Push (add);} system.out.println ("end");}} import java.util *;. public class SycTest {int i1 = 0; int i2 = 0; Thread1 thrd1 = new Thread1 (); Thread2 thrd2 = new Thread2 (); class Thread1 extends Thread {public synchronized void run () {/ / At this time, the method is closed, the method level is synchronized while (TRUE) {i1 ; I2 ; // statement synchronization synchronized (this) {i1 ; i2 ;}}} public synchronized void valueofi () {if (true) {IF I1! = i2) {system.out.println ("i1 =" i1 ", i2 =" i2);}}}} Class Thread2 extends thread}} Class Thread2 Extends thread {public void run () {THRD1.VALUEOFI () } Public static void main (string args []) {syctest u = new syctest (); u.thrd1.start (); u.THRD2.Start ();}} public class Untitled1 {public static void main (String [] args) {final Object resource1 = "resource1"; final Object resource2 = "resource2"; final Object resource3 = "resource3"; Thread t1 = new Thread () {// implicit containing class public void run () {synchronized (resource1) {System.out.println ( "Thread 1: locked resource 1"); try {Thread.sleep (50);} catch (InterruptedException e) {} synchronized (resource2) {//System.out.println ("thread 1: Locked Resource 2 "); System.out.Println (" Get Resource 2 ");}}}}; thread t2 = new thread () {public void Run () {synchronized (resource2) {System.out.println ( "Thread 2: locked resource 2"); try {Thread.sleep (50);} catch (InterruptedException e) {} synchronized (resource3) {System.out.println ( "Get Resource 3");}}}}; thread t3 = new Thread () {public void run () {synchronized (resource3) {System.out.println ( "Thread 3: locked resource 3"); try {Thread.sleep (50);} catch (InterruptedException e) {} synchronized ( Resource1) {system.out.println ("Get Resource 1");}}}}; t1.start (); t2.start (); t3.start ();}} Thrd1.SetPriority (THRD1.MAX_PRIORITY); Import java.util. *; // Optimization of an example in front Class adder {Int A, B; Adder (int A, int b) {this.a = a; this.b = b;} Public void add () {system.out.println (this.a this.b);}} class myThread extends Thread {boolean terminer = false; Vector vec = new Vector (); myThread () {this.start ();} public void push (Adder a) {synchronized (this) {vec.addElement (a); notify (); Public void setterminer () {terminer = true; Public void Run () {while {{{{{{}} {if (!) {if (! vec.isempty ()) {(()) {(()) {() .firstlement ()). Add (); Vec.remove (0);} else {Try {Wait ();} catch (exception ex) {}}}}}}}}}}}}}}}}}}}}} } Public class untricled1 {public static void main (string args []) {mythread myt = new mythread (); for (int i = 0; i <30000; i ) {add {add {adder add = new adder (i, 1); myt. Push (add);} Try {myt.sleep (100);} catch (exception ex) {} myt.setterminer (); system.out.println ("end");}} // Network programming client Simple Writing Import Java.net. *; Public class untricled1 {public static void main (string args []) {Try {socket s = new socket ("192.168.0.6", 3108); system.out.println (s);} catch (exception ex) {}}} } The server is written by ServerSocket S = New Serversocket (8000); socket sock = s.accept (); system.out.println (SOCK); Simple delivery message // clientimport java.net. *; Import java.io. *; public class Untitled1 {public static void main (String args []) {try {Socket socket1 = new Socket ( "192.168.0.4", 4000); PrintWriter out = new PrintWriter (new BufferedWriter (new OutputStreamWriter (socket1.getOutputStream ()) ), true); out.println ("Hello");} catCH (Exception E) {}}} // serverimport java.io. *; import java.net. *; public class untricled2 {public static void main (String Args []) { Try {Serversocket S = New Serversocket (4000); socket Sock = S.Accept (); BufferedReader IN = New BufferedReader (Sock. GetInputStream ()); system.out.print (in.readline ());} catch (exception ex) {} } Four ways to access XML DOM, JDOM, DOMGJ, SAX