Java 易错点
---恢复内容开始---
1.Arrays.copyOfRange(strs,index1,index2)
包括index1,不包括index2
2.输出String[]的Strings 用Arrays.toString()
3.先 Arrays.sort(); 再 Arrays.binarySearch();
4. Java Math 是静态方法
Math.abs(s);//s的绝对值
Math.pow(a,b);//a^b
Math.sqrt(double);//double^(1/2)
5.构造函数不能加static 关键字
6.static方法中不能用this对象,所以不能调用非static的方法,非static function==this.function
7.overload && override
区别:是否改变方法签名
方法签名=方法名+参数列表
overload方法重载 在一个类中
(1)方法名相同
(2)方法的参数类型、个数、顺序至少有一项不同
(3)方法返回类型可以不同
(4)方法的修饰符可以不同
只有返回类型不同不是重载
override 方法覆盖 在继承时
1)子类重写父类的方法,父类的方法被覆盖,
子类重写父类的类成员变量,父类的类成员变量被隐藏,super可以调用,父类对象再调用时还是自己的变量值
2)final方法在继承时不能被override
import java.awt.*;
public class override {
public void func()
{
System.out.println("super");
}
public static void main(String[] args)
{
override or=new override();
or.func();
override oor=new over();
oor.func();
}
}
class over extends override{
public void func()
{
System.out.println("child");
super.func();
}
}
8. Generic
<1>
public class Name<T>{
public Name(T );//
}
<2>
static <T> void function(T p1, T p2);
<3>
public class Name<T extends Number>
Name<String> n=new Name<String>();//Exception ClassCastException
<4>wildcard 通配符
public int func(List<Number> list){}
List<Integer> l=new ArrayList<Integer.();
func(l);//wrong
right
public int func(List<? extends Number> list){}//Number 为父类,?用来占位
UpperBound Wildcard
instance is subclass of <? extends super>
Lower Bound wildcard
void function(List<? super Integer. list){};
易错点:
1)class C<T,S>
C<int,char> c=new C<int,char>();//C<Integer,Character> c=new C<Integer,Character>();
2) T f=new T();//No instance
9.Exception
1>不在try catch中的:Error,RuntimeException (ArrayIndexOutofBoundsException,OverflowException,NullPointerException,ArithmeticException)
2>exception:try->catch->finally
no exception:try->finally
3>throws void function throws Exception{}
throw throw new INException("");
+s 用于声明,不+s用于动作
4>overriding
class Super throws IOException
class Child extends Super throws Exception///wrong
//Child extends Super throws IOException
5>try{}
catch{}catch{}finally{post(); return value;}
10.IO
1>filtered/buffered/piped streams
2>java.io.file---path in a file system :file and directory
3>directory:
import java.io.*;
public class director {
public static void main(String[] args)
{
try
{
File f=new File("E://");
if(!f.exists())
f.createNewFile();
File[] filelist=f.listFiles();
for(int i=0;i<filelist.length;i++)
System.out.println(filelist[i].getPath());
}catch(IOException e1)
{
System.out.println(e1);
}
}
}
4>Stream
% sequence:FIFO
%Byte Stream:
unit:byte
binary data
InputStream
int read()
OutputStream
void write(int)
File ,Data,ByteArray,Buffered,Object
%Character Stream:
unit:char
text
Reader
int read()
Writer
void writer(int)
File Piped Buffered
%close();
%read();结束判断-1
int
0~2^8-1
%DataInputStream
读取数字 readInt() readshort() readBoolean()
%ObjectInputStream/ObjectOutputStream
%BufferedWriter newLine();
%OutputStream reader flush();
11.多线程
1>public class Mythread extends Thread{
public void run(){}
public static void main(String args[]){
Thread t=new Mythread();
t.start();
}
2>Runnalble interface
public class Mytask implements Runnable{
public void run(){}
public static void main(String[] args){
Thread t=new Thread(new MyTask());
t.start();
}
3>State: New:new Thread(arg)
Running:start()
Wait:waiting()
blocked:sychronized() join()
time waiting:sleep()
4>wait another thread terminated
main thread exit before Mytask Thread
join():wait until the thread terminated
join(long milllis):wait until the thread terminated or wait after millisecond time
try{
Thread t=new Thread(new Mytask());
t.join();//t调用join(),告诉其他线程:等我结束后你们再开始
}catch(InterruptedException e){}
5>wait but not terminated
sleep(long millis)
static method:Thread.sleep(millis)
1s=1000ms
throw InterruptedException
public void run(){
try{
Thead.sleep(1000L);
}catch(InterruptedException e){}}
6>terminated other threads
interrupt()
try{
Thread.sleep(1000L);
}catch(InterruptedException e)
{}//Interrupt() cause receive the exception
Thread t= new Yhread(new Mytask());
t.sleep();
t.interrupt();}
Can be interrupted:blocked sychronized();time waiting sleep()/waiting()+时间参数;
waiting waiting();
interrupted();//return boolean
try{
while(Thread.interrupted()){//static function
Thread.sleep();}
}catch(InterruptedException e){}
7>sychronized
public sychronized long func(){}
wait()
in a sychronized method or statement
realse the lock of the object
running->waiting
until another thread invokes the notify()/notifyAll()
wait(long timeout)
notify();/notifyAll()
notifyAll使所有原来在该对象上等待被notify的线程统统退出wait的状态,变成等 待该对象上的锁,一旦该对象被解锁,他们就会去竞争。
notify只是选择一个wait状态线程进行通知
public sychronized void deposit(long ){
balance +=;
notifyAll();//我用完了,其他的都醒来,该你们竞争他了;}
public sychronized void withdraw()
{
while(条件)//用while,不用if
wait();
}
12.数值范围
boolean | 1 | true/false | Boolean |
char 2*byte | 16 | unicode | Character |
byte | 8 | [-2^7,2^7-1] | Byte |
short 2*byte | 16 | [-2^15,2^15-1] |
Short |
int 4*byte | 32 | [-2^31,2^31-1] | Integer |
long 8*byte | 64 | [-2^63,2^63-1] | Long |
float 4*byte | 32 | -3.4*10^38~3.4*10^38 | Float |
double 8*byte | 64 | -1.7*10^308~1.7*10^308 | Double |
void | Void |
boolean tf=null;//错误
13.命名
首字母:字母,_ ,$
14.SQL
insert,update,delete,query
CRUD:create,read,update,delete
SQLException:try catch
15.GUI
1>只有FlowLayout 中的组件不随容器的大小改变而改变
2>
16.others
1>数组命名:String arr[]=new String[10];/String[] arr=new String[10];
2>String 未初始化输出的是null
3>List mylist=new ArrayList();//ArrayList extends List,所以声明一个父类的对象具体为一个子类的对象
4>Abstract Class 和 Interface 异同 作用:
/////////////////////////////////////////////////////////////////////////////
Abstrat class:1.including abstract methods一定要有
2.不能实例化对象
3.一定要被继承或生效extends/implemented
abstract class是extends,interface是implements
/////////////////////////////////////////////////////////////////////////////
#Interface
区分类型
表示具有功能:克隆,比较,Runnable(can be run in a thread),对象能被序列化
publci interface Comparabale<T>{}
Fields in interface: public static final ;一定要被初始化
implement an interface:1.implement multiple interfaces
class CA implements Comparable<>,Serialization,Cloneable
2.interface中的所有方法都要被实现
/////////////////////////////////////////////////////////////////////////////
Abstract class vs Interface
#1interface能够多继承,abstract class 不能多继承
#2abstract class 能够部分实现,interface 没有实现
5>继承:接口继承,继承类
#1 继承类:构造函数:Subclass的构造函数的第一行代码必须是调用super();,default 构造函数不明写也可
#2 final:类前加final class不能被继承
method前加final method不能被重载
6>overriding
权限:扩大 super是private,subclass 是public,返回类型:缩小
7>多态 :参数列表不同
8>List<? super Integer>Integer的父类:Number,Object,
9>Integer i=new Interger(1);//对象
Integer i=1;//报错
int i=1;//基础变量
---恢复内容结束---
1.Arrays.copyOfRange(strs,index1,index2)
包括index1,不包括index2
2.输出String[]的Strings 用Arrays.toString()
3.先 Arrays.sort(); 再 Arrays.binarySearch();
4. Java Math 是静态方法
Math.abs(s);//s的绝对值
Math.pow(a,b);//a^b
Math.sqrt(double);//double^(1/2)
5.构造函数不能加static 关键字
6.static方法中不能用this对象,所以不能调用非static的方法,非static function==this.function
7.overload && override
区别:是否改变方法签名
方法签名=方法名+参数列表
overload方法重载 在一个类中
(1)方法名相同
(2)方法的参数类型、个数、顺序至少有一项不同
(3)方法返回类型可以不同
(4)方法的修饰符可以不同
只有返回类型不同不是重载
override 方法覆盖 在继承时
1)子类重写父类的方法,父类的方法被覆盖,
子类重写父类的类成员变量,父类的类成员变量被隐藏,super可以调用,父类对象再调用时还是自己的变量值
2)final方法在继承时不能被override
import java.awt.*;
public class override {
public void func()
{
System.out.println("super");
}
public static void main(String[] args)
{
override or=new override();
or.func();
override oor=new over();
oor.func();
}
}
class over extends override{
public void func()
{
System.out.println("child");
super.func();
}
}
8. Generic
<1>
public class Name<T>{
public Name(T );//
}
<2>
static <T> void function(T p1, T p2);
<3>
public class Name<T extends Number>
Name<String> n=new Name<String>();//Exception ClassCastException
<4>wildcard 通配符
public int func(List<Number> list){}
List<Integer> l=new ArrayList<Integer.();
func(l);//wrong
right
public int func(List<? extends Number> list){}//Number 为父类,?用来占位
UpperBound Wildcard
instance is subclass of <? extends super>
Lower Bound wildcard
void function(List<? super Integer. list){};
易错点:
1)class C<T,S>
C<int,char> c=new C<int,char>();//C<Integer,Character> c=new C<Integer,Character>();
2) T f=new T();//No instance
9.Exception
1>不在try catch中的:Error,RuntimeException (ArrayIndexOutofBoundsException,OverflowException,NullPointerException,ArithmeticException)
2>exception:try->catch->finally
no exception:try->finally
3>throws void function throws Exception{}
throw throw new INException("");
+s 用于声明,不+s用于动作
4>overriding
class Super throws IOException
class Child extends Super throws Exception///wrong
//Child extends Super throws IOException
5>try{}
catch{}catch{}finally{post(); return value;}
10.IO
1>filtered/buffered/piped streams
2>java.io.file---path in a file system :file and directory
3>directory:
import java.io.*;
public class director {
public static void main(String[] args)
{
try
{
File f=new File("E://");
if(!f.exists())
f.createNewFile();
File[] filelist=f.listFiles();
for(int i=0;i<filelist.length;i++)
System.out.println(filelist[i].getPath());
}catch(IOException e1)
{
System.out.println(e1);
}
}
}
4>Stream
% sequence:FIFO
%Byte Stream:
unit:byte
binary data
InputStream
int read()
OutputStream
void write(int)
File ,Data,ByteArray,Buffered,Object
%Character Stream:
unit:char
text
Reader
int read()
Writer
void writer(int)
File Piped Buffered
%close();
%read();结束判断-1
int
0~2^8-1
%DataInputStream
读取数字 readInt() readshort() readBoolean()
%ObjectInputStream/ObjectOutputStream
%BufferedWriter newLine();
%OutputStream reader flush();
11.多线程
1>public class Mythread extends Thread{
public void run(){}
public static void main(String args[]){
Thread t=new Mythread();
t.start();
}
2>Runnalble interface
public class Mytask implements Runnable{
public void run(){}
public static void main(String[] args){
Thread t=new Thread(new MyTask());
t.start();
}
3>State: New:new Thread(arg)
Running:start()
Wait:waiting()
blocked:sychronized() join()
time waiting:sleep()
4>wait another thread terminated
main thread exit before Mytask Thread
join():wait until the thread terminated
join(long milllis):wait until the thread terminated or wait after millisecond time
try{
Thread t=new Thread(new Mytask());
t.join();//t调用join(),告诉其他线程:等我结束后你们再开始
}catch(InterruptedException e){}
5>wait but not terminated
sleep(long millis)
static method:Thread.sleep(millis)
1s=1000ms
throw InterruptedException
public void run(){
try{
Thead.sleep(1000L);
}catch(InterruptedException e){}}
6>terminated other threads
interrupt()
try{
Thread.sleep(1000L);
}catch(InterruptedException e)
{}//Interrupt() cause receive the exception
Thread t= new Yhread(new Mytask());
t.sleep();
t.interrupt();}
Can be interrupted:blocked sychronized();time waiting sleep()/waiting()+时间参数;
waiting waiting();
interrupted();//return boolean
try{
while(Thread.interrupted()){//static function
Thread.sleep();}
}catch(InterruptedException e){}
7>sychronized
public sychronized long func(){}
wait()
in a sychronized method or statement
realse the lock of the object
running->waiting
until another thread invokes the notify()/notifyAll()
wait(long timeout)
notify();/notifyAll()
notifyAll使所有原来在该对象上等待被notify的线程统统退出wait的状态,变成等 待该对象上的锁,一旦该对象被解锁,他们就会去竞争。
notify只是选择一个wait状态线程进行通知
public sychronized void deposit(long ){
balance +=;
notifyAll();//我用完了,其他的都醒来,该你们竞争他了;}
public sychronized void withdraw()
{
while(条件)//用while,不用if
wait();
}
12.数值范围
boolean | 1 | true/false | Boolean |
char 2*byte | 16 | unicode | Character |
byte | 8 | [-2^7,2^7-1] | Byte |
short 2*byte | 16 | [-2^15,2^15-1] |
Short |
int 4*byte | 32 | [-2^31,2^31-1] | Integer |
long 8*byte | 64 | [-2^63,2^63-1] | Long |
float 4*byte | 32 | -3.4*10^38~3.4*10^38 | Float |
double 8*byte | 64 | -1.7*10^308~1.7*10^308 | Double |
void | Void |
boolean tf=null;//错误
13.命名
首字母:字母,_ ,$
14.SQL
insert,update,delete,query
CRUD:create,read,update,delete
SQLException:try catch
15.GUI
1>只有FlowLayout 中的组件不随容器的大小改变而改变
2>
16.others
1>数组命名:String arr[]=new String[10];/String[] arr=new String[10];
2>String 未初始化输出的是null
3>List mylist=new ArrayList();//ArrayList extends List,所以声明一个父类的对象具体为一个子类的对象
4>Abstract Class 和 Interface 异同 作用:
/////////////////////////////////////////////////////////////////////////////
Abstrat class:1.including abstract methods一定要有
2.不能实例化对象
3.一定要被继承或生效extends/implemented
abstract class是extends,interface是implements
/////////////////////////////////////////////////////////////////////////////
#Interface
区分类型
表示具有功能:克隆,比较,Runnable(can be run in a thread),对象能被序列化
publci interface Comparabale<T>{}
Fields in interface: public static final ;一定要被初始化
implement an interface:1.implement multiple interfaces
class CA implements Comparable<>,Serialization,Cloneable
2.interface中的所有方法都要被实现
/////////////////////////////////////////////////////////////////////////////
Abstract class vs Interface
#1interface能够多继承,abstract class 不能多继承
#2abstract class 能够部分实现,interface 没有实现
5>继承:接口继承,继承类
#1 继承类:构造函数:Subclass的构造函数的第一行代码必须是调用super();,default 构造函数不明写也可
#2 final:类前加final class不能被继承
method前加final method不能被重载
6>overriding
权限:扩大 super是private,subclass 是public,返回类型:缩小
7>多态 :参数列表不同
8>List<? super Integer>Integer的父类:Number,Object,
9>Integer i=new Interger(1);//对象
Integer i=1;//报错
int i=1;//基础变量
17.collection
1>Array
int[] arr=int[]{1,2,3};
Arrays static function
1.Arrays.fill(arr,1);2.Arrays.sort(arr);3.int Arrays.binarySearch(arr,);4.boolean Arrays.equals(arr1,arr2);5.Arrays.hashCode(arr);
arr.toString();//arr address
arr.length;//int
2>collection
#1 LikedList=interface Queue+interface List
LinkedList ll=new LinkedList();
ll.addFirst(E); ll.addLat(E);
ll.removeFirst(); ll.removeLast(); ll.remove(index);
ll.peekFirst();//getfirst,not remove ll.peek();//返回头元素,ll为空返回null
ll.peekLast();
#2 ArrayList al=new ArrayList();
al.add(E); al.add(index,E); al.remove(E); al.remove(index,E);
al.get(index); al.set(index,E); al.contain(E);
#3 ArrayList vs LinkedList
LinkedList:1.内存中无固定大小
2.add delete insert 效率更高
3.随机访问效率不高
ArrayList:1.随机访问效率高
2.存储空间可能浪费
3.insert delete效率低
4.动态增大数组的大小会造成存储空间未充分利用
#4 collections
java.util.Collectionsall methods are static
Collections.sort(arraylist); Collections.binarySearch(arraylist); Collection.fill(arraylist," ");
#5 Map 无序
put(k,v); get(k); remove(k); contain(k);
keySet();//set<T> values();//set<S> entrySet();//all k v
for(Map.Entry<T,S> m:map.entrySet()){
m.getKey() m.getValue()}
for(T k:map.keySet())
for(S v:map.values())
#6 Iterator
Iterator<T> it=map.keySet().iterator();
while(is.hasNext()) {it.next();}
貌似少了一个socket和http部分,等我考完期末再总结吧