Preparation before the java written test interview - java basic complex and error-prone questions (3)

35.How many types of streams are there in java? JDK provides some abstract classes for each type of stream for inheritance, please name them which classes are?

Byte stream, character stream.

Byte stream inherits from InputStream:

和OutputStream:

The character stream inherits from InputStreamReader:

和OutputStreamWriter:

There are many other streams in the java.io package, mostly for performance and ease of use.

 

36. What is java serialization and how to realize java serialization?

We sometimes transfer a java object into a byte stream or restore it into a java object from a byte stream . For example, to store the java object on the hard disk or transmit it to other computers on the network, in this process we You can write your own code to convert a java object into a byte stream of a certain format and then transmit it. However, jre itself provides this support. We can call the writeObject method of OutputStream to do it. If we want java to do it for us, The object to be transmitted must implement the serializable interface, so that special processing will be performed when javac is compiled, and the compiled class can be manipulated by the writeObject method, which is called serialization. The class that needs to be serialized must implement the Serializable interface, which is an identification interface, and there are no methods that need to be implemented. Implements Serializable is only to mark that the object can be serialized .

 For example, in web development, if the object is stored in the Session, and tomcat needs to serialize the Session object to the hard disk when it restarts, this object must implement the Serializable interface. If the object is to be transmitted over the network through a distributed system or through remote calls such as rmi, it is necessary to transmit the object on the network, and the transmitted object must implement the Serializable interface.

 

37. Describe the principle mechanism of JVM loading class files?

Class loading in JVM is implemented by ClassLoader and its subclasses . Java ClassLoader is an important Java runtime system component. It is responsible for finding and loading the classes of the class file at runtime .

38. What is the difference between heap (heap) and stack (stack).

Java memory is divided into two categories, one is stack memory and the other is heap memory. Stack memory means that when the program enters a method, a separate private storage space is allocated for the method to store the local variables inside the method . When the method ends, the stack allocated to the method will be released. The variables will also be freed accordingly.

The heap is a different memory from the stack. It is generally used to store data that is not placed in the current method stack. For example, objects created using new are placed in the heap, so it will not disappear with the end of the method. After the local variables in the method are modified with final, they are placed on the heap instead of the stack .

The stack stores the values ​​of basic types and the reference addresses of some objects;

The heap stores objects. Such as:

Person p = new Person();

The new Person object is placed in the heap memory, and the reference variable p of Person is placed in the stack memory

39. What is GC? Why is GC?   

GC means garbage collection (Gabage Collection). Memory processing is a place where programmers are prone to problems. Forgetting or wrong memory recycling can lead to program or system instability or even crash. The GC function provided by Java can automatically monitor whether objects exceed The scope can achieve the purpose of automatically reclaiming memory. The Java language does not provide an explicit operation method for releasing allocated memory.

40. The advantages and principles of garbage collection. And consider 2 recycling mechanisms.

A notable feature of Java language is the introduction of garbage collection mechanism, which solves the most troublesome memory management problem for C++ programmers. It makes Java programmers no longer need to consider memory management when writing programs. Due to a garbage collection mechanism, objects in Java no longer have the concept of "scope", only object references have "scope". Garbage collection can effectively prevent memory leaks and effectively use available memory. The garbage collector usually runs as a separate low-level thread. In unpredictable circumstances, it clears and recycles objects that have died in the memory heap or that have not been used for a long time. Programmers cannot call the garbage collector in real time. objects or all objects are garbage collected. The collection mechanism includes generational replication garbage collection, marked garbage collection, and incremental garbage collection .

41. What is the basic principle of the garbage collector? Can the garbage collector reclaim memory right away? Is there any way to proactively notify the virtual machine for garbage collection?

For the GC, when the programmer creates an object, the GC begins to monitor the address, size, and usage of the object. Generally, GC records and manages all objects in the heap in a directed graph manner. In this way it is determined which objects are "reachable" and which objects are "unreachable". When the GC determines that some objects are "unreachable", it is the responsibility of the GC to reclaim the memory space. Can. Programmers can manually execute System.gc() to notify the GC to run, but the Java language specification does not guarantee that the GC will be executed.

 43, when to use assert.

