面试难点总结[java篇]

http://blog.csdn.net/qiyuexuelang/article/details/8823225

填空选择类

       1.八大基本数据类型的范围及默认值
           数据类型 大小 范围 默认值 
                    byte(字节) 8 -128 - 127 0

shot(短整型) 16 -32768 - 32768 0

int(整型) 32 -2147483648-2147483648  0

long(长整型) 64 -9233372036854477808-9233372036854477808 0

 float(浮点型) 32 -3.40292347E+38-3.40292347E+38 0.0f

double(双精度) 64 -1.79769313486231570E+308-1.79769313486231570E+308 0.0d

char(字符型) 16 ‘ \u0000 - u\ffff ’ ‘\u0000 ’

boolean(布尔型) 1 true/false false

注意:string不是基本数据类型

 2.Integer是java提供的封装类  int是原始类型; Integer的 缺省值 null  int的缺省值 0
 3.string类提供了数值不变的字符串, 如果操作少量数据用string
   stringBuffer类提供的字符串可修改(动态),多线程操作字符串缓冲区下操作大量数据
 4.ArrayList、Vector使用数组存储数据,索引快插入慢 
       Vector同步线程安全,ArrayList不是线程安全;
       Vector在需要增长时默认增长方式为原来的一倍,而ArrayList为原来的一半
   LinkedList使用双向链表存储,索引难插入快
 5.sleep()线程类方法,导致此线程暂停执行时间,但监控状态依然保持,到时自动恢复 占用CPU
   wait()Object类方法,对对此对象调用wait()方法导致线程放弃对象锁,只有对此对象发出          notify()方法后,本线程才进入对象锁对其进行执行。
 6.java中以unicode编码,一个char占16个字节,故能放一个中文
 7.java常见的类型流:字节流Input/OutputStream  字符流Reader Writer
 8.常见的runtime exception:ArrayStoreException  BufferOverflowException                        ClassNotFoundException  NumberFormatException IndexOutOfBoundsException
   NullPointerException NoSuchElementException
 9.java常用包:java.io.*;java.util.*;java.long.*;java.net.*;java.sql.*;javax.servlet;
   java常用类:BufferedReader BufferedWriter FileReader String Integer java.util.Date
               system Class List HashMap
   java常用接口:Romote List Map Document Modelist Servlet HttpServletRequest                               HttpServletResponse session
 10.switch 作用类型char byte int short
 11.Class类是由java编译器自动产生,它伴随着每个类
     任何一个java程序都默认引入一个包,这个包叫java.long 
     java语言中,所有类或接口的父类  java.long.Object
 12.java实现序列化的方法是 实现java.io.Serializable
 13.Collection框架实现比较要实现 Comparable接口和Comparator接口
 14.在try-catch-finally语句中,System.exit()是干掉finally的最佳办法
  •   判断改正类
       1.short s1 =1;s1=s1+1 (需强转)  short s1 =1;s1+=1; 正确
       2.接口可以继承接口  抽象类可以实现implements接口
       3.数组有length属性,String有length方法 String类是final类,故不可继承
       4.set里元素通过iterator()方法 区分重复与否,因此equals()判断两个set是否相等
       5.构造器不能被继承,因此不能重写,但可被下载
       6.不能从static方法内发出对非static方法的调用
       7.try内有return,紧跟后的finally依然会执行,在return之前
       8.float f =(float)3.4
       9.boolean b = null 错误! boolean只能为true或false
       10.是否合法 
            2variable  错     variable2  对  _whatavariable 对   _3_  对  $anothervar  
            #myvar  
       11.break case 语句 没有break 就会继续下面的语句,满足case后的case失效,执行后面的代码直至遇           到break为止
       12.C语言 int i = 0;if(i){}  true
          java语言 return false
       13.class Base{
             class Sub extends Base{}
             public class Cex{
            //主方法(简)
             main{
               Base b = new Base ();
               Sub s = (Sub)b;
               }
            }
          运行结果:Runtime Exception
            }
         14.java没有 <<<</div>
           System.out.println(-1>>>2); 输出一个大于10的结果 (1073741823)
           ------------------(-1>>2); 输出一个-1
           ------------------(2>>1);   输出1
         15.var a = 2;var b = 3; (二进制解决)     a&b =2, a|b =3, a^b=1 , ~a =-3
         16.组件必须放到一定的容器中才能显示
         17.final的变量只能在两个地方赋值,一个是定义它的时候,一个是在构造函数中
            static final声明的变量一律常量,必须声明时初始化
         18.class SomeThing{
             int i;
           public void doSomething(){
           System.out.println("i+"+i);
            }
             }
           正确
        19.数据类型转换(低向高要强转,高向低不需要)
           long l =4990; true    int i = 4L; false   double d = 34.4; true   double t = 0.9F; true
        20.StringBuffer sb = "ab"  ;   false 需初始化
        21.boolean b = new Boolean("abce")  true  返回值为false
        22.程序段 1)fobj = new Object(); 2)fobj.Method(); 3)fobj = new Object();4)fobj.Method();
              符合垃圾收集器的代码是 3
              符合条件:1.给对象赋了空值 2.给对象赋了新值
                   1)Object sobj = new Object();2)Object sobj = null;
                   3)Object sobj = new Object(); 4)sobj = new Object();
              符合垃圾收集器的代码是 1,3
        23.-0.0==+0.0 但+0.0>-0.0在比较max min时
        24.
  •   比较问答类
       1.线程同步的方法 :
         wait()使线程处于等待,释放对象锁  sleep()使当前线程睡眠
         notifyAll()唤醒所有等待线程 notify()唤醒处于等待的某个线程
       2.多线程实现方法 :
          继承Thread类和实现runable接口
         同步实现方法 :
          synchronized,wait,notify
       3.throw 明确地抛出一个“异常” throws表明一个成员函数可能要抛出的“异常”
       4.java实现多态的机制:
          方法重写Overriding  父类与子类之间多态性的一种表现 
          方法重载OverLoading 一个类中多态性的表现
       5.静态变量与实例变量:静态变量是常量,实例变量是变量 class A a; a.i = 10;
       6.软件开发中的设计模式
         单例模式  主要作用是保证在java应用程序中,一个类class只有一个实现类 
         DAO模式 模板模式 工厂模式 委托代理模式 MVC模式
       6.抽象类和接口
         1)定义
         抽象类:声明方法的存在却不去实现他的类,用于创建一个体现某些行为的类,但不能在该类中实现            该类的情况的
         接  口:抽象类的特例,接口中所有方法都必须是抽象的,接口定义的方法默认为public
           是没有实现的方法和常量组成的集合,用来解决java不支持多继承问题
         2)不同
         抽:一个类只能继承一个抽象类,若干个继承类继承一个抽象类
         口:一个雷可以实现多个接口。若一个类实现一个接口,那么必须实现接口的全部方法
         抽:成员变量可以是任意类型
         口:public static final
         抽:成员方法可以有abstract的,也可以不是
         口:方法都是public,且都没实现 
         3)共性
         都不能被实例化,但都可以定义一个抽象类/接口的对象变量(引用)指向继承/实现它的子类对象
         抽象类中可以由抽象方法,也可没有,有抽象方法的类一定是抽象类
       7.在编程过程中哪些地方该用注解:
       8.&&&
         &是位运算符,表示按位与运算,&&是逻辑运算符,表示逻辑与(and)
       9.数据连接池 与 数据库连接池
       10.heapstack的区别
           栈是一种线性集合,其删除和添加元素的操作应该在同一时间完成。按先进后出的方式处理
           堆是栈的一个组成
       11.CORBA是什么?用途是什么?
           答:CORBA 标准是公共对象请求代理结构(Common Object Request Broker Architecture),由对象          管理组
   织 (Object Management Group,缩写为 OMG)标准化。它的组成是接口定义语言(IDL), 语言绑定       (binding:
    也译为联编)和允许应用程序间互操作的协议。 其目的为:
    用不同的程序设计语言书写
    在不同的进程中运行
    为不同的操作系统开发
  12. 谈谈finalfinallyfinalize的区别。
    
