经典完整java面试题大全

本文涵盖了Java面试中的重点知识点,包括面向对象、异常处理、内存管理、集合框架、数据库操作、Web技术等多个方面。从super()与this()的区别、访问修饰符的用法到排序算法、多线程、反射机制、序列化,再到JDBC、Servlet、Struts、Hibernate等技术的应用,全面解析Java开发的关键点。同时,文章也涉及到EJB、WebService、设计模式和J2EE架构等内容,是Java开发者面试准备的宝贵资料。
摘要由CSDN通过智能技术生成

Java面向对象

1.       super()与this()的区别?

This():当前类的对象,super父类对象。html

Super():在子类访问父类的成员和行为,必须受类继承规则的约束前端

而this他表明当前对象,固然全部的资源均可以访问.java

在构造函数中,若是第一行没有写super(),编译器会自动插入.可是若是父类没有不带参数的构造函数,或这个函数被私有化了(用private修饰).此时你必须加入对父类的实例化构造.而this就没有这个要求,由于它自己就进行实例化的构造.
而在方法中super和this使用的方法就差很少了.只不过super 要考虑是否能访问其父类的资源.程序员

2.       做用域public,protected,private,以及不写时的区别?

Ø        Public:不一样包、同一包、类内均可用web

Ø        Private:类内算法

Ø        Protected: 不一样包的子类、同一包、类内均可用spring

Ø        不写时:同一包内、类内sql

3.       编程输出以下图形。

* * * * *数据库

* * * *apache

* * *

* *

*

代码以下:

1

2

3

4

5

6

7

8

9

10

public  class  Print {

     public  static  void  main(String[] args) {

        for  ( int  i =  0 ; i <  5 ; i++){

            for  ( int  j =  5 ; j > i; j--) {

               System.out.print( "*" );

            }

            System.out.println();

        }

     }

}

4.       JAVA的事件委托机制和垃圾回收机制

java 事件委托机制的概念,一个源产生一个事件并将它送到一个或多个监听器那里。在这种方案中,监听器简单的等待,直到它收到一个事件。一旦事件被接受,监听器将处理这个事件,而后返回。

垃圾回收机制 垃圾收集是将分配给对象但再也不使用的内存回收或释放的过程。若是一个对象没有指向它的引用或者其赋值为null,则次对象适合进行垃圾回收

5.       在JAVA中,如何跳出当前的多重嵌套循环?

用break; return 方法。

6.       什么是java序列化,如何实现java序列化?(写一个实例)

序列化:

处理对象流的机制,所谓对象流也就是将对象的内容进行流化。能够对流化后的对象进行读写操做,也可将流化后的对象传输于网络之间。序列化是为了解决在对对象流进行读写操做时所引起的问题。

序列化的实现:

将须要被序列化的类实现Serializable接口,该接口没有须要实现的方法,implements Serializable只是为了标注该对象是可被序列化的,而后使用一个输出流(如:FileOutputStream)来构造一个ObjectOutputStream(对象流)对象,接着,使用ObjectOutputStream对象的writeObject(Object obj)方法就能够将参数为obj的对象写出(即保存其状态),要恢复的话则用输入流。

7.       一个".java"源文件中是否能够包括多个类(不是内部类)?有什么限制?

能够。若是这个类的修饰符是public,其类名与文件名必须相同。

8.       排序都有哪几种方法?请列举。用JAVA实现一个快速排序?

排序的方法有:插入排序(直接插入排序、希尔排序),交换排序(冒泡排序、快速排序),选择排序(直接选择排序、堆排序),归并排序,分配排序(箱排序、基数排序)

快速排序的伪代码。

9.       Overload和Override的区别。Overloaded的方法是否能够改变返回值的类型?

方法的

重写Override,子类覆盖父类的方法,将子类传与父类的引用调用的仍是子类的方法。

重载Overloading 一个类多个方法,名称相同,参数个数类型不一样。

二者都是Java多态性的不一样表现。

Overloaded的方法是能够改变返回值的类型。

1

2

3

4

5

6

7

8

1 ,  public  class  Ctest()

{

     Public  static  void  main()

{

System.out.prinln( 8 + 8 +” 88 ”+ 8 + 8 );

}

}

168888

10.  Final类有什么特色?

      属性常量

      方法不能够overridding

      类不能够继承