Assertion is a common debugging method in software development, and many development languages ​​support this mechanism. In implementation, assertion is a statement in the program that checks a boolean expression. A correct program must ensure that the value of the boolean expression is true ; if the value is false , the program is already in an incorrect state , assert will give a warning or exit. Generally speaking, assertions are used to guarantee the most basic and critical correctness of programs . Assertion checks are usually turned on during development and testing . To improve performance, assertion checking is usually turned off after the software is released.

44. Will there be memory leaks in java? Please describe briefly .

The so-called memory leak means that an object or variable that is no longer used by the program has been occupied in memory . There is a garbage collection mechanism in java, which can ensure that when an object is no longer referenced, that is, when the object is programmed with orphans, the object will be automatically cleared from memory by the garbage collector. Since Java uses a directed graph for garbage collection management, it can eliminate the problem of reference cycles. For example, there are two objects that refer to each other. As long as they are unreachable with the root process, the GC can also recycle them.

Memory leaks in java: long-lived objects hold references to short-lived objects, and memory leaks are likely to occur. Although short-lived objects are no longer needed, because long-lived objects hold their references As a result, it cannot be recycled. This is the scene of memory leaks in java. In layman's terms, the programmer may create an object, and the object will not be used in the future, but the object has been referenced, that is, the object is useless but cannot be used. Reclaimed by the garbage collector, this is where memory leaks may occur in java . For example, in the cache system, we load an object and put it in the cache (for example, in a global map object), and then never use it again, This object is always referenced by the cache, but is no longer used.

To check for memory leaks in java, be sure to let the program execute all branch situations until the end of the program, and then see if an object has been used. If not, then it can be determined that the object is a memory leak.

45. The problem of obtaining the class name method getClassName() .

publicclass Test extends Date{ 

    publicstaticvoid main(String[] args) {

       new Test().test();

    }

   

    publicvoid test()

    {

       System.out.println(

              super.getClass().getName()

       );

    }

}

The answer is that Test is not the class name of the parent class, because the getClass() method inherits from Object, and the method is defined as: Get the current running class (ie Test), if you really need to get the name of the parent class, you can use this method:

getClass().getSuperClass().getName()

46. ​​Name some commonly used classes, packages and interfaces, please give 5 each

To make people feel that you are very familiar with java ee development, you can not only list those things in core java, but also list those things that you are involved in the ssh project. Just write the classes involved in the programs you've written recently.

Commonly used classes: BufferedReader BufferedWriter FileReader FileWirter String Integer

java.util.Date,System,Class,List,HashMap

Commonly used packages: java.lang   java.io java.util   java.sql , javax.servlet , org.apache.strtuts.action , org.hibernate

常用的接口:Remote List Map  Document  NodeList ,Servlet,HttpServletRequest,HttpServletResponse,Transaction(Hibernate)、Session(Hibernate),HttpSession

47. 3 ways to get a class

1.class.forName("full path name of        class") 2.Classname.class 3.Instance.getClass();


48. 4 ways to create objects

1. Instantiate an object with the new statement

2. Created an object using the reflection mechanism:

   Use the newInstance method of the Class class, or the newInstance method of the Constructor class

3. Create an object through the clone() method

4. Create an object by deserialization:

49. JVM loading class file process

 (1) Loading: Find and import class files

 (2) Link: Perform verification, preparation and parsing steps, where the parsing step is optional:

          a) Verification: Verify the correctness of the loaded class file;

          b) Preparation: allocate storage space to the static variables of the class;

          c) Analysis: convert symbolic references to direct references;

  (3) Initialization: Initialize the static code block of the static variable of the class;


50. Four scopes in JSP?

