javaSE复习总结2

1、抽象类(共性功能)

  抽象类不一定有抽象方法,但是有抽象方法的一定是抽象类

  抽象类的子类要么是抽象类要么重写抽象类中的所有方法

  抽象类的成员特点:

    成员变量:可以是变量也可以是常量

    成员方法:可以有抽象方法(限定子类必须完成某些动作)  

          也可以有非抽象方法(提高代码的复用性)

    构造方法:有构造方法但是不能实例化(构造方法的作用是用于子类访问父类数据的初始化)

 

  abstract不能与以下共存:

    private:抽象的需要子类重写其中的方法,但是私有的不允许重写

    final:终态的不允许被重写

    static:没有意义(静态随着类的加载而加载,但是abstract要重写之后才有实际意义,并且abstract中的方法是没有方法体的,静态可以通过类名调用,但是在abstract中完全没有意义)

 

  如果一个类中没有抽象方法却定义为抽象类:为的是不让创建对象

2、接口(interface)(扩展功能)

  接口的子类要么是抽象类,要么事重写接口中的所有方法

  接口中的成员特点:

    成员变量:只能是常量,默认修饰符为public static final

    成员方法:只能是抽象方法,默认修饰符为:public abstract

    构造方法:接口中没有构造方法,接口主要是扩展功能的,没有具体存在

 

3、类与类:只能单继承,但是可以多重继承

  类与接口:实现关系,可以单实现也可以多实现,还可以在继承一个类的同时实现多个接口

  接口与接口:继承关系,可以单继承也可以多继承。

4、返回值问题:

  基本类型:返回什么就用什么接收

  引用类型:

    类:返回的是该类的对象

    抽象类:返回的是该类的子类对象

    接口:返回的是该接口的子类对象

 

5、局部内部类访问局部变量:

  必须被final修饰:因为局部变量会随着方法的调用结束而消失,这时候局部对象并没有立刻从堆内存中消失,还要使用那个变量。为了让数据能够继续使用,所用要用final修饰,这样在堆内存中存储的实际是一个常量值。

 

6、Object类的成员方法:

  public int hashCode():返回该对象的哈希码值(根据对象的地址来计算)。不同对象的哈希码值一般不同,同一个对象的哈希码值一定相同

  public final Class getClass():返回此Object的运行时的类

  public String toString():返回该对象的字符串表示

  protected void finalize():当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法

 

7、Scanner类的成员方法

  hasNextXxx():判断是否有下一个输入项

  nextXxx:获取下一个输入项

 

8、String类(public final class String

   String s="hello";此时s也是一个对象

   字符串是常量,它的值在创建之后不能更改

    String s ="hello";

    s+= world;

    syso(s);//输出的是helloworld

要记着那幅图!!!!

String s 首先进入栈中,然后去方法区的字符串常量池中寻找hello,(字符串的直接赋值方式是先到字符串的常量池中去寻找,如果有就直接返回,如果没有就创建并返回)。此时得到地址值为0001.然后进入了字符串常量池中找world,没有便创建地址为0002.此时hello与world进行拼接,并不是直接在hello上面进行拼接,而是重新开了一个地址0003在上面为helloworld。最后将地址0003赋值给变量s,此时输出的是helloworld。

字符串一旦被赋值就不能改变指的是hello和world的值一旦赋值就不能改变,而不是变量s不能改变。

 

String类的转换功能:

  byte[] getBytes():将一个字符串转换为字节数组

  char[] toCharArray():  将此字符串转换为一个新的字符数组。

 

9、StringBuffer:同步的,数据安全效率低

  添加:public StringBuffer append(String str)

  插入:public StringBuffer insert(int offeset,String str)

  删除:delete

  反转:reverse

 

10、StringBuilder:不同步,数据不安全,效率高

 

  String 和StringBuffer和  StringBuilder的区别:

  1)String是内容不可变的,其他两个是内容可变的

  2)StringBuffer:同步的,数据安全效率低;StringBuilder:不同步,数据不安全,效率高

 

  StringBuffer和数组的区别:

    两者都可以看作一个容器,装其他数据。StringBuffer的数据始终是一个字符串数据

数组可以存放多种数据,但是必须是同一种数据类型的

 

