1  Java面向对象
  2 1.        super()与this()的区别?
  3 This():当前类的对象,super父类对象。
  4 
  5 Super():在子类访问父类的成员和行为,必须受类继承规则的约束
  6 
  7 而this他代表当前对象,当然所有的资源都可以访问.
  8 
  9 在构造函数中,如果第一行没有写super(),编译器会自动插入.但是如果父类没有不带参数的构造函数,或这个函数被私有化了(用private修饰).此时你必须加入对父类的实例化构造.而this就没有这个要求,因为它本身就进行实例化的构造.
 10 而在方法中super和this使用的方法就差不多了.只不过super 要考虑是否能访问其父类的资源.
 11 
 12  
 13 
 14 2.        作用域public,protected,private,以及不写时的区别?
 15 ?         Public:不同包、同一包、类内都可用
 16 
 17 ?         Private:类内
 18 
 19 ?         Protected: 不同包的子类、同一包、类内都可用
 20 
 21 ?         不写时:同一包内、类内
 22 
 23 3.        编程输出如下图形。
 24 * * * * *
 25 
 26 * * * *
 27 
 28 * * *
 29 
 30 * *
 31 
 32 *
 33 
 34 代码如下:
 35 
 36 public class Print {
 37 
 38     public static void main(String[] args) {
 39 
 40        for (int i = 0; i < 5; i++) {
 41 
 42            for (int j = 5; j > i; j--) {
 43 
 44               System.out.print("*");
 45 
 46            }
 47 
 48            System.out.println();
 49 
 50        }
 51 
 52     }
 53 
 54 }
 55 
 56 4.        JAVA的事件委托机制和垃圾回收机制
 57 java 事件委托机制的概念,一个源产生一个事件并将它送到一个或多个监听器那里。在这种方案中,监听器简单的等待,直到它收到一个事件。一旦事件被接受,监听器将处理这个事件,然后返回。
 58 
 59 垃圾回收机制 垃圾收集是将分配给对象但不再使用的内存回收或释放的过程。如果一个对象没有指向它的引用或者其赋值为null,则次对象适合进行垃圾回收
 60 
 61  
 62 
 63 5.        在JAVA中,如何跳出当前的多重嵌套循环?
 64 用break; return 方法。
 65 
 66 6.        什么是java序列化,如何实现java序列化?(写一个实例)
 67 序列化:
 68 
 69     可以将一个对象保存到一个文件,所以可以通过流的方式在网络上传输,可以将文件的内容读取,转化为一个对象。
 70 
 71  
 72 
 73  
 74 
 75 处理对象流的机制,所谓对象流也就是将对象的内容进行流化。可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间。序列化是为了解决在对对象流进行读写操作时所引发的问题。
 76 
 77 序列化的实现:
 78 
 79 将需要被序列化的类实现Serializable接口,该接口没有需要实现的方法,implements Serializable只是为了标注该对象是可被序列化的,然后使用一个输出流(如:FileOutputStream)来构造一个ObjectOutputStream(对象流)对象,接着,使用ObjectOutputStream对象的writeObject(Object obj)方法就可以将参数为obj的对象写出(即保存其状态),要恢复的话则用输入流。
 80 
 81  
 82 
 83 7.        一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制?
 84 可以。如果这个类的修饰符是public,其类名与文件名必须相同。
 85 
 86 8.        排序都有哪几种方法?请列举。用JAVA实现一个快速排序?
 87 排序的方法有:插入排序(直接插入排序、希尔排序),交换排序(冒泡排序、快速排序),选择排序(直接选择排序、堆排序),归并排序,分配排序(箱排序、基数排序)
 88 
 89 快速排序的伪代码。
 90 
 91 9.        Overload和Override的区别。Overloaded的方法是否可以改变返回值的类型?
 92 方法的
 93 
 94 重写Override,子类覆盖父类的方法,将子类传与父类的引用调用的还是子类的方法。
 95 
 96 重载Overloading 一个类多个方法,名称相同,参数个数类型不同。
 97 
 98 两者都是Java多态性的不同表现。
 99 