page, request, session and application, as follows:

  • ①page represents objects and attributes related to a page.
  • ②request represents objects and attributes related to a request issued by the Web client. A request may span multiple pages and involve multiple web components; temporary data that needs to be displayed on the page can be placed in this scope
  • ③session represents the objects and attributes related to a session established by a user and the server. Data related to a user should be placed in the user's own session
  • ④ application represents the objects and attributes related to the entire Web application, which is essentially a global scope spanning the entire Web application, including multiple pages, requests and sessions.

51. What are the technologies for implementing session tracking?

Since the HTTP protocol itself is stateless, in order to distinguish different users, the server needs to track the user session. Simply put, it is to register the user and assign a unique ID to the user. The next time the user requests to include this ID, Based on this, the server determines which user it is.

  • ①URL rewriting: Add user session information to the URL as a parameter of the request, or add a unique session ID to the end of the URL to identify a session.
  • ②Set form hidden fields: Add fields related to session tracking to implicit form fields. These information will not be displayed in the browser but will be submitted to the server when the form is submitted. 
    Both of these approaches are difficult to handle with information passing across multiple pages, because things can become cumbersome if you have to modify the URL each time or add implicit form fields to the page to store information about the user's session.
  • ③Cookie: There are two kinds of cookies, one is window-based, after the browser window is closed, the cookie is gone; the other is to store the information in a temporary file and set the time of existence. When the user establishes a session with the server through the browser, the session ID will be returned with the response information and stored in the window-based cookie, which means that as long as the browser is not closed, the session has not timed out, and the session ID will be restored on the next request. It will be submitted to the server for the server to identify the user. Information can be saved for the user in the session. Session objects are in server memory, while window-based cookies are in client memory. If the browser disables cookies, then session tracking is required in the following two ways. Of course, there are a few things to pay attention to when using cookies: first, don't store sensitive information in cookies; second, the amount of data stored in cookies is limited (4k), and too much content cannot be stored in cookies; third, browsers usually only allow one site Store up to 20 cookies. Of course, other information related to the user session (except the session ID) can also exist in cookies to facilitate session tracking.
  • ④HttpSession:在所有会话跟踪技术中,HttpSession对象是最强大也是功能最多的。当一个用户第一次访问某个网站时会自动创建HttpSession,每个用户可以访问他自己的HttpSession。可以通过HttpServletRequest对象的getSession方法获得HttpSession,通过HttpSession的setAttribute方法可以将一个值放在HttpSession中,通过调用HttpSession对象的getAttribute方法,同时传入属性名就可以获取保存在HttpSession中的对象。与上面三种方式不同的是,HttpSession放在服务器的内存中,因此不要将过大的对象放在里面,即使目前的Servlet容器可以在内存将满时将HttpSession中的对象移到其他存储设备中,但是这样势必影响性能。添加到HttpSession中的值可以是任意Java对象,这个对象最好实现了Serializable接口,这样Servlet容器在必要的时候可以将其序列化到文件中,否则在序列化时就会出现异常。