11、Arrays:针对数组操作的工具类

  toString:回指定数组内容的字符串表示形式。

  sort:对指定的数组按数字升序进行排序。

  binarySearch: 使用二分搜索法来搜索指定的数组,以获得指定的值。如果它包含在数组中,则返回搜索键的索引;否则返回 (-(插入点) - 1)插入点 被定义为将键插入数组的那一点:即第一个大于此键的元素索引,如果数组中的所有元素都小于指定的键,则为 a.length。注意,这保证了当且仅当此键被找到时,返回的值将 >= 0。

 

12、自动装箱拆箱:

Integer x = new Interger(4);

可以直接写成Integer x=4;//自动装箱(基本类型到引用类型)------------源码为:Integer x=Integer.valueOf(4);

x=x+5;//自动拆箱(引用类型到基本类型),再自动装箱    ---------------源码为:x=Interger.valueOf(x.intValue()+200);

syso("x"+x);-----------------------syso((new StringBuilder("x")).append(x).toString()); 

 

 valueOf(int i):返回一个表示指定的 int 值的 Integer 实例。

intValue():int 类型返回该 Integer 的值

 

注意:在使用时如果 int x=null;则在使用时代码就会报空指针异常。因为源码中x会调方法,而null不能调用方法。

 

13、Date类:类 Date 表示特定的瞬间,精确到毫秒。

  Date d = new Date();返回当前时间

   long time = System.currentTimeMillis();返回当前时间的毫秒值

 

14、DateFormat类:DateFormat 是日期/时间格式化子类的抽象类,

  public final String format(Date date) 将一个 Date 格式化为日期/时间字符串。 

15、SimpleDateFormat:SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类。其父类是DateFormat

  Date d = new Date();创建日期对象  --------------得到当前时间

  SimpleDateFormat sdf = new SimpleDateFormat();建立默认格式化对象--------------------确定需要格式化的样式

  String s = sdf.format(d);-----------------格式化

 

16、集合:

Collection(集合的接口)(单列的)

-------List(接口)(有序,允许重复元素)

-----------ArrayList:底层是数据结构是数组,增删慢,查询快-------------线程不安全,效率高

-----------LinkedList:底层是数据结构是链表,增删快,查询慢------------线程不安全,效率高

-----------Vector:底层是数据结构是数组,增删慢,查询快------------线程安全,效率低

-------Set(接口)(唯一(不包含重复元素))

-----------TreeSet(有两种排序方法,具体取决于使用哪种构造方法)

TreeSet保证元素的唯一性:底层数据结构是红黑树(红黑树是一种自平衡的二叉树)

-----------HashSet:不保证元素的迭代顺序----------------如何保证元素的唯一性:底层数据结构是哈希表(元素是链表的数组),哈希表依赖于哈希值存储。添加功能的底层依赖两个方法:hashCode-----equals

 

一种是使用元素的自然顺序对元素进行排序:(无参构造:构造一个新的空set,该set根据元素的自然顺序进行排序)----元素具备比较性

  一个类要想能够进行自然排序,必须实现自然排序接口

public class Student implements Comparable<Studnet>{

  private int age;

  private String name;

  getX();//学生类的get  和 set方法

  setX();

public int compareTo(Studnet s){

//主要条件 按年龄从小到大排序

int num=this.age - s.age;

//自己分析次要条件 

int num2 = num== 0 ? this.name.compareTo(s.name) : num ;

return num;

}

}

 一种是根据创建set时提供的Comparator进行排序(带参构造:构造一个新的空TreeSet,它根据指定的比较器进行排序)------集合具备比较性

public class Student{

  private int age;

  private String name;

  getX();//学生类的get  和 set方法

  setX();

}

main(){

// 如果一个方法的参数是接口,那么真正要的是接口的实现类的对象
// 而匿名内部类就可以实现这个东西

  TreeSet<Student>ts = new TreeSet<Student>(new Comparator<Student>(){ 

  public int compare(Studnet s1,Stuent s2){

  int num=s1.age - s2.age;

  int num2 = num== 0 ? s1.name.compareTo(s2.name) : num ;

  return num;

}

});

//创建元素

//添加元素

//遍历

}

 

 

Collection接口的成员方法:

  Object[] toArray();将集合转化为数组

  Iterator iterator():迭代器,集合的专用遍历方式

---------------------------hasNext()

---------------------------next()

    Iterator it  = c.iterator();

    while(it.hasNext()){

      String s = (String)it.next();

      syso(s);

    }

 

