您所在的位置:主页 > 培训巧问答 >

Java面试集合(二)

时间:2018-08-22 10:37来源:未知 作者:os 点击:

 

 

前言

大家好,给大家带来Java面试集合(二)的概述,希望你们喜欢

1.请问线程有哪些状态?

新建状态(New)
就绪状态(Runnable)
运行状态(Running)
阻塞状态(Blocked)
死亡状态(Terminated)

2.表达线程代码?

new Thread(){
 public void run(){}
}.start();
new Thread(new Runnable(){
 public void run(){}
}).start();

3.如何表示什么是接口?

interface Student{
 public void read();
 public void write();
}
class ChineseStudent implements Student{
 public void read(){
 System.out.println("read");
 public void write(){
 System.out.println("write");
}
}
//测试类
class Test{
 public static void main(String args[]){
  ChineseStudent chinesestudent = new ChineseStudent();
  Student student = chinesestudent;
  
  student.read();
  student.write();
  }
}
//implements关键字,继承多个接口
interface Student{
 public void read();
 public void write();
}
interface Teacher{
 public void teach();
 public void test();
}
class Person implements Student,Teacher{
  public void read(){
   system.out.printlln("read");
}
  public void write(){
   System.out.println("write");
}
  public void teach(){
   System.out.println("teach");
}
  public void test(){
   System.out.println("test");
}
}
//测试类
class Test{
 public static void main(String args[]){
  Person person = new Person();
  Student student = person;
   student.read();
   student.write();
  Teacher teacher = person;
    teacher.teach();
    teacher.close();
}
}

4.简单说说String类和StringBuffer类之间的区别?
答:String类是不可变的类,字符串一旦被初始化就不可能改变;StringBuffer是可变的字符串类,可以修改字符串的值。

5.简单说说List,Set,Map的区别?

List的特点是元素有序,元素可重复;List常见的实现类为ArrayList和LinkedList;Set的特点是元素的无序,元素不可重复,Set常见的实现类有HashSet和TreeSet;Map的特点是存储的元素是键(key)和值(Value)的映射关系,元素都是成对出现的,Map的常见实现类是HashMap和TreeMap。

6.来描述类集合框架?

public class Test{
 public static void main(String[] args){ 
  ArrayList list = new Arraylist();
  for(int i = 0; i<10; i++){
   list.add("Test:"+i);
  }
  //打印
  Iterator iterator = list.iterator();
  while(iterator.hasNext()){
   //集合不能记住元素的类型
   Object object = iterator.next();
   System.out.println(object);
  }
}

7.说说字节流和字符流?
答:字节流的两个基类分别是InputStream和OutputStream,字符流的两个基类分别是Reader和Writer。字节流是以8位字节为单位的字节流类,而字符流是以16位字节为单位。

8.用代码介绍FileInputStream和FileOutputStream,以及BufferedReader和BufferedWriter?

public class Test{
 public static void main(String args) throws Exception{
  //字节流
 FileInputStream in = new FileInputStream("C:/test.txt");
 FileOutStream out = new FileOutputStream("D:/testone.txt");
  byte[] buf = new byte[1024];
  int len;
  while( (len = in.read(buf))  !=  -1){
   out.write(buf,0,len);
 }
 in.close();
 out.close();

//字符流
BufferedReader bf = new BufferedReader(new FileReadere("C:/test.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("D://testtwo.txt"));
String str;
 while( (str = bf.readLine() ) != null ){
  bw.write(str);
  bw.newLine();
}
bf.close();
bw.close();
}
}

9.网络开发Socket和ServerSocket的表达?

//服务端
public class ServerSocket1{
 public static void main(String[] args){
  try{
   ServerSocket ss = new ServerSocket(2008);
  while(true){
  Socket s = ss.accept();
  InputStream is = s.getInputStream();
  OutputStream os = s.getOutputStream();
  PrintStream ps = new PrintStream(os);
 ps.println("hello, i am server");
 
 DataInputStream dis = new DataInputStream(is);
 String str = dis.readLine();
 System.out.println(str);
 s.close();
}
}
catch(IOException ee){
 System.out.println(ee);
}
catch(Excepiton e){
 System.out.println(e);
}
//客户端
public class ClientSocket{
 public static void main(String[] args){
 try{
 Socket s  = new Socket("####id",2008);
 InputStream is = s.getInputStream();
 OutputStream os = s.getOutputStream();
 PrintStream ps = new PrintStream(os);
 ps.println("hello, i am client");
 DataInputStream dis = new DataInputStream(is);
 String str = dis.readLine();
 System.out.println(str);
 s.close();
}
catch(ConnectException eee) {
     System.out.println(eee);
   }
   catch(IOException ee) {
     System.out.println(ee);
   }
   catch(Exception e) {
     System.out.println(e);
   }
 }
}

10.谈谈,解惑?
答:对于我来说,我认为程序员并不是最好的职业,这是从享受生活的角度上看的,我听说过太多程序员的熬夜现象了,这是一门学到老的专业方向,如今IT的发展太快了,并且太多细节需要我们深入了解,这就大大让IT工作太累了,如果不是喜欢,不是兴趣,建议自己重新思考,重新定义。

总结

  • 本文讲了Java面试集合(二),如果您还有更好地理解,欢迎沟通