short s1=1,s1=(short)s1+1; //正确
short s1=1,s1=s1+1; //错误
short s1=1,s1+=1; //正确
String a="abc";
String b="abc";
a==b; //这是比较的对象的地址
a.equals(b);
a.equalignorecase(b); //忽略大小写的比较
//异或(^) 与(&&) 或(||) 非(!)
// << >> >>>
Java Applet调用时自动运行init,start,paint,stop,olestroy这5个函数
public class Untitled1 {
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所在的类库
public class Untitled1 {
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.addElement("String" + i);
}
System.out.println(Integer.parseInt(v.elementAt(0).toString()) +
Integer.parseInt(v.elementAt(1).toString()));
v.size();//返回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 < a.length; i++) {
a[i] = new Integer(2 * i + 1);
}
Vector b = new Vector();
for (int i = 0; i < 10; i++)
b.addElement(new Array(i));
for (int i = 0; i < b.size(); i++)
( (Array) b.elementAt(i)).print();
}
}
class DataOnly {
int i;
float f;
boolean b;
public static void main(String args[]) {
DataOnly d = new DataOnly();
d.i = 47;
d.f = 1.1f;
d.b = true;
System.out.println(d.i);
}
}
BitSet
import java.util.*;
Stack
import java.util.*;
public class Untitled1 {
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 < a.length; i++) {
a[i] = new Integer(i);
sta.push(a[i]);
if (a[i].intValue() % 2 != 0) {
bs.set(i);
}
System.out.print(bs.get(i) ? "1" : "0");
}
System.out.println("/n");
System.out.println("size=" + sta.size());
while (!sta.isEmpty()) {
System.out.println( ( (Integer) sta.pop()).intValue());
}
}
}
Hashtable
import java.util.*;
class Untitled1 {
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=(Float)ht.get("pi");
System.out.println(a.intValue());
System.out.println(b.longValue());
System.out.println(c.floatValue());
}
}
小综合
import java.util.*;
public class Untitled1 {
public static void main(String[] args) {
int i;
Vector vec = new Vector();
Hashtable ht = new Hashtable();
ArrayList al = new ArrayList();
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,LinkedList ll)
{
int i;
int flag=0;
for(i=0;i<ll.size() ;i++)
{
MyInt temp=(MyInt)ll.get(i);
if (temp.getName().equals(a))
{
flag=1;
System.out.println(temp.getValue());
}
}
if (flag==0)
System.out.println("Can't find the value refer to /"" + a +
"/" in the LindedList");
}
public void findInArrayList(String a, ArrayList al) {
int i;
int flag = 0;
for (i = 0; i < al.size(); i++) {
MyInt temp = (MyInt) al.get(i);
if (temp.getName().equals(a)) {
flag = 1;
System.out.println(temp.getValue());
}
}
if (flag == 0) {
System.out.println("Can't find the value refer to /"" + a +
"/" in the ArrayList");
}
}
public void findInHashSet(String a, HashSet hs) {
int i;
//Iterator it=hs.iterator();
// MyInt myint=(MyInt)it.next();
int flag = 0;
Iterator it = hs.iterator();
for (i = 0; i < hs.size(); i++) {
MyInt temp = (MyInt) it.next();
if (temp.getName().equals(a)) {
flag = 1;
System.out.println(temp.getValue());
}
}
if (flag == 0) {
System.out.println("Can't find the value refer to /"" + a +
"/" in the HashSet");
}
}
public void findInVector(String a, Vector vec) {
int i;
int flag = 0;
for (i = 0; i < vec.size(); i++) { //查找vec中的"int3"
// obj;
MyInt obj = (MyInt) vec.get(i);
if ( (obj.getName()).equals(a)) {
flag = 1;
System.out.println(obj.getValue());
}
}
if (flag == 0) {
System.out.println("Can't find the value refer to /"" + a +
"/" in the Vector");
}
}
public void findInHashtable(String a, Hashtable ht) {
if (ht.get(a) != null) {
System.out.println( ( (Integer) ht.get(a)).intValue());
}
else {
System.out.println("Can't find the value refer to /"" + a +
"/" in the Hashtable");
}
}
}
class MyInt {
private int Value;
private String Name;
public void setName(String Name) {
this.Name = Name;
}
public String getName() {
return Name;
}
public void setValue(int Value) {
this.Value = Value;
}
public int getValue() {
return Value;
}
}
错误处理
import java.util.*;
public class Untitled1 {
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 Untitled1 {
public static void main(String[] args) {
Untitled1 u=new Untitled1();
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 sssssss(int i) throws MyException, Exception {
//System.out.println(5/0);
if (i == 0) {
throw new MyException("不能为零", 1002);
}
else {
throw new Exception("i<>0");
}
}
/*catch (IOException e) {
System.out.println(e.toString());
System.out.println(e.getMessage());
e.printStackTrace();
}*/
}
class MyException
extends 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;
}
}
//IO
import java.io.*;
public class Untitled1 {
public static void main(String[] args) {
MyListFile nf = new MyListFile();
nf.listFile();
}
}
class MyListFile {
public void listFile() {
File path = new File("d://"); //path.getParent获取上一目录名,path.getName获取当前目录名
String[] strArray;
strArray = path.list();
for (int i = 0; i < strArray.length; i++) {
System.out.println(strArray[i]);
}
}
}
import java.io.*;
public class Untitled1 {
public static void main(String[] args) {
ReadTxtFile rtf = new ReadTxtFile();
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); 现输现读
//BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 这样可以用readLine()
//FileInputStream in=new FileInputStream("d:/1.txt");
//BufferedInputStream in=new BufferedInputStream(new FileInputStream("d://1.txt"));
//可以这样读中文 BufferedReader in = new BufferedReader(new FileReader("d://1.txt"));
}
catch (FileNotFoundException ex1) {
}
int b;
try {
while((b=in.read() )!=-1)
System.out.println((char)b); //readLine()则判断是否返回为null
}
catch (IOException ex) {
}
}
}
写文件
import java.io.*;
public class Untitled1 {
public static void main(String[] args) {
WriteTxtFile wtf = new WriteTxtFile();
wtf.writeFile();
}
}
class WriteTxtFile {
public void writeFile() {
PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter("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(new FileWriter("d://new.txt"));
out.println(in.readLine());
in.close();
}
catch (IOException ex) {
}
out.close();
}
}
Zip
import java.util.zip.*;
import java.io.*;
public class Untitled1 {
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();
}
}
}
多线程
//此例可用内部类、外部类、Runnable
import 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.CENTER);
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) {
try {
Thread.currentThread().sleep(100);
}
catch (InterruptedException e) {}
if (runFlag) {
t.setText(Integer.toString(count++));
}
}
}
}
//Runnable做法
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 = true;
public void init() {
add(t);
start.addActionListener(new StartL());
add(start);
onOff.addActionListener(new OnOffL());
add(onOff);
}
public void go()
{
Thread thrd=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()) {
( (Adder) vec.firstElement()).add();
vec.remove(0);
}
}
}
}
}
public class Untitled1 {
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() { //此时会死锁 ,方法级的同步
while (true) {
i1++;
i2++; //语句同步synchronized(this){i1++;i2++;}
}
}
public synchronized void valueOfI() {
while (true) {
if (i1 != i2) {
System.out.println("i1=" + i1 + ",i2= " + i2);
}
}
}
}
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() { //隐含类
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.*; //前面一个例子的优化
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 (true) {
{
if (terminer) {
break;
}
synchronized (this) {
if (!vec.isEmpty()) {
( (Adder) vec.firstElement()).add();
vec.remove(0);
}
else {
try {
wait();
}
catch (Exception ex) {
}
}
}
}
}
}
}
public class Untitled1 {
public static void main(String args[]) {
myThread myt = new myThread();
for (int i = 0; i < 30000; i++) {
Adder add = new Adder(i, 1);
myt.push(add);
}
try {
myt.sleep(100);
}
catch (Exception ex) {
}
myt.setTerminer();
System.out.println("end");
}
}
//网络编程
客户端简单写法
import java.net.*;
public class Untitled1{
public static void main(String args[]){
try {
Socket s = new Socket("192.168.0.6", 3108);
System.out.println(s) ;
}
catch (Exception ex) {
}
}
}
服务器端写法
ServerSocket s = new ServerSocket(8000);
Socket sock=s.accept() ;
System.out.println(sock) ;
简单传递消息
//client
import 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)
{
}
}
}
//server
import java.io.*;
import java.net.*;
public class Untitled2 {
public static void main(String args[]) {
try {
ServerSocket s = new ServerSocket(4000);
Socket sock = s.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(sock.
getInputStream()));
System.out.print(in.readLine());
}
catch (Exception ex) {
}
}
}
四种访问xml的方法 dom,jdom,domgj,sax