Java concept
包装类将基本数据类型封装成数据对象(Integer).
如果Vector定义为保存Object则可以存放任意类型。
接口中常量声明必须初始化.
下列哪种情况可以终止当前线程的运行?抛出一个异常时.sleep()仅仅挂起
ServerSocket(int port) 是服务端绑定port端口,调accept()监听等待客户端连接,它返回一个连接队列中的一个socket。
Socket(InetAddress address , int port)是创建客户端连接主机的socket流,其中InetAddress是用来记录主机的类,port指定端口。
Hashtable不允许null键或值
不论程序是否发生错误及捕捉到异常情况,都会执行finally部分,当try区段的程序发生异常时,才会执行catch区段的程序。
在 hibernate 开发中,关于 POJO 类对象的状态说法正确的是()。
自由状态(Transient):实体在内存中自由存在,与数据库中的记录无关
持久状态(Persistent):实体处于由Hibernate框架所管理的状态,对应了数据库中的一条记录,同时与某个session实例发生了关联
游离状态(Detached):在session 关闭之后,可以使对象从持久状态转换到游离状态。
抽象类可以有构造方法,接口中不能有构造方法
抽象类中可以有普通成员变量,接口中没有普通成员变量,接口中可以有变量,但是无论你怎么写,最后都是public static final的。
一个类可以实现多个接口,但只能继承一个抽象类。
JDK中提供的java、javac、jar等开发工具也是用Java编写的
要使对象具有序列化能力实现java.io.Serializable,在接口内部没有定义任何属性与方法。只是用于标志此接口的实现类可以被序列化与反序列化。
Java使用Unicode编码
如果在一个线程构造了一个不可变对象之后(对象仅包含final字段),就可以保证了这个对象被其他线程正确的查看。错误,指向有可能变
JMM通过控制主内存与每个线程的本地内存之间的交互,来为java程序员提供内存可见性保证
“synchronized” — 保证在块开始时都同步主内存的值到工作内存,而块结束时将变量同步回主内存修饰整块
“volatile” — 保证修饰后在对变量读写前都会与主内存更新。
readwritelock适用于读多写少的并发场景
CopyOnWriteArrayList适合使用在读操作远远大于写操作的场景里,比如缓存。
ReadWriteLock 当写操作时,其他线程无法读取或写入数据,而当读操作时,其它线程无法写入数据,但却可以读取数据 。适用于 读取远远大于写入的操作。
ConcurrentHashMap是一个线程安全的Hash Table,它的主要功能是提供了一组和HashTable功能相同但是线程安全的方法。ConcurrentHashMap可以做到读取数据不加锁,并且其内部的结构可以让其在进行写操作的时候能够将锁的粒度保持地尽量地小,不用对整个ConcurrentHashMap加锁。
volatile只能保证变量的安全,不能保证线程的安全
jre判断程序是否执行结束的标准是所有的前台线程执行完毕
java用(监视器)机制实现了进程之间的同步执行;jvm中没有进程的概念 ,但是jvm中的线程映射为操作系统中的进程,对应关系为1:1。在jvm中是使用监视器锁来实现不同线程的异步执行,在语法的表现就是synchronized。
ServletContext的参数值context.getInitParameter()
Stub(适配器模式,动态链接原理)
存根类是一个类,它实现了一个接口,它的作用是:如果一个接口有很多方法,如果要实现这个接口,就要实现所有的方法。但是一个类从业务来说,可能只需要其中一两个方法。如果直接去实现这个接口,除了实现所需的方法,还要实现其他所有的无关方法。而如果通过继承存根类实现接口,就免去了这种麻烦。
RMI 采用stubs 和 skeletons 来进行远程对象(remote object)的通讯。stub 充当远程对象的客户端代理,有着和远程对象相同的远程接口,远程对象的调用实际是通过调用该对象的客户端代理对象stub来完成的。
每个远程对象都包含一个代理对象stub,当运行在本地Java虚拟机上的程序调用运行在远程Java虚拟机上的对象方法时,它首先在本地创建该对象的代理对象stub, 然后调用代理对象上匹配的方法。每一个远程对象同时也包含一个skeleton对象,skeleton运行在远程对象所在的虚拟机上,接受来自stub对象的调用。这种方式符合等到程序要运行时将目标文件动态进行链接的思想
super关键字是在子类对象内部指代其父类对象的引用,可以找到父类的属性。
JDBC使用了桥接模式.JDBC提供两套接口,一个面向数据库厂商,一个面向JDBC使用者。
桥接模式:
定义 :将抽象部分与它的实现部分分离,使它们都可以独立地变化。
意图 :将抽象与实现解耦。
桥接模式所涉及的角色
- Abstraction:定义抽象接口,拥有一个Implementor类型的对象引用
- RefinedAbstraction:扩展Abstraction中的接口定义
- Implementor:是具体实现的接口,Implementor和RefinedAbstraction接口并不一定完全一致,实际上这两个接口可以完全不一样Implementor提供具体操作方法,而Abstraction提供更高层次的调用
- ConcreteImplementor:实现Implementor接口,给出具体实现
Jdk中的桥接模式:JDBC
JDBC连接数据库的时候,在各个数据库之间进行切换,基本不需要动太多的代码,甚至丝毫不动,原因就是JDBC提供了统一接口,每个数据库提供各自的实现,用一个叫做数据库驱动的程序来桥接就行了
JDK1.6提供的6中作用于不同年代的垃圾收集器.
CMS,SerilizeOld,Parallel Old,ParNew,Serial,ParallelScanvenge
Obejct的方法:notify();notifyAll();wait()
5.处理异常常用的两种方式:
- try…catch(捕获处理机制);
- throws(冒泡处理机制).
注意细节:使用try…catch块捕获时可以没有catch块,但当没用catch块的时候必须得有finally块
6.DBMS对数据库的保护通过4个方面来实现:
数据库的恢复
数据库的并发控制
数据库的完整性控制
数据库安全性控制
DBMS中实现事务持久性的子系统是恢复管理子系统。
7.final定义的变量可以在赋值的时候完成初始化
8.socket.getLocalAddress()获取本机IP
9.反射机制:在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性,破坏了封装机制.
10.HttpServlet容器响应Web客户请求流程如下:
1)Web客户向Servlet容器发出Http请求;
2)Servlet容器解析Web客户的Http请求;
3)Servlet容器创建一个HttpRequest对象,在这个对象中封装Http请求信息;
4)Servlet容器创建一个HttpResponse对象;
5)Servlet容器调用HttpServlet的service方法,这个方法中会根据request的Method来判断具体是执行doGet还是doPost,把HttpRequest和HttpResponse对象作为service方法的参数传给HttpServlet对象;
6)HttpServlet调用HttpRequest的有关方法,获取HTTP请求信息;
7)HttpServlet调用HttpResponse的有关方法,生成响应数据;
8)Servlet容器把HttpServlet的响应结果传给Web客户。
10.结束进程:Thread.interrupt()/启动进程:Thread.start()已经启动.一个线程等待另外一个线程的方法是wait()方法
11.所有对象都在堆栈上分配/方法区加载类信息,线程共享;虚拟机展线程隔离.
12.虚拟机中没有泛型,只有普通类和普通方法。所有泛型类的类型参数在编译时都会被擦除,创建泛型对象时请指明类型,让编译器尽早的做参数检查。
13.解决哈希冲突常用的两种方法是:开放定址法和链地址法
开放定址法:当冲突发生时,使用某种探查(亦称探测)技术在散列表中形成一个探查(测)序列。沿此序列逐个单元地查找,直到找到给定的关键字,或者碰到一个开放的地址(即该地址单元为空)为止(若要插入,在探查到开放的地址,则可将待插入的新结点存人该地址单元)。查找时探查到开放地址则表明表中无待查的关键字,即查找失败。
链地址法:将所有关键字为同义词的结点链接在同一个单链表中。若选定的散列表长度为m,则可将散列表定义为一个由m个头指针组成的指针数组T[0..m-1]。凡是散列地址为i的结点,均插入到以T[i]为头指针的单链表中。T中各分量的初值均应为空指针。
14.Servlet生命周期:JSP会先解释成Servlet源文件,然后编译成Servlet类文件,每当用户端运行JSP时,jsp service()方法都会运行一次.
15.执行forward时,浏览器不知道服务器发送的内容是从何处来,浏览器地址栏中还是原来的地址/执行redirect时,服务器端告诉浏览器重新去请求地址/forward是内部重定向,redirect是外部重定向
16.子类调用父类的方法:子类也可以new一个父类对象,然后调用父类方法,并不一定要使用super方法。
17.1)抽象类可以包括抽象方法和非抽象方法
2)如果类里面有抽象方法的话,那么类一定要声明为抽象的!
18.Class clazz = Class.forName(“work.litao.Child”);
//得到父类
Class superClass = clazz.getSuperclass();
//得到父类非继承的所有方法
Method[] methods = superClass.getDeclaredMethods();
19.J2EE中常用的名词解释
1.web容器:给处于其中的应用程序组件(JSP,SERVLET)提供一个环境,使JSP,SERVLET直接和容器中的环境变量接接口互,不必关注其它系统问题。主要有WEB服务器来实现。例如:TOMCAT,WEBLOGIC,WEBSPHERE等。该容器提供的接口严格遵守J2EE规范中的WEB APPLICATION 标准。我们把遵守以上标准的WEB服务器就叫做J2EE中的WEB容器。
2.Web container:实现J2EE体系结构中Web组件协议的容器。这个协议规定了一个Web组件运行时的环境,包括安全,一致性,生命周期管理,事务,配置和其它的服务。一个提供和JSP和J2EE平台APIs界面相同服务的容器。一个Web container 由Web服务器或者J2EE服务器提供。
3.EJB容器:Enterprise java bean 容器。更具有行业领域特色。他提供给运行在其中的组件EJB各种管理功能。只要满足J2EE规范的EJB放入该容器,马上就会被容器进行高效率的管理。并且可以通过现成的接口来获得系统级别的服务。例如邮件服务、事务管理。一个实现了J2EE体系结构中EJB组件规范的容器。 这个规范指定了一个Enterprise bean的运行时环境,包括安全,一致性,生命周期,事务, 配置,和其他的服务。
4.JNDI:(Java Naming & Directory Interface)JAVA命名目录服务。主要提供的功能是:提供一个目录系统,让其它各地的应用程序在其上面留下自己的索引,从而满足快速查找和定位分布式应用程序的功能。
5.JMS:(Java Message Service)JAVA消息服务。主要实现各个应用程序之间的通讯。包括点对点和广播。
6.JTA:(Java Transaction API)JAVA事务服务。提供各种分布式事务服务。应用程序只需调用其提供的接口即可。
7.JAF:(Java Action FrameWork)JAVA安全认证框架。提供一些安全控制方面的框架。让开发者通过各种部署和自定义实现自己的个性安全控制策略。
8.RMI/IIOP:(Remote Method Invocation /internet对象请求中介协议)他们主要用于通过远程调用服务。例如,远程有一台计算机上运行一个程序,它提供股票分析服务,我们可以在本地计算机上实现对其直接调用。当然这是要通过一定的规范才能在异构的系统之间进行通信。RMI是JAVA特有的。RMI-IIOP出现以前,只有RMI和CORBA两种选择来进行分布式程序设计。RMI-IIOP综合了RMI和CORBA的优点,克服了他们的缺点,使得程序员能更方便的编写分布式程序设计,实现分布式计算。首先,RMI-IIOP综合了RMI的简单性和CORBA的多语言性(兼容性),其次RMI-IIOP克服了RMI只能用于Java的缺点和CORBA的复杂性(可以不用掌握IDL)。
20.JDBC提供了Statement、PreparedStatement 和 CallableStatement三种方式来执行查询语句,其中Statement用于通用查询, PreparedStatement用于执行参数化查询,而CallableStatement则是用于存储过程;
1.Statement、PreparedStatement和CallableStatement都是接口(interface)。
2.Statement继承自Wrapper、PreparedStatement继承自Statement、CallableStatement继承自PreparedStatement。
3.Statement接口提供了执行语句和获取结果的基本方法;PreparedStatement接口添加了处理 IN 参数的方法;CallableStatement接口添加了处理 OUT 参数的方法。
4.a.Statement: 普通的不带参的查询SQL;支持批量更新,批量删除;
b.PreparedStatement: 可变参数的SQL,编译一次,执行多次,效率高; 安全性好,有效防止Sql注入等问题; 支持批量更新,批量删除;
c.CallableStatement:继承自PreparedStatement,支持带参数的SQL操作; 支持调用存储过程,提供了对输出和输入/输出参数(INOUT)的支持;
Statement每次执行sql语句,数据库都要执行sql语句的编译, 最好用于仅执行一次查询并返回结果的情形,效率高于PreparedStatement。
PreparedStatement是预编译的,使用PreparedStatement有几个好处
1.在执行可变参数的一条SQL时,PreparedStatement比Statement的效率高,因为DBMS预编译一条SQL当然会比多次编译一条SQL的效率要高。
2.安全性好,有效防止Sql注入等问题。
3.对于多次重复执行的语句,使用PreparedStament效率会更高一点,并且在这种情况下也比较适合使用batch;
4.代码的可读性和可维护性。
21.ResultSet去索引列名主要用的是索引,从1开始也可以使用列名。
22.优化Hibernate所鼓励的7大措施:
1.尽量使用many-to-one,避免使用单项one-to-many
2.灵活使用单向one-to-many
3.不用一对一,使用多对一代替一对一
4.配置对象缓存,不使用集合缓存
5.一对多使用Bag 多对一使用Set
6.继承使用显示多态 HQL:from object polymorphism=”exlicit” 避免查处所有对象
7.消除大表,使用二级缓存
23.throw用于抛出异常对象,throws声明方法要抛出的异常。
24.Java非静态方法属于对象的成员??????
25.栈中主要存放一些基本类型的变量(int, short, long, byte, float, double, boolean, char)和对象句柄。
堆式存储:堆内存用来存放由 new 创建的对象和数组,在堆中分配的内存,由 Java 虚拟机的自动垃圾回收器来管理。
26.泛型仅仅是java的语法糖,它不会影响java虚拟机生成的汇编代码,在编译阶段,虚拟机就会把泛型的类型擦除,还原成没有泛型的代码,顶多编译速度稍微慢一些,执行速度是完全没有什么区别的.
27.Java默认提供的三个ClassLoader是BootStrap ClassLoader,Extension ClassLoader,App ClassLoader,动态加载Class文件进入内存.Bootstrap ClassLoader:负责加载java基础类,主要是 %JRE_HOME/lib/ 目录下的rt.jar、resources.jar、charsets.jar和class等.
Extension ClassLoader:负责加载java扩展类,主要%JRE_HOME/lib/ext 目录下的jar和class.
App ClassLoader:负责加载当前java应用的classpath中的所有类。
classloader 加载类用的是全盘负责委托机制。 所谓全盘负责,即是当一个classloader加载一个Class的时候,这个Class所依赖的和引用的所有 Class也由这个classloader负责载入,除非是显式的使用另外一个classloader载入.
所以,当我们自定义的classlo ader加载成功了 com.company.MyClass以后,MyClass里所有依赖的class都由这个classLoader来加载完成.
- 双亲委派模型
我们应用程序都是由以上三种类加载器互相配合进行加载的,还可以加入自己定义的类加载器。称为 类加载器的双亲委派模型 ,这里类加载器之间的父子关系一般不会以继承的关系来实现,而是都使用 组合关系 来复用父加载器的。 - 双亲委托模型的工作原理
是当一个类加载器收到了类加载的请求,它首先不会自己去尝试加载这个类,而是把这个请求委派给父类加载器去完成,每一个层次的类加载都是如此,因此所有的加载请求最终都应该传送到顶层的启动类加载器中,只有当父加载器反馈自己无法加载这个加载请求的时候,子加载器才会尝试自己去加载。 - 使用双亲委派模型好处?(原因)
第一:可以避免重复加载,当父亲已经加载了该类的时候,子类不需要再次加载。
第二:考虑到安全因素,如果不使用这种委托模式,那我们就可以随时使用自定义的String来动态替代java核心api中定义类型,这样会存在非常大的安全隐患,而双亲委托的方式,就可以避免这种情况,因为String已经在启动时被加载,所以用户自定义类是无法加载一个自定义的类装载器。
28.Hibernate2延迟加载实现:a)实体对象 b)集合(Collection)
get支持延迟加载,load不支持延迟加,get()采用立即加载方式,而load()采用 延迟加载 ; get()方法执行的时候,会立即向数据库发出查询语句, 而load()方法返回的是一个代理(此代理中只有一个id属性),只有等真正使用该对象属性的时候,才会发出 sql语句 2.如果数据库中没有对应的记录,get()方法返回的是null.而load()方法出现异常ObjectNotFoundException
hibernate使用Java反射机制,而不是字节码增强程序来实现透明性
29.Webservice是跨平台,跨语言的远程调用技术;它的通信机制实质就是xml数据交换;它采用了soap协议(简单对象协议)进行通信;Web service 可以将你的服务发布到互联网上让别人去调用,也可以调用别人发布的web service,和使用自己的代码一样.
157.接口和抽象类不能是final的.接口的方法都是public,可以不声明方法;抽象类可以不包含抽象方法,但是不能实例化;空接口:标记接口,Seriazliable
158.abstract不能与final并列修饰同一个类;静态方法不能直接处理静态对象.
159.File类:FileReader()/FileWriter():继承自字节流;createNewFile/createTempFile();
160.线程安全的Map的实现:Map map = new ConcurrentHashMap();
Map map = Collections.synchronizedMap(new HashMap());
161.JVM:通过 ClassLoader 寻找和装载 class 文件;解释字节码成为指令并执行,提供 class 文件的运行环境;进行运行期间垃圾回收.OS和硬件交互.
162.对于多线程资源共享的问题,同步机制采用了“以时间换空间”的方式,而ThreadLocal采用了“以空间换时间”的方式.ThreadLocal存放的值是线程封闭,线程间互斥的,主要用于线程内共享一些数据,避免通过参数来传递.
ThreadLocal继承Object,相当于没继承任何特殊的。ThreadLocal没有实现任何接口.ThreadLocal是采用哈希表的方式来为每个线程都提供一个变量的副本.
163.如果父类中的某个方法使用了synchronized关键字,而子类中也覆盖了这个方法,默认情况下子类中的这个方法并不是同步的,必须显示的在子类的这个方法中加上 synchronized关键字才可。当然,也可以在子类中调用父类中相应的方法,这样虽然子类中的方法并不是同步的,但子类调用了父类中的同步方法,也就相当子类方法也同步了。定义在接口中的方法默认是public的.
CGI内存开销高,
类种方法由县级:静态成员变量或静态代码块>main方法>非静态成员变量或非静态代码块>构造方法
164.Jave ee:javax.servlet;javax.servlet.http
javax.servlet包中包含了7个接口,3个类和2个异常类,它们分别是:接口 :RequestDispatcher,Servlet,ServletConfig,ServletContext,ServletRequest,ServletResponse 和 SingleThreadModel. Use ServletConfig在init()方法中作为参数获取ServletContext;
java并发框架支持锁:读写锁;自旋锁(线程数增加时性能明显下降,不停在循环体内部执行);乐观锁;可重入锁(可重入锁,也叫做递归锁,指的是同一线程 外层函数获得锁之后 ,内层递归函数仍然有获取该锁的代码,但不受影响。在JAVA环境下 ReentrantLock和synchronized都是可重入锁);阻塞锁(和自旋锁对立)
165.jvm堆分为:新生代(一般是一个Eden区,两个Survivor区),老年代(old区)。方法区使用元空间(MetaSpace)实现
167.结构型模式是描述如何将类对象结合在一起,形成一个更大的结构,结构模式描述两种不同的东西:类与类的实例。故可以分为类结构模式和对象结构模式。
168.
在GoF设计模式中,结构型模式有:
1.适配器模式 Adapter
适配器模式是将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
两个成熟的类需要通信,但是接口不同,由于开闭原则,我们不能去修改这两个类的接口,所以就需要一个适配器来完成衔接过程。
2.桥接模式 Bridge
桥接模式将抽象部分与它的实现部分分离,是它们都可以独立地变化。它很好的支持了开闭原则和组合锯和复用原则。实现系统可能有多角度分类,每一种分类都有可能变化,那么就把这些多角度分离出来让他们独立变化,减少他们之间的耦合。
3.组合模式 Composite
组合模式将对象组合成树形结构以表示部分-整体的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。
4.装饰模式 Decorator
装饰模式动态地给一个对象添加一些额外的职责,就增加功能来说,它比生成子类更灵活。也可以这样说,装饰模式把复杂类中的核心职责和装饰功能区分开了,这样既简化了复杂类,有去除了相关类中重复的装饰逻辑。 装饰模式没有通过继承原有类来扩展功能,但却达到了一样的目的,而且比继承更加灵活,所以可以说装饰模式是继承关系的一种替代方案。
5.外观模式 Facade
外观模式为子系统中的一组接口提供了同意的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
外观模式中,客户对各个具体的子系统是不了解的,所以对这些子系统进行了封装,对外只提供了用户所明白的单一而简单的接口,用户直接使用这个接口就可以完成操作,而不用去理睬具体的过程,而且子系统的变化不会影响到用户,这样就做到了信息隐蔽。
6.享元模式 Flyweight
享元模式为运用共享技术有效的支持大量细粒度的对象。因为它可以通过共享大幅度地减少单个实例的数目,避免了大量非常相似类的开销。.
享元模式是一个类别的多个对象共享这个类别的一个对象,而不是各自再实例化各自的对象。这样就达到了节省内存的目的。
7.代理模式 Proxy
为其他对象提供一种代理,并由代理对象控制对原对象的引用,以间接控制对原对象的访问。
169.CyclicBarrier和CountDownLatch都可以让一组线程等待其他线程;Callable类的call()方法可以返回值和抛出异常;
170.Servlet与JSP九大内置对象的关系
JSP对象 怎样获得
out->response.getWriter
request ->Service方法中的req参数
response ->Service方法中的resp参数
session ->request.getSession
application ->getServletContext
exception ->Throwable
page ->this
pageContext ->PageContext
Config ->getServletConfig
171.LinkedBlockingQueue是一个可选有界队列,不允许null值;PriorityQueue是一个无界队列,不允许null值,入队和出队的时间复杂度是O(log(n))
172.Java于方法的调用采用的是栈帧(方法调用和方法执行),调用则入栈,完成之后则出栈。