List集合除了可以用迭代器遍历之外,还有一种特有的遍历方式:

  List list = new ArrayList();

  for(int x=0;x<list.size();x++){

    String s = (String)list.get(x);

    syso(s);

   }

 

 

 

Map(双列接口,其键是唯一的)

----------------HashMap:键是哈希表结构,可以保证键的唯一性

------------------------LinkedHashMap:Map接口的哈希表和链表实现,具有可预知的迭代顺序

---------------TreeMap:键是红黑树结构,可以保证键的排序和唯一性

Map集合的遍历:

  Map<String,String> map = new HashMap<String,String>();

  map.put("a","aa");

  map.put("b","bb");

 1) 根据键找值:

    Set<String> set = map.keySet();

    for(String key : set){

      String value = map.get(key);

      syso(key+"---"+value);

    }

  根据键值对对象找键和值

  Set<Map.Entry<String,String>> set2 = map.entrySet();

  for(Map.Entry<String,String> me : set2){

    String key = me.getKey();

    String value = me.getValue();

    syso(key+"---"+value);

   }

 

 

Collections:针对集合进行操作的工具类,可以对集合进行排序和查找

  成员方法:

    sort:排序

    binarySearch:查找

    reverse:反转

    shuffle:使用默认或指定随机源对指定列表进行置换(随机置换)

 

 

17、异常

throw和throws的区别:

  throw:用在方法体内,跟的是异常对象名

      只能抛出一个异常对象名

      表示抛出异常,由方法体内的语句处理

      执行throw则是一定抛出了某种异常

 

  throws:用在方法声明后面,跟的是异常类名

      可以跟多个异常类名

      表示抛出异常,由该方法的调用者来处理

      throws表示出现异常的一种可能性,并不代表一定会发生异常

finally:

  被finally控制的语句体一定会执行。但是假如在执行到finally之前jvm退出了,那么其中的语句将不会执行

  如果catch里面有return语句,那么finally中的语句还是会执行的。先执行finally中的语句再执行return。

 

18、IO流

File类

File(String pathname)
          通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。

File(File parent, String child)
          根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。

File(String parent, String child)
          根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。

 

File 的成员方法

创建:

  createNewFile():  当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。

  mkdir():  创建此抽象路径名指定的目录。

  mkdirs();  创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。

删除:

  delete():  删除此抽象路径名表示的文件或目录。

重命名:

  renameTo():重新命名此抽象路径名表示的文件。

判断:

  isFile():  测试此抽象路径名表示的文件是否是一个标准文件。

  isDirectory(): 测试此抽象路径名表示的文件是否是一个目录。

 

字节流(  除了  用windows自带的记事本打开能读懂的用字符流 其他都用字节流)

-----输入流:InputStream(此抽象类是表示字节输入流的所有类的超类)

      (read()一次读一个字节----------read(byte[] b) 一次读一个字节数组)

---------------------FileInputStream(FileInputStream 从文件系统中的某个文件中获得输入字节)

              (FileInputStream(File file)-----或 ----FileInputStream(String name))            

---------------------------BufferedInputStream:字节缓冲输入流(是FileInputStream的子类)

            (BufferedInputStream(InputStream in)创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。)

 

-----输出流   OutputStream

      (write(int by)一次写一个字节------------write(byte[] bys,int index,int len))一次写一个字节数组的一部分

---------------------FileOutputStream            

--------------------------BufferedOutputStream:字节缓冲输出流(是FileOutputStream的子类)

           (BufferedOutputStream(OutputStream out)创建一个新的缓冲输出流,以将数据写入指定的底层输出流。)

//字节流复制文件的四种方式

public class CopyImageDemo {
public static void main(String[] args) throws IOException {
// 使用字符串作为路径
// String srcString = "c:\\a.jpg";
// String destString = "d:\\b.jpg";
// 使用File对象做为参数
File srcFile = new File("c:\\a.jpg");
File destFile = new File("d:\\b.jpg");

// method1(srcFile, destFile);
// method2(srcFile, destFile);
// method3(srcFile, destFile);
method4(srcFile, destFile);
}

// 字节缓冲流一次读写一个字节数组
private static void method4(File srcFile, File destFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcFile));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destFile));

byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}

bos.close();
bis.close();
}

// 字节缓冲流一次读写一个字节
private static void method3(File srcFile, File destFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcFile));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destFile));