52请对以下Java EE中的名词进行解释
  • 1.容器:容器为Java EE应用程序组件提供了运行时支持。容器提供了一份从底层Java EE API到应用程序组件的联合视图。Java EE应用程序组件不能直接地与其它Java EE应用程序组件交互。它们通过容器的协议和方法来达成它们之间以及它们与平台服务之间的交互。在应用程序组件和Java EE服务之间插入一个容器,这允许该容器透明地为组件注入必须的服务,例如声明式事务管理,安全检查,资源池和状态管理。
  • 2.资源适配器:资源适配器是一个系统级的组件,它通常实现了对外部资源管理器的网络连接。资源适配器能够扩展Java EE平台的功能。这只需要实现一个Java EE标准服务API(例如JDBC驱动程序),或者定义并实现一个能连接到外部应用程序系统的资源适配器就可以达到。资源适配器也可以提供完整的本地或本地资源的服务。资源适配器接口通过Java EE服务供应商接口(Java EE SPI)来连接Java EE平台。使用Java EE SPI连接到Java EE平台的资源适配器可以和所有的Java EE产品协同工作。
  • 3.JNDI(Java Naming & Directory Interface):Java命名目录接口,主要提供的功能是:提供一个目录系统,让其它各地的应用程序在其上面留下自己的索引,从而满足快速查找和定位分布式应用程序的功能。
  • 4.JMS(Java Message Service):Java消息服务是用于消息发送的标准API,它支持可靠的“点对点”消息发送和“发布-订阅”模型。Java EE规范要求JMS供应商同时实现“点对点”消息发送和”发布/订阅”型消息发送。
  • 5.JTA(Java Transaction API):Java 事务编程接口。Java事务API由两部分组成:①一个应用程序级的边界划分接口,容器和应用程序组件用它来划分事务边界;②一个介于事务管理器和资源管理器之间的Java EE SPI级接口。
  • 6.JPA(Java Persistence API):Java持久化API是用于持久化和对象/关系映射管理的标准API。通过使用一个Java域模型来管理关系型数据库,Java EE规范为应用程序开发者提供了一种对象/关系映射功能。Java EE必须对Java持久化API提供支持。它也可以用在Java SE环境中。
  • 7.JAF(JavaBean Activation FrameWork):JAF API提供了一个框架来处理不同MIME类型的数据,它们源于不同的格式和位置。JavaMail API使用了JAF API。JAF API包含在Java SE中,因此它可以被Java EE应用程序使用。
  • 8.JAAS(Java Authentication and Authorization Service):使服务能够基于用户进行验证和实施访问控制。它实现了一个Java版的标准的的Plugable Authentication Module (PAM)框架,并支持基于用户的授权。Java Authorization Service Provider Contract for Containers (JACC) 定义了Java EE应用程序服务器和授权服务提供方之间的协议,允许将自定义的授权服务提供方插入任何Java EE产品中。
  • 9.JMX(Java Management Extension):Java平台企业版管理规范中定义了一种API,通过一种特殊的管理型EJB来管理Java EE服务器。JMX API也提供了一些管理上的支持。

53.简单介绍El 表达式

El表达式是jsp中的一种技术,原要用java语言获取的值可以是用El表达式代替,使代码更加简洁。

El表达式使用${}取值,可以从四个域中取值:page,request,session,application.如果知道确定的域,可以使用${resquestScope.name}取值,可以获取对象属性,获取list,map集合中的值。支持基本的运算符和三元运算符。

54.介绍ognl表达式

OGNLStruts 2默认的表达式语言。是Object Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目。

 1.#符号的用途一般有三种。    1)访问非根对象属性,由于Struts 2中值栈被视为根对象,所以访问其他非根对象时,需要加#前缀。实际上#相当于ActionContext.getContext();;#session.msg表达式相当于ActionContext.getContext().getSession(). getAttribute(”msg”) 。     2)用于过滤和投影(projecting)集合,如示例中的persons.{?#this.age>20}。

    3)用来构造Map,例如示例中的#{’foo1′:’bar1′, ’foo2′:’bar2′}。

2.struts2配置文件中也可以使用ognl表达式获取值栈总的值。

3.和El表达式的区别:

El表达式:

单纯在jsp页面中出现,是在四个作用域中取值,page,request,session,application.
>>如果在struts环境中,它除了有在上面的四个作用域的取值功能外,还能从值栈(valuestack)中取值.
>>特点1:${name},name在值栈中的查找顺序是:先从对象栈中取,取到终止,否则,向map中取。
>>特点2:在对象栈的查找顺序是,先从model中找是否有name这个属性,找到终止,否则,找action中是否有name这个全局变量。
>>特点3:${#name},里面的是不带#号的。
>>特点4:如果放在对象栈中的是一个自定义的对象,那么${property}里面可以直接去该对象的属性值,不用这样${object.property}

OGNL表达式:
1:读取从后台传递的值
%{#name}:表示从值栈的map中取值
%{name}:表示从值栈的对象栈中取值
%{#request.name}:表示从request域中取值


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325169096&siteId=291194637