应届毕业生面试分享
1、详细阐述一下Collection接口所包含的内容
ArrayList----按顺序存放数据(Vector的替代者)
List LinkList-----按顺序存放数据的链表 允许重复存放
Vector--------按顺序存放数据(线程安全的)
Collection
HashSet ---根据HashCode()和equals()方法来判断是否有重复
Set 实现Set接口的集合不允许重复存放数据
SortedSet(Interface)----TreeSet 通过实现Comparable接口和
Comparator接口而具有排序功能的集合
2、子类与父类之间的变量初始化的顺序。请看下列程序
package ch;
class Super{
int i=10;
Super(){
print();
i=20;
}
void print(){
System.out.println(i);
}
}
public class Sub extends Super{
int j=30;
Sub(){
print();
j=40;
}
void print(){
System.out.println(j);
}
public static void main(String[] args){
System.out.println(new Sub().j);
}
}
运行结果为0、30、40。
变量初始化的过程为(1)、申请空间 (2)、默认初始化 (3)、定义初始化 (4)、构造器初始化。如果有子类与父类,肯定是先加载父类。
3、动态绑定
从概念上的理解是不同的类对与同一消息做出的不同的响应。
从代码实现的角度是:
abstract public class Shape{
abstract public void draw();
abstract public void draw(String title);
}
public class Rectangle extends Shape{
public void draw(){
System.out.println("a rectangle");
}
public void draw(String title){
System.out.println("a rectangle named " + title);
}
}
public class Circle extends Shape{
public void draw(){
System.out.println("a circle");
}
public void draw(String title){
System.out.println("a circle named " + title);
}
}
public class ShapeTest{
public static void draw(Shape shape){
shape.draw();
}
public static void draw(Shape shape, String title){
shape.draw(title);
}
public static void main(String[] args){
Shape shape = new Circle();
draw(shape);
draw(shape, "circle");
System.out.println("********************");
shape = new Rectangle();
draw(shape);
draw(shape, "rectangle");
}
}
以上代码实现了动态绑定。动态绑定的重点在于(1)、Static的方法取决于编译类型(2)、非Static的方法取决于运行类型(3)、变量取决于编译类型。
4、sleep()和wait()有什么区别?
sleep是线程类(Thread)的方法,导致此线程暂停执行指定时间,给执行机会给其他线程,但是监控状态依然保持,到时后会自动恢复。调用sleep不会释放对象锁。
wait是Object类的方法,对此对象调用wait方法导致本线程放弃对象锁,进入等待此对象的等待锁定池,只有针对此对象发出notify方法(或notifyAll)后本线程才进入对象锁定池准备获得对象锁进入运行状态。
5、Override和Overload的区别?
方法的重写Overriding和重载Overloading是Java多态性的不同表现。重写Overriding是父类与子类之间多态性的一种表现,重载Overloading是一个类中多态性的一种表现。如果在子类中定义某方法与其父类有相同的名称和参数,我们说该方法被重写 (Overriding)。子类的对象使用这个方法时,将调用子类中的定义,对它而言,父类中的定义如同被"屏蔽"了。如果在一个类中定义了多个同名的方法,它们或有不同的参数个数或有不同的参数类型,则称为方法的重载(Overloading)。Overload的一些限制条件:(1)、方法名和参数和返回类型必须相同(2)、异常不能扩大(3)、可见范围不能缩小(4)、不能有final的修饰符(5)、Static的方法将被隐藏。Override的一些限制条件:(1)、方法名必须相同(2)、参数必须不同(3)、返回类型可以不同
6、String和StringBuffer的区别?
JAVA平台提供了两个类:String和StringBuffer,它们可以储存和操作字符串,即包含多个字符的字符数据。这个String类提供了数值不可改变的字符串。而这个StringBuffer类提供的字符串进行修改。当你知道字符数据要改变的时候你就可以使用StringBuffer。典型地,你可以使用StringBuffers来动态构造字符数据。
7、OOAD中的Association、Aggregation、Composition的区别?
三者从概念上来讲:Association是一般的关联,有”user a”的含义。Aggregation和Composition都有整体和部分的关系,其中Aggregation中的部分脱离了整体,部分仍然有意义,有”has a”的含义,是共享式的。而Composition中的部分脱离了整体,部分将没有任何意义,是独占式的。
从代码实现的角度上讲:三者都是以属性出现,其中Association中作为属性出现时,不需要对其进行强制赋值,只要在使用是对其进行初始化即可。Aggregation中作为属性出现时,需要在构造器中通过传递参数来对其进行初始化。Composition中 作为属性出现时,需要在整体的构造器中创建部分的具体实例,完成对其的实例化。
从数据库的层面上来讲:Association不需要被级联删除,Aggregation不需要被级联删除,Composition是需要被级联删除的。
下面通过一个例子来更深刻的理解这三者的区别。
//Association relationship
public class Student{
private String name;
private int age;
BasketBall aBall;
public Student( String name, int age){
this.name=name;
this.age=age;
}
public void getBall(BasketBall aBall){
this.aBall=aBall;
}
public void play(){
System.out.println("I am playing basketball"+aBall);
}
}
class BasketBall{
private Color aColor;
private int size;
public BasketBall(Color aColor, int size){
this.aColor=aColor;
this.size=size;
}
}
class StudentAdmin{
public static void main(String aa[]){
Student aStudent=new Student("Peter", 22);
BasketBall aBasketBall=new BasketBall(Color.red, 32);
aStudent.getBall(aBasketBall);
aStudent.play();
}
}
//Aggregation relationship
public class Computer{
private String cpu;
private float weight;
private Monitor aMonitor;
public Computer(String cpu, float weight, Monitor aMonitor){
this.cpu=cpu;
this.weight=weight;
this.aMonitor=aMonitor;
}
public void turnOn(){ System.out.println("I am on now"); }
}
class Monitor{
private int inch;
private boolean isFlat;
//no information of computer
public Monitor(int inch, boolean isFlat){
this.inch=inch;
this.isFlat=isFlat;
}
}
class ComputerAdmin{
public static void main(String aa[]){
Monitor aMonitor=new Monitor(17, true);
System.out.println("I do something others here");
Computer aComputer=new Computer(486, 32.0, aMonitor);
System.out.println("Computer is :"+aComputer);
aComputer.turnOn();
}
}
//Composition
public class Computer{
private String cpu;
private float weight;
private Monitor aMonitor;
public Computer(String cpu, float weight, int inch, boolean isFlat){
this.cpu=cpu;
this.weight=weight;
this.aMonitor=new Monitor(inch, isFlat);
}
public void turnOn(){ System.out.println("I am on now"); }
}
class Monitor{
private int inch;
private boolean isFlat;
//no information of computer
public Monitor(int inch, boolean isFlat){
this.inch=inch;
this.isFlat=isFlat;
}
}
class ComputerAdmin{
public static void main(String aa[]){
//Monitor aMonitor=new Monitor(17, true);
Computer aComputer=new Computer(486, 32.0, 17, true);
System.out.println("Computer is :"+aComputer);
aComputer.turnOn();
}
}
8、数据连接池的工作机制是什么?
J2EE服务器启动时会建立一定数量的池连接,并一直维持不少于此数目的池连接。
客户端程序需要连接时,池驱动程序会返回一个未使用的池连接并将其表记为忙。如果当前没有空闲连接,池驱动程序就新建一定数量的连接,新建连接的数量有配置参数决定。当使用的池连接调用完成后,池驱动程序将此连接表记为空闲,其他调用就可以使用这个连接。
9、说出Servlet的生命周期,并说出Servlet和CGI的区别。
Servlet被服务器实例化后,容器运行其init方法,请求到达时运行其service方法,service方法自动派遣运行与请求对应的doXXX方法(doGet,doPost)等,当服务器决定将实例销毁的时候调用其destroy方法。
与cgi的区别在于servlet处于服务器进程中,它通过多线程方式运行其service方法,一个实例可以服务于多个请求,并且其实例一般不会销毁,而CGI对每个请求都产生新的进程,服务完成后就销毁,所以效率上低于servlet。
最后祝愿达内在读学员能够找到一份理想的工作!同时也向那些正在犹豫来达内学习的学弟、学妹们承诺:选择达内是正确的(这是发自我内心的话)。You are the best!