11.  继承时候类的执行顺序问题,通常都是选择题,问你将会打印出什么?

答:父类:

1

2

3

4

5

6

7

8

package  test;

public  class  FatherClass

{

public  FatherClass()

{

System.out.println( "FatherClassCreate" );

}

}

子类:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

package  test;

import  test.FatherClass;

public  class  ChildClass  extends  FatherClass

{

public  ChildClass()

{

System.out.println( "ChildClass Create" );

}

public  static  void  main(String[] args)

{

FatherClass fc =  new  FatherClass();

ChildClass cc =  new  ChildClass();

}

}

输出结果:

C:>java test.ChildClass

FatherClass Create

FatherClass Create

ChildClass Create

12. 内部类的实现方式?

答:示例代码以下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

package  test;

public  class  OuterClass

{

private  class  InterClass

{

Public Interlass()

{

System.out.println( "InterClass Create" );

}

}

public  OuterClass()

{

InterClass ic =  new  InterClass();

System.out.println( "OuterClassCreate" );

}

public  static  void  main(String[] args)

{

OuterClass oc =  new  OuterClass();

}

}

输出结果:

C:>java test/OuterClass

InterClass Create

OuterClass Create

13.   用JAVA实现一种排序,JAVA类实现序列化的方法(二种)?

14.    如在COLLECTION框架中,实现比较要实现什么样的接口?

15. 用插入法进行排序代码以下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

package  test;

import  java.util.*;

class  InsertSort{

ArrayList al;

public  InsertSort( int  num, int  mod)

{

al =  new  ArrayList(num);

Random rand =  new  Random();

System.out.println( "The ArrayList Sort Before:" );

for  ( int  i= 0 ;i<num ;i++ )

{

al.add( new  Integer(Math.abs(rand.nextInt()) % mod +  1 ));

System.out.println( "al[" +i+ "]=" +al.get(i));

}

}

public  void  SortIt()

{

Integer tempInt;

int  MaxSize= 1 ;

for ( int  i= 1 ;i<al.size();i++)

{

tempInt = (Integer)al.remove(i);

if (tempInt.intValue()>=((Integer)al.get(MaxSize- 1 )).intValue())

{

al.add(MaxSize,tempInt);

MaxSize++;

System.out.println(al.toString());

 else  {

for  ( int  j= 0 ;j<MaxSize ;j++ )

{

if

    

(((Integer)al.get(j)).intValue()>=tempInt.intValue())

{

al.add(j,tempInt);

MaxSize++;

System.out.println(al.toString());

break ;

}

}

}

}

System.out.println( "The ArrayList Sort After:" );

for ( int  i= 0 ;i<al.size();i++)

{

System.out.println( "al[" +i+ "]=" +al.get(i));

}

}

public  static  void  main(String[] args)

{

InsertSort is =  new  InsertSort( 10 , 100 );

is.SortIt();

}

}

JAVA类实现序例化的方法是实现java.io.Serializable接口

Collection框架中实现比较要实现Comparable 接口和 Comparator 接口

16.  编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 可是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输入"我ABC汉DEF",6,应该输出为"我ABC"而不是"我ABC+汉的半个"。

答:代码以下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

public  static  void  split(String source, int  num)  throws  Exception

     {

        int  k= 0 ;

        String temp= "" ;

        for  ( int  i =  0 ; i<source.length(); i++)

        {

            byte []b=(source.charAt(i)+ "" ).getBytes();

            k=k+b.length;

            if (k>num)

            {

               break ;

            }

            temp=temp+source.charAt(i);     

        }

        System.out.println(temp);

     }

15、Java编程,打印昨天的当前时刻

1

2

3

4

5

6

7

public  class  YesterdayCurrent{

public  void  main(String[] args){

Calendar cal = Calendar.getInstance();

cal.add(Calendar.DATE, - 1 );

System.out.println(cal.getTime());

}

}

16、文件读写,实现一个计数器

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

public  int  getNum(){

int  i = - 1 ;

try {

String stri= "" ;

BufferedReader in =  new  BufferedReader( new  FileReader(f));

while ((stri=in.readLine())!= null ){

i = Integer.parseInt(stri.trim());

}

in.close();

} catch (Exception e){

e.printStackTrace();

}

return  i;

}

public  void  setNum(){

int  i = getNum();

i++;

try {

PrintWriter out= new  PrintWriter( new  BufferedWriter(newFileWriter(f, false)));

out.write(String.valueOf(i));  //多是编码的缘由,若是直接写入int的话,将出现java编码和windows编码的混乱,所以此处写入的是String

out.close() ;

} catch (Exception e){

e.printStackTrace();

}

}

17、指出下面程序的运行结果。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

class  A{

static {

System.out.print( "1" );

}

public  A(){

System.out.print( "2" );

}

}

class   extends  A{

static {

System.out.print( "a" );

}

public  B(){

System.out.print( "b" );

}

}

public  class  Hello{

public  static  void  main(String[] ars){

A ab =  new  B();  //执行到此处,结果: 1a2b

ab =  new  B();  //执行到此处,结果: 1a2b2b

}

}