100 Overloaded的方法是可以改变返回值的类型。
101 
102  
103 
104 1, public class Ctest()
105 
106 {
107 
108     Public static void main()
109 
110 {
111 
112 System.out.prinln(8+8+”88”+8+8);
113 
114 }
115 
116 }
117 
118  
119 
120 168888
121 
122 10.    Final类有什么特点?
123        属性常量
124 
125        方法不可以overridding
126 
127        类不可以继承
128 
129 11.    继承时候类的执行顺序问题,一般都是选择题,问你将会打印出什么? 
130 答:父类: 
131 
132 package test; 
133 
134 public class FatherClass 
135 
136 { 
137 
138 public FatherClass() 
139 
140 { 
141 
142 System.out.println("FatherClass Create"); 
143 
144 } 
145 
146 } 
147 
148 子类: 
149 
150 package test; 
151 
152 import test.FatherClass; 
153 
154 public class ChildClass extends FatherClass 
155 
156 { 
157 
158 public ChildClass() 
159 
160 { 
161 
162 System.out.println("ChildClass Create"); 
163 
164 } 
165 
166 public static void main(String[] args) 
167 
168 { 
169 
170 FatherClass fc = new FatherClass(); 
171 
172 ChildClass cc = new ChildClass(); 
173 
174 } 
175 
176 } 
177 
178 输出结果: 
179 
180 C:>java test.ChildClass 
181 
182 FatherClass Create 
183 
184 FatherClass Create 
185 
186 ChildClass Create
187 
188 12.    内部类的实现方式? 
189 答:示例代码如下: 
190 
191 package test; 
192 
193 public class OuterClass 
194 
195 { 
196 
197 private class InterClass 
198 
199 { 
200 
201 Public Interlass() 
202 
203 { 
204 
205 System.out.println("InterClass Create"); 
206 
207 } 
208 
209 } 
210 
211 public OuterClass() 
212 
213 { 
214 
215 InterClass ic = new InterClass(); 
216 
217 System.out.println("OuterClass Create"); 
218 
219 } 
220 
221 public static void main(String[] args) 
222 
223 { 
224 
225 OuterClass oc = new OuterClass(); 
226 
227 } 
228 
229 } 
230 
231 输出结果: 
232 
233 C:>java test/OuterClass 
234 
235 InterClass Create 
236 
237 OuterClass Create 
238 
239 13.    用JAVA实现一种排序,JAVA类实现序列化的方法(二种)?
240 14.     如在COLLECTION框架中,实现比较要实现什么样的接口? 
241 15.  用插入法进行排序代码如下 
242 package test; 
243 
244 import java.util.*; 
245 
246 class InsertSort 
247 
248 { 
249 
250 ArrayList al; 
251 
252 public InsertSort(int num,int mod) 
253 
254 { 
255 
256 al = new ArrayList(num); 
257 
258 Random rand = new Random(); 
259 
260 System.out.println("The ArrayList Sort Before:"); 
261 
262 for (int i=0;i<num ;i++ ) 
263 
264 { 
265 
266 al.add(new Integer(Math.abs(rand.nextInt()) % mod + 1)); 
267 
268 System.out.println("al["+i+"]="+al.get(i)); 
269 
270 } 
271 
272 } 
273 
274 public void SortIt() 
275 
276 { 
277 
278 Integer tempInt; 
279 
280 int MaxSize=1; 
281 
282 for(int i=1;i<al.size();i++) 
283 
284 { 
285 
286 tempInt = (Integer)al.remove(i); 
287 
288 if(tempInt.intValue()>=((Integer)al.get(MaxSize-1)).intValue()) 
289 
290 { 
291 
292 al.add(MaxSize,tempInt); 
293 
294 MaxSize++; 
295 
296 System.out.println(al.toString()); 
297 
298 } else { 
299 
300 for (int j=0;j<MaxSize ;j++ ) 
301 
302 { 
303 
304 if 
305 
306  
307 
308 (((Integer)al.get(j)).intValue()>=tempInt.intValue()) 
309 
310 { 
311 
312 al.add(j,tempInt); 
313 
314 MaxSize++; 
315 
316 System.out.println(al.toString()); 
317 
318 break; 
319 
320 } 
321 
322 } 
323 
324 } 
325 
326 } 
327 
328 System.out.println("The ArrayList Sort After:"); 
329 
330 for(int i=0;i<al.size();i++) 
331 
332 { 
333 
334 System.out.println("al["+i+"]="+al.get(i)); 
335 
336 } 
337 
338 } 
339 
340 public static void main(String[] args) 
341 
342 { 
343 
344 InsertSort is = new InsertSort(10,100); 
345 
346 is.SortIt(); 
347 
348 } 
349 
350 } 
351 
352  
353 
354  
355 
356 JAVA类实现序例化的方法是实现java.io.Serializable接口 
357 
358 Collection框架中实现比较要实现Comparable 接口和 Comparator 接口
359 
360  
361 
362 16.    编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输入"我ABC汉DEF",6,应该输出为"我ABC"而不是"我ABC+汉的半个"363 答:代码如下: 
364 
365 public static void split(String source,int num) throws Exception
366 
367     {
368 
369         int k=0;
370 
371         String temp="";
372 
373         for (int i = 0; i <source.length(); i++)
374 
375         {   
376 
377             byte[] b=(source.charAt(i)+"").getBytes();
378 
379             k=k+b.length;
380 
381             if(k>num)
382 
383             {
384 
385                 break;
386 
387             }
388 
389             temp=temp+source.charAt(i);         
390 
391         }   
392 
393         System.out.println(temp);
394 
395     }
396 
397  
398 
399 15、Java编程,打印昨天的当前时刻
400 public class YesterdayCurrent{
401 
402 public void main(String[] args){
403 
404 Calendar cal = Calendar.getInstance();
405 
406 cal.add(Calendar.DATE, -1);
407 
408 System.out.println(cal.getTime());
409 
410 }
411 
412 } 
413 
414 16、文件读写,实现一个计数器
415 public int getNum(){
416 
417 int i = -1;
418 
419 try{
420 
421 String stri="";
422 
423 BufferedReader in = new BufferedReader(new FileReader(f));
424 
425 while((stri=in.readLine())!=null){
426 
427 i = Integer.parseInt(stri.trim());
428 
429 }
430 
431 in.close();
432 
433 }catch(Exception e){
434 
435 e.printStackTrace();
436 
437 }
438 
439 return i;
440 
441 }
442 
443 public void setNum(){
444 
445 int i = getNum();
446 
447 i++; 
448 
449 try{
450 
451 PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(f,false))); 
452 
453 out.write(String.valueOf(i)); //可能是编码的原因,如果直接写入int的话,将出现java编码和windows编码的混乱,因此此处写入的是String
454 
455 out.close() ;
456 
457 }catch(Exception e){
458 
459 e.printStackTrace();
460 
461 }
462 
463 } 
464 
465  
466 
467 17、指出下面程序的运行结果。
468 class A{
469 static{
470 System.out.print("1");
471 }
472 public A(){
473 System.out.print("2");
474 }
475 }
476 class B extends A{
477 static{
478 System.out.print("a");
479 }
480 public B(){
481 System.out.print("b");
482 } 
483 }
484 public class Hello{
485 public static void main(String[] ars){
486 A ab = new B(); //执行到此处,结果: 1a2b
487 ab = new B(); //执行到此处,结果: 1a2b2b
488 }
489 }
490 
491   注:类的static 代码段,可以看作是类首次加载(被虚拟机加载)执行的代码,而对于类的加载,首先要执行其基类的构造,再执行其本身的构造
492 
493 18、抽象类和接口的区别?
494 (1)接口可以被多重implements,抽象类只能被单一extends
495 
496 (2)接口只有定义,抽象类可以有定义和实现
497 
498 (3)接口的字段定义默认为:public static final, 抽象类字段默认是"friendly"(本包可见)
499 
500 当功能需要累积时用抽象类,不需要累积时用接口。
501 
502 19、什么是类的返射机制?
503 通过类(Class对象),可以得出当前类的fields、method、construtor、interface、superClass、modified等,同是可以通过类实例化一个实例、设置属性、唤醒方法。Spring中一切都是返射、struts、hibernate都是通过类的返射进行开发的。
504 
505 20、类的返射机制中的包及核心类?
506 ?  java.lang.Class
507 
508 ?  java.lang.refrection.Method
509 
510 ?  java.lang.refrection.Field
511 
512 ?  java.lang.refrection.Constructor
513 
514 ?  java.lang.refrection.Modifier
515 
516 ?  java.lang.refrection.Interface
517 
518 21、得到Class的三个过程是什么?
519 对象.getClass()
520 
521         类.class或Integer.type(int)  Integer.class(java.lang.Integer)
522 
523         Class.forName();
524 
525 22、如何唤起类中的一个方法?
526 产生一个Class数组,说明方法的参数
527 
528 通过Class对象及方法参数得到Method
529 
530 通过method.invoke(实例,参数值数组)唤醒方法
531 
532 23、如何将数值型字符转换为数字(Integer,Double)?
533 Integer.parseInt(“1234”)
534 
535 Double.parseDouble(“123.2”)
536 
537 24、如何将数字转换为字符?
538 1+””
539 
540 1.0+””
541 
542 25、如何去小数点前两位,并四舍五入。
543 double d=1256.22d;
544 
545 d=d/100;
546 
547 System.out.println(Math.round(d)*100);
548 
549 26、如何取得年月日,小时分秒?
550 Calendar c=Calendar.getInstance();
551 
552         c.set(Calendar.YEAR,2004);
553 
554         c.set(Calendar.MONTH,0);
555 
556         c.set(Calendar.DAY_OF_MONTH,31);
557 
558         System.out.println(c.get(Calendar.YEAR)+"  "+(c.get(Calendar.MONTH)+1)+"  "+c.get(Calendar.DAY_OF_MONTH));
559 
560 27、如何取得从1970年到现在的毫秒数
561 Java.util.Date dat=new Date();
562 
563 long now=dat.getTime();
564 
565 28、如何获取某个日期是当月的最后一天?
566 当前日期加一天,若当前日期与结果的月份不相同,就是最后一天。
567 
568  
569 
570 取下一个月的第一天,下一个月的第一天-1
571 
572  
573 
574 public static void main(String[] args)
575 
576     {
577 
578         Calendar c=Calendar.getInstance();
579 
580         c.set(Calendar.YEAR,2004);
581 
582         c.set(Calendar.MONTH,0);
583 
584         c.set(Calendar.DAY_OF_MONTH,30);
585 
586         Calendar c1=(Calendar)c.clone();
587 
588         System.out.println(c.get(Calendar.YEAR)+"  "+(c.get(Calendar.MONTH)+1)+"  "+c.get(Calendar.DAY_OF_MONTH));
589 
590         
591 
592         c.add(Calendar.DAY_OF_MONTH,1);
593 
594         if(c.get(Calendar.MONTH)!=c1.get(Calendar.MONTH))
595 
596         {
597 
598             System.out.println("是最后一天");
599 
600         }
601 
602         else
603 
604         {
605 
606             System.out.println("不是取后一天");
607 
608             
609 
610         }
611 
612     } 
613 
614 29、如何格式化日期?
615 Import java.text. SimpleDateFormat;
616 
617 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
618 
619 Date dat=new Date();
620 
621 //把日期转化为字符串
622 
623 String str=sdf.format(dat);
624 
625 System.out.println(str);
626 
627 //将字符串转化为日期
628 
629 Java.util.Date d1=sdf.parse(“yyyy-mm-dd”);
630 
631 30、编码转换,怎样实现将GB2312编码的字符串转换为ISO-8859-1编码的字符串。
632 String a=new String("中".getBytes("gb2312"),"iso-8859-1");
633 
634  
635 
636  
637 
638 String a=new String("中".getBytes("iso-8859-1"));
639 
640  
641 
642 32、String s = new String("xyz");创建了几个String Object?
643 New了一个,”XYZ”本来又是一个
644 
645 两个
646 
647 33、float型float f=3.4是否正确?
648 ?  报错,应当是float f=3.4f
649 
650 ?  如果是float f=3(整数)正确
651 
652 35、说出一些常用的类,包,接口,请各举5个
653 常用的类:BufferedReader  BufferedWriter  FileReader  FileWirter  String  Integer
654 
655 常用的包:java.lang  java.awt  java.io  java.util  java.sql javax.xml javax.sevlet javax.ejb.  java.net  javax.faces  
656 
657 常用的接口: List  Map  Document  NodeList EjbObject EjbHome SessionBean  EntityBean
658 
659 36、java中会存在内存泄漏吗,请简单描述。
660 会。如:int i,i2;  return (i-i2);   //when i为足够大的正数,i2为足够大的负数。结果会造成溢位,导致错误。
661 
662 37、java中实现多态的机制是什么?
663        静态的多态:方法名相同,参数个数或类型不相同。(overloading)
664 
665        动态的多态:
666 
667               子类覆盖父类的方法,将子类的实例传与父类的引用调用的是子类的方法
668 
669               实现接口的实例传与接口的引用调用的实现类的方法。       
670 
671 38、垃圾回收器的基本原理是什么?垃圾回收器可以马上回收内存吗?有什么办法主动通知虚拟机进行垃圾回收?
672 动态内存
673 
674         存放类实例
675 
676 静态内存
677 
678 类本身
679 
680 垃圾收集主要针对的是动态内存,一般当内存不够用时会进行垃圾收集。
681 
682 或通过System.gc()手动收集,但不保证一定执行。 
683 
684 39、静态变量和实例变量的区别?
685 static i = 10; //常量
686 
687   class A a;  a.i =10;//可变
688 
689 静态方法可以调用静态变量。
690 
691 实现方法可以调用静态变量、实例变量
692 
693 41、是否可以从一个static方法内部发出对非static方法的调用?
694 不可以,如果其中包含对象的method();不能保证对象初始化.
695 
696 42、写clone()方法时,通常都有一行代码,是什么?
697 Clone 有缺省行为,super.clone();他负责产生正确大小的空间,并逐位复制。
698 
699 43、JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?在try块中可以抛出异常吗?
700 Try:执行部分,产生异常
701 
702 Catch:捕捉异常
703 
704 Finally:不管有没有异常都执行
705 
706 Throws:在方法声明处声明要抛出的异常,调用者必须对其进行处理。
707 
708 Throw:抛出一个异常
709 
710  
711 
712 在try中可以抛出异常,一般与声明的异常相同。
713 
714  
715 
716 自定义异常要继承于Exception或Exception的子类
717 
718 45、冒泡排序法
719 //相邻两个数比较,将最小或最大的放到后面,最后面数的不参与比较
720 
721 public class BubbleSort {
722 
723     private static int al[] = new int[10];
724 
725     public BubbleSort() {
726 
727        al[0]=2;
728 
729        al[1]=3;
730 
731        al[2]=23;
732 
733        al[3]=45;
734 
735        al[4]=1;
736 
737        al[5]=67;
738 
739        al[6]=23;
740 
741        al[7]=80;
742 
743        al[8]=35;
744 
745        al[9]=72;
746 
747     }
748 
749     public static void main(String[] args) {
750 
751        BubbleSort bs = new BubbleSort();
752 
753        System.out.println("排序前:");
754 
755        display(al);
756 
757        
758 
759        for(int i=0;i<al.length;i++) {
760 
761        
762 
763            for (int j = 0; j < al.length-i-1; j++) {
764 
765               
766 
767               if(al[j]>al[j+1]) {
768 
769                   swap(j,j+1);
770 
771               }
772 
773            }
774 
775        }
776 
777        System.out.println();
778 
779        System.out.println("排序后:");
780 
781        display(al);
782 
783     }
784 
785     private static void display(int[] al2) {
786 
787        for (int i = 0; i < al2.length; i++) {
788 
789            System.out.print(al2[i]+"  ");
790 
791        }
792 
793     }
794 
795     private static void swap(int i, int j) {
796 
797        int temp = al[i];
798 
799        al[i]= al[j];
800 
801        al[j] = temp;
802 
803     }
804 
805 }
806 
807 46、String and StringBuffer的区别?
808 String:长度给定不可变,当多个字符串联合时要先转为StringBuffer,再联合,速度慢。
809 
810 StringBuffer:长度可变,可以将多个字符串值直接联合,效率高
811 
812 47、用java代码编写堆栈
813 public class Stack {
814 
815  
816 
817     int[] data;
818 
819     int maxSize;
820 
821     int top;
822 
823     public Stack(int maxSize) {
824 
825        this.maxSize = maxSize;
826 
827        data = new int[maxSize];
828 
829        top = -1;
830 
831     }
832 
833     
834 
835     /**
836 
837      * 依次加入数据
838 
839      * @param data 要加入的数据
840 
841      * @return 添加是否成功
842 
843      */
844 
845     public boolean push(int data) {
846 
847        if(top+1== maxSize) {
848 
849            System.out.println("栈已满!");
850 
851            return false;
852 
853        }
854 
855        this.data[++top] = data;
856 
857        return true;
858 
859     }
860 
861     
862 
863     /**
864 
865      * 从栈中取出数据
866 
867      * @return 取出的数据
868 
869      */
870 
871     public int pop() throws Exception{
872 
873        if(top==-1) {
874 
875            throw new Exception("栈已空!");
876 
877        }
878 
879        return this.data[top--];
880 
881     }
882 
883     
884 
885     public static void main(String[] args) throws Exception {
886 
887        Stack stack=new Stack(1000);
888 
889        stack.push(1);
890 
891        stack.push(2);
892 
893        stack.push(3);
894 
895        stack.push(4);
896 
897        stack.push(5);
898 
899        while(stack.top>=0)
900 
901        {
902 
903            System.out.println(stack.pop());
904 
905        }      
906 
907     }
908 
909 }
910 
911 48、集合的作用是什么?
912 数据的传送 增、删、改、查、constainsAll,可以存放不同类型的对象。
913 
914 49、集合的通用方法有那些?通用方法是什么?(操作)
915 集合List 的遍历方法有:
916 
917 Iterator:
918 
919 Enumeration
920 
921 For
922 
923 Get
924 
925 set
926 
927 Collection的通用方法有:
928 
929     Iterator()
930 
931     Add()
932 
933     Clear();
934 
935     remove()
936 
937     
938 
939 50、说出ArrayList,Vector, LinkedList的存储性能和特性HashMap和Hashtable的区别
940 ArrayList Vector:以数组的方式存储,增、删慢,查、改快
941 
942     ArrayList:线程不安全,速度快
943 
944     ArrayList:线程安全,速度慢(synchoronized)
945 
946 LikedList: 以单链表的方式存储,增、删快,查、改慢
947 
948  
949 
950 HashMap与Hashtable都实现的Map接口,HashTable线程安全,HashMap线程不安全。
951 
952 51、Collection 和 Collections的区别。
953 Collection是集合的根接口,其下有set及list
954 
955 Collections是集合的算法。
956 
957 52、Set里的元素是不能重复的,那么用什么方法来区分重复与否呢? 是用==还是equals()? 它们有何区别?用contains来区分是否有重复的对象。还是都不用。
958  
959 
960 在比较时先调用hashCode方法,如果不相同,证明不相等。
961 
962 如果相同,再调用equals方法,如果equals方法相同,证明相等,不相同,证明不相等。
963 
964  
965 
966 ==:主要用在基本数据类型及引用
967 
968 Equals:主要是对象或对象引用的比较。
969 
970  
971 
972 集合中是否包含某一个元素用contains来判断。
973 
974 53、List, Set, Map是否继承自Collection接口?
975 List,set继承于Collection
976 
977 Map没有继承于Collection,其相对是独立的。
978 
979 属于Collection类型的对象,可以通过构造函数将一个集合构造成另外一个集合。
View Code

相关文章:

  • 2021-12-04
猜你喜欢
  • 2022-02-04
  • 2021-09-29
  • 2022-12-23
  • 2021-09-04
  • 2022-01-18
  • 2021-06-01
  • 2021-08-04
相关资源
相似解决方案