int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
}

bos.close();
bis.close();
}

// 基本字节流一次读写一个字节数组
private static void method2(File srcFile, File destFile) throws IOException {
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);

byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}

fos.close();
fis.close();
}

// 基本字节流一次读写一个字节
private static void method1(File srcFile, File destFile) throws IOException {
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);

int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}

fos.close();
fis.close();
}
}

 

字符流:(=字节流+编码表)(用windows自带的记事本打开能读懂的用字符流)

-----输入流  Reader(用于读取字符流的抽象类)

          (read()一次读一个字符----------read(char[] b) 一次读一个字符数组)

--------------------------InputStreamReader(inputStream in):字符输入流(转换流)  InputStreamReader(inputStream in,String charsetName)//指定编码

              (InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符)

--------------------------------------------FileReader(用来读取字符文件的便捷类。)(是InputStreamReader的直接子类)

-------------------------BufferedReader(是Reader的子类)

            (String readLine()一次读取一个字符串)

           ( BufferedReader in = new BufferedReader(new InputStreamReader(System.in));)

 

 

-----输出流  Writer(写入字符流的抽象类)

          (write(int ch)一次写一个字符------------write(char[] chs,int index,int len))一次写一个字符数组的一部分

--------------------------OutputStreamWriter:字符输出流(转换流) OutputStreamWriter(outputStream outString charsetName)//指定编码

--------------------------------------------FileWriter

-------------------------BufferedWriter

              (void write(String line)一次写一个字符串     void newLine()写一个换行符)

 

 

 //字符流复制文本文件的五种方式

public class CopyFileDemo {
public static void main(String[] args) throws IOException {
String srcString = "c:\\a.txt";
String destString = "d:\\b.txt";
// method1(srcString, destString);
// method2(srcString, destString);
// method3(srcString, destString);
// method4(srcString, destString);
method5(srcString, destString);
}

// 字符缓冲流一次读写一个字符串
private static void method5(String srcString, String destString)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcString));
BufferedWriter bw = new BufferedWriter(new FileWriter(destString));

String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}

bw.close();
br.close();
}

// 字符缓冲流一次读写一个字符数组
private static void method4(String srcString, String destString)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcString));
BufferedWriter bw = new BufferedWriter(new FileWriter(destString));

char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1) {
bw.write(chs, 0, len);
}

bw.close();
br.close();
}

// 字符缓冲流一次读写一个字符
private static void method3(String srcString, String destString)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcString));
BufferedWriter bw = new BufferedWriter(new FileWriter(destString));

int ch = 0;
while ((ch = br.read()) != -1) {
bw.write(ch);
}

bw.close();
br.close();
}

// 基本字符流一次读写一个字符数组
private static void method2(String srcString, String destString)
throws IOException {
FileReader fr = new FileReader(srcString);
FileWriter fw = new FileWriter(destString);

char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1) {
fw.write(chs, 0, len);
}

fw.close();
fr.close();
}

// 基本字符流一次读写一个字符
private static void method1(String srcString, String destString)
throws IOException {
FileReader fr = new FileReader(srcString);
FileWriter fw = new FileWriter(destString);

int ch = 0;
while ((ch = fr.read()) != -1) {
fw.write(ch);
}

fw.close();
fr.close();
}
}

 

 

打印流(只能操作目的地不能操作数据    可以操作任意类型的数据    如果启动了自动刷新,能够自动刷新)

 

随机访问流  RandonAccessFile

  其实它并不属于流,是Object的子类,但是它融合了InputStream 和 OutputStream的功能,支持对随机文件的读取和写入

 

序列化流  ObjectOutputStream  

反序列化流  ObjectInputStream

 

Properties集合     作为Map集合使用

  Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。 

  setProperty(String key, String value)调用 Hashtable 的方法 put

  getProperty(String key) 用指定的键在此属性列表中搜索属性。

  stringPropertyNames()返回此属性列表中的键集,其中该键及其对应值是字符串,如果在主属性列表中未找到同名的键,则还包括默认属性列表中不同的键。

 

  load(Reader reader) 按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。

  store(Writer writer, String comments)以适合使用 load(Reader) 方法的格式,将此 Properties 表中的属性列表(键和元素对)写入输出字符。

 

  

转载于:https://www.cnblogs.com/xj-mango/p/6504925.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值