  注:类的static 代码段,能够看做是类首次加载(被虚拟机加载)执行的代码,而对于类的加载,首先要执行其基类的构造,再执行其自己的构造

18、抽象类和接口的区别?

(1)接口能够被多重implements,抽象类只能被单一extends

(2)接口只有定义,抽象类能够有定义和实现

(3)接口的字段定义默认为:public static final, 抽象类字段默认是"friendly"(本包可见)

当功能须要累积时用抽象类,不须要累积时用接口。

19、什么是类的返射机制?

经过类(Class对象),能够得出当前类的fields、method、construtor、interface、superClass、modified等,同是能够经过类实例化一个实例、设置属性、唤醒方法。Spring中一切都是返射、struts、hibernate都是经过类的返射进行开发的。

20、类的返射机制中的包及核心类?

  java.lang.Class

  java.lang.refrection.Method

  java.lang.refrection.Field

  java.lang.refrection.Constructor

  java.lang.refrection.Modifier

  java.lang.refrection.Interface

21、获得Class的三个过程是什么?

对象.getClass()

      类.class或Integer.type(int)  Integer.class(java.lang.Integer)

      Class.forName();

22、如何唤起类中的一个方法?

产生一个Class数组,说明方法的参数

经过Class对象及方法参数获得Method

经过method.invoke(实例,参数值数组)唤醒方法

23、如何将数值型字符转换为数字(Integer,Double)?

Integer.parseInt(“1234”)

Double.parseDouble(“123.2”)

24、如何将数字转换为字符?

1+””

1.0+””

25、如何去小数点前两位,并四舍五入。

double d=1256.22d;

d=d/100;

System.out.println(Math.round(d)*100);

26、如何取得年月日,小时分秒?

Calendar c=Calendar.getInstance();

      c.set(Calendar.YEAR,2004);

      c.set(Calendar.MONTH,0);

      c.set(Calendar.DAY_OF_MONTH,31);

      System.out.println(c.get(Calendar.YEAR)+"  "+(c.get(Calendar.MONTH)+1)+"  "+c.get(Calendar.DAY_OF_MONTH));

27、如何取得从1970年到如今的毫秒数

Java.util.Date dat=new Date();

long now=dat.getTime();

28、如何获取某个日期是当月的最后一天?

当前日期加一天,若当前日期与结果的月份不相同,就是最后一天。

取下一个月的第一天,下一个月的第一天-1

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

public  static  void  main(String[] args)

     {

        Calendarc=Calendar.getInstance();

        c.set(Calendar.YEAR, 2004 );

        c.set(Calendar.MONTH, 0 );

        c.set(Calendar.DAY_OF_MONTH, 30 );

        Calendarc1=(Calendar)c.clone();

        System.out.println(c.get(Calendar.YEAR)+ "  " +(c.get(Calendar.MONTH)+1 )+ "  " +c.get(Calendar.DAY_OF_MONTH));

         

        c.add(Calendar.DAY_OF_MONTH, 1 );

        if (c.get(Calendar.MONTH)!=c1.get(Calendar.MONTH))

        {

            System.out.println( "是最后一天" );

        }

        else

        {

            System.out.println( "不是取后一天" );

             

        }

     }

29、如何格式化日期?

Import java.text. SimpleDateFormat;

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");

Date dat=new Date();

//把日期转化为字符串

String str=sdf.format(dat);

System.out.println(str);

//将字符串转化为日期

Java.util.Date d1=sdf.parse(“yyyy-mm-dd”);

30、编码转换,怎样实现将GB2312编码的字符串转换为ISO-8859-1编码的字符串。

String a=new String("中".getBytes("gb2312"),"iso-8859-1");

String a=new String("中".getBytes("iso-8859-1"));

32、String s = new String("xyz");建立了几个StringObject?

New了一个,”XYZ”原本又是一个

两个

33、float型floatf=3.4是否正确?

