SE

来源:互联网 发布:淘宝卖家如何分销 编辑:程序博客网 时间:2024/04/30 06:57

1.字符串字面量在内容一样的时候重用同一个String对象
字面量和常量的运算结果是字符串,也同用内存

String s1="123abc";String s2="123abc";syso(s1==s2)//trueString s1 = "1"+"23"+"abc";String s2 = "1"+23+"abc";String s3 = '1'+23+"abc";String s4="123abc";System.out.print(s1==s2);//trueSystem.out.print(s1==s3);//falseSystem.out.println(s1==s4);//false

2.String,StringBuilder,StringBuffer区别:操作量大的前提下,StringBuilder>StringBuffer>String
一般操作量少用String,StringBuilder非线程安全,StringBuffer线程安全
3.==和equals方法的区别:
对于基本类型的变量,==比较值,equals不能用于基本类型变量
对于引用类型的变量,在没有重写equals的前提下,都是比较对象的地址。String重写了equals方法。
4.Object类的toString方法返回值是类名@散列码,没有实际意义,建议重写
equals默认比较地址

    public String toString() {        return x+","+y;    }    //重写equals方法比较两个对象是否相等    public boolean equals(Object obj){        //当两个对象的x和y都相等时候则相等        if(obj==null) return false;        if(this==obj) return true;        //使用if语句保护,避免造型异常        if(obj instanceof PointX){            //为了读取x y属性必须造型为子类型            PointX other=(PointX)obj;            return this.x == other.x &&                 this.y == other.y;        }        //方法一定返回一个boolean值!        return false;    }

5.包装类:将基本类型包装成对象;Java5后引入自动拆包装
将字符串类型转换为基本类型

String str="-1232";int i=Integer.parseInt(str);System.out.println(i);//-1232String s="Abc123";int j=Integer.parseInt(s);System.out.println(j);//NumberFormatExeception

6.时间API
Date类型对象中默认封装了当前系统时间。
SimpleDateFormat:转换为自己想要的时间格式

SimpleDateFormat fmt=new SimpleDateFormat();Date date=new date();System.out.println();

将字符串按格式解析为计算机时间

String str="1980-1-1";SimpleDateFormat fmt=new SimpleDateFormat("yyyy-m-d");Date date=fmt.parse(str);System.out.println(fmt.format(date));

7.集合
Collection接口:
set接口:集合中的元素不按特定的方式排列,不能有重复的元素hashset实现类,treeset实现类;
list接口:集合中的元素以线性方式存储,可以放重复元素。ArrayList实现类,可以对元素随机访问,向Array List插入和删除元素较慢。本质上是数组;LinkedList实现类,对元素的访问慢,插入和删除元素快,本质上是链表;Vector实现类,线程安全的,采用数组。
contains方法:contains依赖于equals方法
集合的迭代:

Iterator ite=c.iterator();while(ite.hasNext()){    String s=(String)ite.next();}

增强for循环:foreach(Object s: c)
Queue队列
Java提供了Queue API,由LinkedList实现,用于处理先进先出的业务问题
1.offer将数据插入队列2.peek检查队列头部元素3.poll从队列拉出数据
Stack栈
栈是后进先出的数据结构,Java利用Deque接口提供了栈操作方法push,pop由LinkedList实现了Stack结构:
push将数据入栈,pop将数据出栈

       // Deque 接口中定义了 栈操作方法        // LinkedList 实现了栈操作方法        Deque<String> stack =             new LinkedList<String>();        //栈提供了 push方法可以将数据“压”入栈中。        stack.push("Tom");        stack.push("Jerry");        stack.push("Andy");        //先压入的数据在栈的最底部        System.out.println(stack);        //出栈的顺序相反:后进先出        while(! stack.isEmpty()){            //利用pop可以从栈中弹出数据            //弹出顺序与进入顺序相反            String s = stack.pop();            System.out.println(s);         }

查找表Map,key是否重复是看hashcode,String已经重写hashcode,其他引用类型需要重写hashcode和equals方法
hashmap:允许键和值为空,非线程安全,效率较高,没用contains方法
hashtable:不允许键和值为空,线程安全,效率稍低,有contains方法。
hashmap是hashtable轻量级的实现,hashmap是Java1.2引入。
Map的常用实现类HashMap是查找性能最好的数据结构。
1.put(key,value),如果map中已存在key,则替换
2.value=get(key)从map中查找value
3.boolean containsKey(key)检查map中是否包含key
关于hashcode:hashcode的存在主要是查找的快捷性;hashcode相同,并不表示地址相同,只能说明对象存在散列存储结构中的同一个“篮子”;
4.map的遍历:利用Entry遍历集合,key的集合遍历map

//利用key遍历Map<String,String> map=new HashMap<String,String>();Set<String> keySet=map.keySet();//获取全部keyforeach(String key:keySet){    syso(key);    syso(map.get(key));}//利用value遍历for(String values:map.values()){}//利用Entry遍历mapSet<Entry<String,String>>entries=map.entrySet();foreach(Entry<String,String> entry:entries){    String key=entry.getKey();    String value=entry.getValue();}

8.Java File

        //使用绝对路径创建文件夹        //创建文件        File file=new File("D:/demo");        boolean a=file.mkdir();        System.out.println(a);        File file1=new File("D:/demo/test.txt");        System.out.println("1");        boolean b=file1.createNewFile();        System.out.println("2");        System.out.println(b);

