This road interview will ask questions face JVM 70 percent of Java programmers do wrong

Preface
talk JVM, a familiar and unfamiliar terms, from the understanding of Java's first day, we would have heard the name before a year or two to work, and when the interview will often asked JDK, JRE the difference JVM three.
JVM can be said and we are old friends, but the scenario at work may not be as those of the framework, but at a critical time or rely on it to fix the problem, as the saying goes, know ourselves, can only know yourself, go JVM as a senior engineer a hurdle, beginning from this article, we will go step by step analysis, to explain, to overcome the mountain.
Java virtual machine class loading mechanism related articles for a search a lot, you also do not have to repeat it again. Here remove a code problem to play big brother to play with, if you look to see the clues, then congratulations, you can down the mountain:
This road interview will ask questions face JVM 70 percent of Java programmers do wrong

Question: What does this program outputs are?
Generally for such problems, small partners mind certainly emerge out of this knowledge
in Java assignment order:
static variables of the parent class assigned
its own static variable assignment
parent class member variable assignment and parent class assignment block
parent class constructor assignment
itself members and their variable assignment block assignment
itself constructor assignment
according to this theory output what is it?
The answer output: 14, so right Me?
It certainly is not correct, this is not to say the above rule is not correct, but that can not simply apply this rule. The correct answer is:
This road interview will ask questions face JVM 70 percent of Java programmers do wrong

There is no answer yet? One of the main points here: instance initialization initialize do not have to begin at the end of class initialization. Life cycle class are: Load -> Authentication -> Prepare -> parse -> Initialization -> Use -> Uninstall, only in the preparation phase and the initialization phase will involve the class assignment and initialization of variables, so only for two stages for analysis;
preparation phase class needs to be done is to allocate memory for class variables and set the default value, and therefore class variable st is null, b is 0; (Note that if the class variable is final, when javac compiler will generate value for the ConstantValue property, in the preparation phase of the virtual machine will be based ConstantValue setting variable to the value specified, if here so defined: static final int b = 112, then the value is in the preparation stage 112 b, which is no longer the 0 .)
initialization phase class needs to do is to perform the class constructor (class constructor compiler to collect all assignment static block of statements and class variables according to the statement sequence combined generating class constructor in the source code, the constructor of the object is (), is the class constructor (), can be seen in the stack information), to perform assignment so the first static variable, i.e. st = new StaticTest (), this time will Initialized, which object-object member variable is initialized and then the constructor is executed, so the print 2-> set to a 110-> constructor is executed (print 3, a case has been assigned the value 110, but only sets the default b after initialization value of 0, the assignment was not completed action), and other objects to complete before continuing execution class constructor statement, then not explained in detail, can be executed in the order of statements in the source code.
It also involves a cold inside knowledge, is there a special initialization logic during nesting. In particular embedded variable happens to be a static member, and is an instance of this class. This leads to an interesting phenomenon: "instance initialization actually appear before the static initialization." In fact, not in advance, you have to know whether or not initialization java record time. Look at a simplified code, the key issues to explain:
This road interview will ask questions face JVM 70 percent of Java programmers do wrong

根据上面的代码,有以下步骤:
首先在执行此段代码时,首先由main方法的调用触发静态初始化。
在初始化Test 类的静态部分时,遇到st这个成员。
但凑巧这个变量引用的是本类的实例。
那么问题来了,此时静态初始化过程还没完成就要初始化实例部分了。是这样么?
从人的角度是的。但从java的角度,一旦开始初始化静态部分,无论是否完成,后续都不会再重新触发静态初始化流程了。
因此在实例化st变量时,实际上是把实例初始化嵌入到了静态初始化流程中,并且在楼主的问题中,嵌入到了静态初始化的起始位置。这就导致了实例初始化完全至于静态初始化之前。这也是导致a有值b没值的原因。
最后再考虑到文本顺序,结果就显而易见了。
详细看到这里,心中大概有个结论了吧。
This road interview will ask questions face JVM 70 percent of Java programmers do wrong

这是20道面试官经常问的JVM面试题,看一看你会几道?

  1. 内存模型以及分区,需要详细到每个区放什么。
  2. 堆里面的分区:Eden,survival (from+ to),老年代,各自的特点。
  3. 对象创建方法,对象的内存分配,对象的访问定位。
  4. GC 的两种判定方法:
  5. SafePoint 是什么
  6. GC 的三种收集方法:标记清除、标记整理、复制算法的原理与特点,分别用在什么地方,如果让你优化收集方法,有什么思路?
  7. GC 收集器有哪些?CMS 收集器与 G1 收集器的特点。
  8. Minor GC 与 Full GC 分别在什么时候发生?
  9. 几种常用的内存调试工具:jmap、jstack、jconsole、jhat
  10. Several class loading procedure:
    11.JVM memory partition which several zones, each zone What is the role of?
    Whether an object 12 is determined and survival (GC determination method or object)?
    13. DESCRIPTION garbage java mechanisms?
    method 14.java in garbage collection What?
    15.java memory model
    16.java class loading process?
  11. Description of java class loading mechanism?
  12. Parent class loader delegation model mechanism?
    19. What is the class loader class loader What?
    20. Description of java memory allocation and recovery policy rates and Minor GC and Major GC

Finally,
we welcome the exchange, like the point of a praise yo remember the article, thanks for the support!

Guess you like

Origin blog.51cto.com/14442094/2429291