  报错,应当是float f=3.4f

  若是是float f=3(整数)正确

35、说出一些经常使用的类,包,接口,请各举5个

经常使用的类:BufferedReader BufferedWriter  FileReader  FileWirter String  Integer

经常使用的包:java.lang  java.awt  java.io java.util  java.sql javax.xmljavax.sevlet javax.ejb.  java.net  javax.faces

经常使用的接口: List  Map  Document NodeList EjbObject EjbHome SessionBean EntityBean

36、java中会存在内存泄漏吗,请简单描述。

会。如:int i,i2;  return (i-i2);   //when i为足够大的正数,i2为足够大的负数。结果会形成溢位,致使错误。

37、java中实现多态的机制是什么?

      静态的多态:方法名相同,参数个数或类型不相同。(overloading)

      动态的多态:

             子类覆盖父类的方法,将子类的实例传与父类的引用调用的是子类的方法

             实现接口的实例传与接口的引用调用的实现类的方法。      

38、垃圾回收器的基本原理是什么?垃圾回收器能够立刻回收内存吗?有什么办法主动通知虚拟机进行垃圾回收?

动态内存

      存放类实例

静态内存

类自己

垃圾收集主要针对的是动态内存,通常当内存不够用时会进行垃圾收集。

或经过System.gc()手动收集,但不保证必定执行。

39、静态变量和实例变量的区别?

static i = 10; //常量

 class A a;  a.i =10;//可变

静态方法能够调用静态变量。

实现方法能够调用静态变量、实例变量

41、是否能够从一个static方法内部发出对非static方法的调用?

不能够,若是其中包含对象的method();不能保证对象初始化.

42、写clone()方法时,一般都有一行代码,是什么?

Clone 有缺省行为,super.clone();他负责产生正确大小的空间,并逐位复制。

43、JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别表明什么意义?在try块中能够抛出异常吗?

Try:执行部分,产生异常

Catch:捕捉异常

Finally:无论有没有异常都执行

Throws:在方法声明处声明要抛出的异常,调用者必须对其进行处理。

Throw:抛出一个异常

在try中能够抛出异常,通常与声明的异常相同。

自定义异常要继承于Exception或Exception的子类

45、冒泡排序法

//相邻两个数比较,将最小或最大的放到后面,最后面数的不参与比较

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

public  class  BubbleSort {

     private  static  int  al[] =  new  int [ 10 ];

     publicBubbleSort() {

        al[ 0 ]= 2 ;

        al[ 1 ]= 3 ;

        al[ 2 ]= 23 ;

        al[ 3 ]= 45 ;

        al[ 4 ]= 1 ;

        al[ 5 ]= 67 ;

        al[ 6 ]= 23 ;

        al[ 7 ]= 80 ;

        al[ 8 ]= 35 ;

        al[ 9 ]= 72 ;

     }

     public  static  void  main(String[] args) {

        BubbleSort bs =  new  BubbleSort();

        System.out.println( "排序前:" );

        display(al);

         

        for ( int  i= 0 ;i<al.length;i++) {

         

            for  ( int  j =  0 ; j < al.length-i- 1 ; j++) {

                

               if (al[j]>al[j+ 1 ]) {

                   swap(j,j+ 1 );

               }

            }

        }

        System.out.println();

        System.out.println( "排序后:" );

        display(al);

     }

     private  static  void  display( int [] al2) {

        for  ( int  i =  0 ; i < al2.length; i++) {

            System.out.print(al2[i]+ "  " );

        }

     }

     private  static  void  swap( int  i, int  j) {

        int  temp = al[i];

        al[i]= al[j];

        al[j] = temp;

     }

}

46、String and StringBuffer的区别?

String:长度给定不可变,当多个字符串联合时要先转为StringBuffer,再联合,速度慢。

StringBuffer:长度可变,能够将多个字符串值直接联合,效率高

47、用java代码编写堆栈

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

格瑞丝网络

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值