文章目录
问题解决能力
- 排除(百度筛选代码,代码筛选目标)
- 使用(脑部代码+结构+尝试)
- 错误、异常(脑部+输出+debug,解决错误)
- 坚持(重试,重做)
总结:
系统输入输出
- System 输出
- Scanner 输入
System.arraycopy(src, srcPos, dest, destPos, length); //数组拷贝,原数组到目标数组
System.currentTimeMillis(); //当前时间,毫秒
System.exit(status); //退出主线程
System.gc(); //垃圾回收,finalize()
public FuXi() {
this.finalize();//当前对象回收
}
Map<String, String> p = System.getenv(); //获取系统环境参数;
Properties d = System.getProperties(); //获取当前项目环境参数
for(Object s:d.keySet()) {
System.out.println(s+":"+d.get(s));
}
System.setOut(new PrintStream("d://aa.txt")); //更改输出位置,将输出写入文件
System.out.println("lhy");
System.nanoTime(); //时间随机值,确保不重复
}
注释
- // 单行
- /* */ 多行
- /** 内容上方 回车 java doc注释(文档)
/**
*
* @author Ocean
*
*/
关键字
- volatile: 保持内存可见性和防止指令重排序
内存可见性:所有线程都能看到共享内存的最新状态 - transient :让某些被修饰的成员属性变量不被序列化
transient int modCount:变量不被序列化 - native:本地方法(访问C语言库),调用C语言,c++的方法
- strictfp:可应用于类、接口,让你的浮点运算更加精确,保障不同的硬件平台所执行的结果一致。
变量
访问符 功能符 类型 变量名 = 值
- 基本类型8种:对应引用类型(直接赋值+默认转换),转换方法:构造方法,valueOf()(强制转换)
- 引用类型:(其他全部都是引用类型,继承Object)
- 内存管理区别:(变量栈:基本类型+引用类型指针,引用堆:引用类型数据)
- 访问符:public(公有)、private(私有)、无访问符(friendly,包内公有,包外私有)、protected(包内公有,包外继承可用)
- 变量种类:(范围,用途)
-
局部变量:“{ }”内可用,临时计算,临时性变量
-
实例变量:类内-类外实例可用,对象属性使用,单类多方法共用变量
-
静态变量:任何位置, 全局变量(通用设置),多类共用
-
静态常量:任何位置(方法参数),方法参数标注(非量词,方法当前类)
Font f = new Font(name, Font.BOLD, size);
-
参数变量
-
运算符
-
i++ ; ++i
-
快速赋值:
a = (a==1)? 2:3;
-
&&;& 之间的关系
-
“>>”;">>>";"<<"移位 用二进制完成运算,ip地址解析
-
优先级 + “()”
-
Boolean 赋值运算
boolean b;
b= a==1;
转义符
String a = " 121 " \nw121";
类型转换
- 基本类型之间的转换(字节补充,截取)
int a = 12121;
byte d = (byte)a;
//结果为89,字节截取
- 强制类型转换
byte d = (byte)a;
student s = (student)obj; //改称呼,继承+接口
数组(索引)
- 定义
数据缓冲区 - 存取
int[] arr = {1,2};
arr[1] = 3; int b = arr[0];
- 遍历
for(int i=0; i<arr.length; i++);
for(int a:arr);
- 多维
(容积为 每维相乘)
int[][] arr = {{1,2},{3,4}};
for(int i=0; i<arr.length; i++)
for(int j=0; j<arr[i].length; j++);
for(int[] a:arr)
for(int b:a);
- 工具
arrays类 - 排序
- 快速
- 冒泡
- 选择
- 插入
可变参数
必须为最后一个参数
public void add(int … all); //all 就是数组变量
new t().add(1,2,3,4); //0长度开始
面向对象
-
类,方法,属性的创建和使用
-
方法调用:类".";类实例"."
-
继承:多命名,代码赋值 super,this;(构造方法转接,参数变量的区别)
-
封装:访问符public;setters和getters(属性方法)
-
多态:
-
方法重写:把上级的行为内容进行修改/覆盖,子类继承父类的方法,进行重写
public String toString() {
return super.toString();
} -
方法重载:方法名字相同,参数不同
public String toString() {
}
public String toString(int a) {
}
-
-
面向对象描述:用现实事物的思维,描述框架和功能
-
构造方法:new 后面的方法
抽象
- 抽象方法:只有描述,没有实现内容的方法 abstract;
- 抽象类:有抽象方法的 和使用时必须重写内部方法的类
- 内部匿名类,外部不匿名类
接口
全部为抽象方法
默认方法:default void ss();
静态常量
public interface fuxi{
default void ss() { //默认方法
}
}
- 耦合度:把类绑定改为接口绑定
- 通知:事件处理,向外通知处理(USB电脑手机连接,网络管理类)
- 接口实现:
-
外部不匿名类,创建类引用接口
class t2 implements t1
-
内部匿名类,new接口
new t1() { };
-
创建接口变量,创建接口实例
t1 t = new t1() { };
-
函数式接口:只有一个方法的接口
t1 t = ()->{ };
-
泛型
- 类,接口,方法
ArrayList< string >
泛型约束:ArrayList<? extends Number>
多泛型定义:class aa < T,D,F >{ }
集合
- List:可通过索引存取数据,按放置顺序摆放
- 集合:arraylist;vector;linkedlist(first,last)
- 方法:add;remove;size;set,indexOf
- Set:内置顺序,无索引,不能重复
- 集合:hashset(根据hash值顺序);treeset(按大小顺序,comparable接口)
- 方法:add;remove;size;set
- Map:键值对放置,通过Key处理
- 集合:hashmap,hashtable,ConcurrentHashMap(线程安全,替代早期hashtable)
- 方法:put,remove,containsKey,keyset,values
- Queue:队列
- LinkedBlockingQueue(线程阻塞队列)
迭代器
- 封装的遍历,有没有+取下个
ArrayList list = new ArrayList<>();
Iterator t = list.iterator();
while(t.hasNext()) {
System.out.println(t.next());
}
- 类用法
private int[] arr = { 12, 3, 3, 6 };
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
int pos=0;
public Integer next() {
return arr[pos++];
}
public boolean hasNext() {
return pos<arr.length;
}
};
}
异常处理
public static void main(String[] args) throws Exception{ //抛出方法
try { //检验异常
throw new MyException(); //跳出自定义异常
} catch (Exception e) { //捕获异常,可多个
e.printStackTrace();//打印异常栈
} finally {
//与return组合,处理返回的收尾工作
}
}
- 自定义异常类
public class MyException extends Exception {
@Override
public String getMessage() {
// TODO Auto-generated method stub
return super.getMessage();
}
}
调试(debug)
-
设置断点
-
debug运行到断点(线程挂起)
-
查看断点信息(变量值)
-
控制运行过程(F5、F6、F7、F8)(查看运行步骤)
-
查看异常栈,解决异常(异常名称,异常信息,栈内容:自上向下找用户行,点击到指定行)
e.printStackTrace();
IO
- 字符流:Reader、Writer
- File:文件字符流
- Print:打印字符流
- 字节流:InputStream、OutputStream
- File:文件字节流
- Object:对象字节流
- Zip:压缩字节流
- ByteArray:字节数组
- 过程:
- 创建(两种流,中间流)
- 读写(读时:缓冲区)
- 关闭(先开后关)
序列化
-
处理核心:ObjectInputStream,与其他流组合使用
-
Serializable:自动序列化,全部实例变量,strictfp排除
-
Externalizable:自定义序列化,根据接口处理,可静态变量
-
序列号:值一致时成功
serialVersionUID
NIO(NON BLOCKING)
- channel:InputStream.getChannel()
- buffer:ByteBuffer b = ByteBuffer.allocate(100);
- channel.read(b);
- selector:网络使用,一个线程管理多个channel(tcp服务端)
多线程
-
Thread类:start()方法,join(),setDaemon()方法,
-
定义:
- Thread t = new Thread() { }; //重写方法
- Thread t = new Thread(new Runnable() {}); //定义Runnable接口
- Thread t = new Thread(()->{}); //lambda方法
-
方法:
- Thread.sleep();
- Thread.currentThread();
-
线程同步:
Object obj = new Object(); //公共访问区 synchronized (obj) { //线程1 obj.wait(); //在当前线程1挂起 } synchronized (obj) { //线程2 obj.notify(); //原已挂起的唤醒,notifyAll()全部唤起 obj.wait(); //线程2挂起 }
反射
- 5套方法
- 获取Class对象
- 创建实例:newInstance
- 属性(get,set)方法(invoke)使用
Properties(hashTable)
- load(),store(),getProperty();
XMl读取
- dom4j
- 获取文档对象,根节点,子结点+属性+内容
Swing
- Jframe对象(add,setvisiable)
- 大写J打头控件
- 事件 add……listener方法:接口
- 布局管理器:setlayout(null) 指定控件位置大小