9.IO流
IO流按照功能可以分为两大类:节点流和处理流
节点流:是流最原始的数据源,提供流最基本的功能。
处理流:也称高级流
按照数据流向分为输入流和输出流:inputStream,outputStream
9.1文件输出入流FileOutputStream,FileInputStream
文件输出流节点流,是以文件为目标数据源的节点流,是基本的流,只提供基本的输出方法write()输入方法read()
9.2缓冲流(高级流)必须依赖基础流,文件复制
*BufferedInputStream in=new BufferedInputStream(new FileInputStream(“D:/text.txt”));
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(“D:/test.txt”));
int b;
while((b=in.read())!=-1){
out.write(b);
}
in.close();
out.close();
**
9.3对象输出流:高级流
实现序列化接口时候Java自动添加两个方法:对象序列化方法(将对象变为byte)
反序列化方法(将byte拼接成对象)
9.4字符流(高级流):只能处理文本文件

OutputStreamWriter osw=new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream()),"utf-8");osw.write("hello");osw.close();InputStreamReader isr=new InputStreamReader(new BufferedInputStream(new FileInputStream("D:/test.txt")),"utf-8");int c;while((c=isr.read()!=-1)){      syso(c);}

9.5PrintWriter写文本文件,BufferedReader提供readLine

OutputStreamWriter osw=new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream("D:/test.txt")),"utf-8");PrintWriter pw=new PrintWriter(osw,true);pw.print("gg");InputStreamReader isr=new InputStreamReader(new BufferedInputStream(new FileInputStrem("D:/test.txt")),"utf-8");BufferedReader br=BufferedReader(isr,true);String str;while((str=br.readLine())!=null){    syso(str);}

10.异常Throwable
10.1分为Error和Exception
10.2区别:Error表示系统出现比较严重的问题,与编写的代码和执行的操作无关,多数是JVM出现了问题。例如:栈空间不足,StackOverflowError,堆空间不足,OutofMemory;
Exeception表示不是特别严重的问题,可以预测和处理。分为一般异常和运行时异常,运行时异常。一般异常需要try catch finally,运行时异常有:NullPointExeception,ClassNotFoundExeception,FileNotFoundExeception,NumberFormatExeceptionn,SQLExeception,ParseExeception,IndexOutOfBoundsExeception
11.线程
a.什么是进程,什么是线程
进程是操作系统中运行的一个任务,线程是操作系统能够运算调度的最小单位,它被包含在进程中,是进程中实际的运作单位。
b.线程和进程的区别
一个进程有多个线程,多个线程共享进程的内存空间,线程是进程的子集。
c.实现线程的方法
继承Thread类,实现runnable接口。因为Java不支持多继承,但可以实现多个接口,一般用runnable接口

//继承Thread类public static void main(String[]args){    Thread1 t1=new Thread1();    t1.start();}class Thread1 extends Thread{    public void run(){    }} //实现Runnable接口public static void main(String[]args){    Run r=new Run();    Thread t1=new Thread(r);    t1.start();}class Run implements Runnable{    public void run(){    }}//匿名内部类public static void main(String []args){    Thread t1=new Thread(){        public void run(){        }       };         Runnable r=new Runnable(){           public void run(){           }      };    Thread t=new Thread(r);    t.start();}

d.线程的五种状态,创建,就绪,运行,阻塞,和死亡
创建:生成线程对象,处于创建状态;
就绪:调用strat()方法后,纳入线程调度,线程处于就绪状态,从等待或睡眠状态回来之后会处于就绪状态;
运行:线程调度将就绪状态的线程设置为当前线程,此时线程进入运行状态,开始运行run方法中的代码;
阻塞:线程运行的时候,被暂停。sleep(),suspend,wait()等都可能导致线程阻塞;
死亡:如果一个线程在run()方法结束,或者调用stop()方法,该线程就会死亡;
e.并发原理:多线程的同时运行只是我们感官上的一种表现,事实上线程是并发运行的,OS将时间划分为很多时间片段,尽可能的均匀分配给每个线程,获取时间片段的线程被CPU执行,而其它线程等待,所以微观上是走走停停,宏观上是都在运行,这种现象叫并发。
多线程并发安全问题:当多个线程并发操作同一临界资源时,由于线程切换时机不确定,可能导致多线程并发安全问题:synchronized
f.sleep和wait的区别:sleep是Thread的方法,导致此线程暂停执行指定时间,将执行机会给其它线程,但是监控状态依然保持,到时自动恢复,sleep不会放弃对象锁;wait是Object类的方法,导致此线程放弃对象锁,必须针对此对象发出notify或者notiifyAll后此线程处于就绪状态;
g.join():用于等待当前线程结束,才会执行另一个线程。yeild()使当前线程主动让出cpu时间片段,进入就绪状态;
h.线程池作用:控制线程数量;重用线程;

/** * 获取上周日日期 * * @param n //n为推迟的周数,-1向前推迟一周,0本周,1下周,依次类推 * @return */private String getPreSundayDate(int n) {    Calendar cal = Calendar.getInstance(Locale.CHINA);    cal.add(Calendar.DAY_OF_WEEK, n * 7);    //想周几,这里就传几Calendar.SUNDAY(TUESDAY...)    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);    cal.add(Calendar.WEEK_OF_YEAR, 1);    return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());}
原创粉丝点击