final 用于声明属性,方法和类,分别表示属性不可变,方法不可覆盖,类不可继承。finally是异常处理语句结构的一部分,表示总是执行。finalize是 Object类的一个方法,在垃圾收集器执行的时候会调用被回收对象的此方法,可以覆盖此方法提供垃圾收集时的其他资源回收,例如关闭文件等。  
   13.  Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实      现)interface(接口)? 
                 可以继承其他类或完成其他接口,在swing编程中常用此方式。
        14. Static Nested Class 和 Inner Class的不同,说得越多越好(面试题有的很笼统)。 

           Static Nested Class是被声明为静态(static)的内部类,它可以不依赖于外部类实例被实例              化。而通常的内部类需要在外部类实例化后才能实例化。
  •   代码编写类
       1.2*8 最快的代码 2<<3移位
       2.字符转换
     
  1. public String translate (String str){  
  2.          String tempStr = "";  
  3.          try{  
  4.         tempStr = new String(str.getBytes("ISO-8859-1"),"GBK");  
  5.         tempStr = tempStr.trim();           
  6.             }catch(Exceptione){  
  7.                       out.print(e.getMessage());            
  8.                                 }  
  9.           return tempStr;  
  10.        }  

         3.插入排序
  1. public class CharuSort {  
  2.         public static void main(String[] args){  
  3.                 int[] sort={4,6,3,9,5};  
  4.                 Sort(sort);  
  5.                 for(int i=0;i<sort.length;i++)  
  6.                 System.out.print(sort[i]+" ");  
  7.         }  
  8.           
  9.         public static void Sort(int[] sort){  
  10.                 int i;            //为扫描次数  
  11.                 int j;            //定为比较得元素  
  12.                 for(i=1;i<sort.length;i++){        //扫描次数为sort.length-1  
  13.                         int temp;          //temp用来暂存数据  
  14.                         temp=sort[i];  
  15.                         j=i-1;  
  16.                         while(j>=0&&temp<sort[j]){        //如果第二个元素小于第一个元素  
  17.                                 sort[j+1]=sort[j];            //把所有的元素往后推一个位置  
  18.                                 j--;  
  19.                         }  
  20.                 sort[j+1]=temp;                   //最小的元素放到第一个位置  
  21.                 }  
  22.         }    
  23. }  


         4.java冒泡排序
  1. public class BubbleSort {  
  2.     private static int c =0;//执行次数  
  3. public static void main(String[] args) {  
  4.     int[] a = {60,1 , 35 ,32,  44 , 321  };  
  5.     sort(a);  
  6.     System.out.println(c);  
  7.     for(int i:a){  
  8.         System.out.print(i+"  ");  
  9.     }  
  10. }  
  11.   
  12. public static void sort(int[] array) {  
  13. int length = array.length;  
  14. int temp;  
  15. boolean isSort;  
  16. for(int i = 1; i < length; i++) {  
  17.     c++;  
  18. isSort = false;  
  19. for(int j = 0; j < length - i; j++) {  
  20. if(array[j] > array[j+1]) {  
  21. //交换  
  22. temp = array[j];  
  23. array[j] = array[j+1];  
  24. array[j+1] = temp;  
  25. isSort = true;  
  26. }  
  27. }  
  28. if(!isSort) break//如果没有发生交换,则退出循环  
  29. }  
  30. }  
  31. }  


         5.简单的C/S程序。java通信编程,用JavaSocket编程读写字符

服务器端:
  1. package test;   
  2. import java.net.*;   
  3. import java.io.*;   
  4. public class Server   
  5. {   
  6. private ServerSocket ss;   
  7. private Socket socket;   
  8. private BufferedReader in;   
  9. private PrintWriter out;   
  10. public Server()   
  11. {   
  12. try   
  13. {   
  14. ss=new ServerSocket(10000);   
  15. while(true)   
  16. {   
  17. socket = ss.accept();   
  18. String RemoteIP = socket.getInetAddress().getHostAddress();   
  19. String RemotePort = ":"+socket.getLocalPort();   
  20. System.out.println("A client come in!IP:"+RemoteIP+RemotePort);   
  21. in = new BufferedReader(new   
  22.    
  23. InputStreamReader(socket.getInputStream()));   
  24. String line = in.readLine();   
  25. System.out.println("Cleint send is :" + line);   
  26. out = new PrintWriter(socket.getOutputStream(),true);   
  27. out.println("Your Message Received!");   
  28. out.close();   
  29. in.close();   
  30. socket.close();   
  31. }   
  32. }catch (IOException e)   
  33. {   
  34. out.println("wrong");   
  35. }   
  36. }   
  37. public static void main(String[] args)   
  38. {   
  39. new Server();   
  40. }   
  41. };   
客户端:
  1. package test;   
  2. import java.io.*;   
  3. import java.net.*;   
  4.    
  5. public class Client   
  6. {   
  7. Socket socket;   
  8. BufferedReader in;   
  9. PrintWriter out;   
  10. public Client()   
  11. {   
  12. try   
  13. {   
  14. System.out.println("Try to Connect to 127.0.0.1:10000");   
  15. socket = new Socket("127.0.0.1",10000);   
  16. System.out.println("The Server Connected!");   
  17. System.out.println("Please enter some Character:");   
  18. BufferedReader line = new BufferedReader(new   
  19.    
  20. InputStreamReader(System.in));   
  21. out = new PrintWriter(socket.getOutputStream(),true);   
  22. out.println(line.readLine());   
  23. in = new BufferedReader(new InputStreamReader(socket.getInputStream()));   
  24. System.out.println(in.readLine());   
  25. out.close();   
  26. in.close();   
  27. socket.close();   
  28. }catch(IOException e)   
  29. {   
  30. out.println("Wrong");   
  31. }   
  32. }   
  33. public static void main(String[] args)   
  34. {   
  35. new Client();   
  36. }   
  37. };   


         6.随机产生0.0-0.1的双精度随机数
              
  1. Double i = Math.random();  
  2.               int b = (int)(Math.random()*1000); //0-1000的整随机数  
  3.               int c = (int)(Math.random()*(max-min)+min); //产生于两整数间的随机数  

         7.单例模式
  1. public class Singleton {   
  2.   private Singleton(){}   
  3.   //在自己内部定义自己一个实例,是不是很奇怪?   
  4.   //注意这是private 只供内部调用   
  5.   private static Singleton instance = new Singleton();   
  6.   //这里提供了一个供外部访问本class的静态方法,可以直接访问     
  7.   public static Singleton getInstance() {   
  8.     return instance;      
  9.    }   
  10. }   


         8.简单的JDBC连接
         9.折半查找(递归)

  1. package test;  
  2.   
  3. public class Search2 {  
  4.   
  5.     public static void main(String[] args) {  
  6.           
  7.         int [] a = new int[]{12,34,44,54,120,131};  
  8.         int result = searchByTwo(a,0,a.length-1,120);  
  9.         if(result ==-1){  
  10.             System.out.println("未找到");}  
  11.         else{  
  12.             System.out.println("该数据所在位置"+(result+1));  
  13.             }  
  14.         }  
  15.       
  16.       
  17.     public static int searchByTwo(int[] array ,int l0,int hi,int x){  
  18.         //当a[i] = x,并且i>=0时,返回i,否则返回-1  
  19.         if(l0 > hi){  
  20.             return -1;  
  21.         }  
  22.         int i = (l0+hi)/2;  
  23.         if(array[i]==x){  
  24.             return i;  
  25.         }else if(array[i]<x){  
  26.             return searchByTwo(array,i+1,hi,x);  
  27.             }else{  
  28.                   
  29.                 return searchByTwo(array,l0,i-1,x);  
  30.             }  
  31.       
  32.     }  
  33.       
  34. }  


         10.汉诺塔(递归)
  1. public class Hanoi {  
  2.   
  3.     public static void main(String[] args) {  
  4.   
  5.     //汉诺塔     
  6.    /* 
  7.        三步走战略: 
  8.         1.将柱x上较小的n-1个盘移到柱Z 
  9.         2.将柱x上的剩余盘移到柱y 
  10.         3.将柱z上的n-1个盘移到柱y 
  11.     */  
  12.        runHanoi(4'A''B''C');  
  13.       
  14.     }  
  15.   
  16.   
  17. public static void runHanoi(int n,char x,char y,char z){  
  18.         if(n==1){  
  19.             System.out.println("move top disk from peg " + x+" to peg "+z);  
  20.         }else {  
  21.             runHanoi(n-1,x,z,y);  
  22.             runHanoi(1,x,y,z);  
  23.             runHanoi(n-1,y,x,z);  
  24.               
  25.         }  
  26.     }  
  27.       
  28. }  
  29.       


         11.不使用Synchronized关键字保证foo()线程的安全
         12.如果系统要使用超大整数(超过long长度)设计一个数据结构来存储这种超大型整数及设计一种算法实现超大整数的加法运算
            
  1. package test;  
  2.   
  3. public class LongAdd {  
  4.   
  5. public static String doAdd(String str1,String str2){  
  6.     String str ="";//相加后的字符串  
  7.     String strTmp = ""//补零对齐  
  8.     int jw = 0//进位  
  9.     int temp;  //每一位相加的结果  
  10.     int len1 = str1.length();  
  11.     int len2 = str2.length();  
  12.     int maxLength = (len1>len2)?len1:len2;  
  13.     int minLength = (len1<len2)?len1:len2;  
  14.       
  15.     for(int i =maxLength-minLength;i>0;i++){  
  16.         strTmp += "0";  
  17.     }  
  18.     if(maxLength == len1){  
  19.         str2 = strTmp +str2;  
  20.     }else{  
  21.         str1 = strTmp +str1;  
  22.     }  
  23.       
  24.     for(int i =maxLength-1;i>=0;i--){  
  25.         int temp1 = Integer.parseInt(String.valueOf(str1.charAt(i)));  
  26.         int temp2 = Integer.parseInt(String.valueOf(str2.charAt(i)));  
  27.       
  28.     if(temp1+temp2+jw>=10&&i!=0){  
  29.         temp = temp1+temp2+jw-10;  
  30.         jw = 1;  
  31.     }else{  
  32.         temp = temp1+temp2+jw;  
  33.         jw = 0;  
  34.     }  
  35.         str = String.valueOf(temp)+str;  
  36.     }  
  37.     return str;  
  38. }   
  39.   
  40. public static void main(String[] args) {  
  41.     String str1 = "1234567890123456789";  
  42.     System.out.println("str1:\n"+str1);  
  43.     System.out.println("result:\n"+doAdd(str1,str1));  
  44. }  
  45. }  

         13 设计四个线程
          
  1. public class ThreadTest1{  
  2. private int j;  
  3. public static void main(String args[]){  
  4. ThreadTest1 tt=new ThreadTest1();  
  5. Inc inc=tt.new Inc();  
  6. Dec dec=tt.new Dec();  
  7. for(int i=0;i<2;i++){  
  8. Thread t=new Thread(inc);  
  9. t.start();  
  10. t=new Thread(dec);  
  11. t.start();  
  12. }  
  13. }  
  14. private synchronized void inc(){  
  15. j++;  
  16. System.out.println(Thread.currentThread().getName()+"-inc:"+j);  
  17. }  
  18. private synchronized void dec(){  
  19. j--;  
  20. System.out.println(Thread.currentThread().getName()+"-dec:"+j);  
  21. }  
  22. class Inc implements Runnable{  
  23. public void run(){  
  24. for(int i=0;i<100;i++){  
  25. inc();  
  26. }  
  27. }  
  28. }  
  29. class Dec implements Runnable{  
  30. public void run(){  
  31. for(int i=0;i<100;i++){  
  32. dec();  
  33. }  
  34. }  
  35. }  
  36. }  
   14.编写File程序,实现以下功能
      1.计算文件/文件夹的大小(M) 
      2.判断操作对象为文件OR文件夹,如果是文件,输出文件内容
         否则列出该文件夹下所有目录
 
  1. public static void main(String[] args) throws Exception   
  2.    {  
  3.        getFile(new File("C:\\entityBean"),"\t");  
  4.    }  
  5.     public static void getFile(File f,String sem) throws Exception  
  6.     {  
  7.         System.out.println(sem+f.getName());  
  8.         File fl[]=f.listFiles();  
  9.         if(fl.length>=1)  
  10.         {  
  11.             for(int i=0;i<fl.length;i++)  
  12.             {  
  13.                 if(fl[i].isDirectory())  
  14.                 {  
  15.                     getFile(fl[i],sem+"\t");  
  16.                 }  
  17.             }  
  18.         }  
  19.     }  
   15.编写一个算法,实现一个字符串所有字符的全排列,并记录排列总数
  1. package test.hello;  
  2. public class AllCombString {   
  3. public static int t;//组合个数   
  4.   
  5. public static void main(String[] args) {   
  6.   String str = "abcdefg";   
  7.   char[] c = str.toCharArray();   
  8.   t++;   
  9.   allCombString(c,0);   
  10.   System.out.println(t);   
  11. }   
  12. public static void allCombString(char[] c,int s){   
  13.   int l = c.length;   
  14.   if(l-s==2){   
  15.    char temp = c[l-1];   
  16.    c[l-1] = c[l-2];   
  17.    c[l-2] = temp;   
  18.    println(c);   
  19.    t++;   
  20.   }   
  21.   else{   
  22.    for(int i=s;i<l;i++){   
  23.     moveToHead(c,i,s);   
  24.     char ct[] = new char[l];   
  25.     System.arraycopy(c, 0, ct, 0, l);//保持其他元素位置不变   
  26.     allCombString(ct,s+1);   
  27.    }   
  28.   }   
  29. }   
  30.   
  31. public static void moveToHead(char[] c,int id,int s){   
  32.   if(id>s&&id<c.length){   
  33.    char temp = c[id];   
  34.    for(int i=id;i>s;i--){   
  35.     c[i] = c[i-1];   
  36.    }   
  37.    c[s] = temp;   
  38.    println(c);   
  39.    t++;   
  40.   }   
  41. }   
  42.   
  43. public static void println(char[] c){   
  44.   System.out.println(new String(c));   
  45. }   
  46. }   

16.请在下面程序相应位置填充合适代码,实现提示功能
public class RightDemo {
public static void main(String[] args) {
int i = 0;
{
        //添加一段代码使得控制台输出right

}
if (i + 1 < i) {
System.out.println("right");
} else {
System.out.println("wrong");
}
}
}
  1. i = Integer.MAX_VALUE;//  
  2.       

java中int占4个字节,存储范围为--214783648 ~- +214783647, 214783647+1会产生溢出,使符号位由0变1,即负数最小值

相关链接 :java 面试


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值