An interview frequently asked question Java threads, causing a chain of tragedy

0. EDITORIAL

These two days have cooked a common Java interview questions, there is no suspense wrong, after running the correct answer, find their own knowledge base are not able to explain why the full, very ashamed, so with this article, its summary reflection.


An interview frequently asked question Java threads, causing a chain of tragedy


1. Topic

Look under the topic:

An interview frequently asked question Java threads, causing a chain of tragedy


Operating results as follows:

hello
hello
changed

The latter two can be understood, parameters, arguments, passed by value, pass by reference a mixed Han, can justify, but first why do hello, str not be reassigned yet, or how the original print value.

After a top doubts, meal Baidu, the amount does not, after Google, find the following concepts understanding is not very thorough:

What is the stack memory, heap memory, What's the difference?

Initializing a primitive type or a data object in memory is how to proceed?

What is the value passed, passed by reference, are there any difference?

String type of data stored in the memory of what area?

String str = "a"; and String str = new String ( "a"); What is the difference in memory allocation?

With these questions, look down together.

2. stack memory, heap memory

Stack memory (stack)

Some basic types of variables (byte, short, int, long, float, double, boolean, char), and object reference variables (Object obj = new Object (); obj is the reference variable) defined in the function in the function stack memory allocation.

When a variable is defined in a code block, Java stack is allocated in variable memory space, when exceeding the scope of variables, Java automatically freed memory space allocated for the variable, the memory space immediately It is used for other purposes.

The advantage of the stack memory, the access speed is faster than the reactor, after register, stack memory data can be shared. But the disadvantage is that there is data in the stack size and survival must be determined, lack of flexibility.

Heap memory (heap)

Created by new objects and arrays (array of new not new can be) stored in the heap memory allocated on the heap memory managed by the JVM garbage collection mechanism.

在堆内存中存储的对象或数组,可以在栈内存中对应一个引用变量,引用变量的取值为对象或数组在堆内存中的首地址,程序可以通过栈内存的引用变量来对数组或对象进行操作。

Object obj = new Object(); obj为引用变量,可以通过obj变量操作Object。

3.基本类型数据、对象的内存分配

基本类型数据

int a = 1;
int b = 1;
int c = 2;

An interview frequently asked question Java threads, causing a chain of tragedy


变量

步骤分析:

1.在栈内存中创建一个变量名为a的引用,然后查找栈内存中是否存在1这个值,未找到,将1存入栈内存并将变量a指向1。

2.在栈内存中创建一个变量名为b的引用,然后查找栈内存中是否存在1这个值,找到了,将变量b指向1。

3.在栈内存中创建一个变量名为c的引用,然后查找栈内存中是否存在2这个值,未找到,将2存入栈内存并将变量c指向2。

在上述步骤可以看到,栈内存中的数据是可以共享的,虽然数据是共享的,但是变量b的修改,并不会影响到变量a。

对象

Object obj = new Object();

An interview frequently asked question Java threads, causing a chain of tragedy


对象

步骤分析:

1.在栈内存中创建一个变量名为obj的引用。

2.在堆内存中创建一个Object对象,堆内存会自动计算Object对象的首地址值,假设为0x0001。

3.栈内存中的变量obj指向堆内存中Object对象的首地址0x0001。

4.值传递、引用传递

先来说说形参和实参,看下面一段代码:

public class Test {
 public static void main(String[] args) {
 int a = 0;
 fun(a);
 }
 public static void fun(int a) {
 a = 1;
 }
}

其中,fun(int a)中的a,它只有在fun方法被调用期间a才有意义,也就是会被分配内存空间,在fun方法执行完成后,a就会被销毁释放空间,这个a为 形参 。

fun(a)中的a,它是方法被调用前就已经被初始化了的,并且在方法被调用时传入,这个a为 实参 。

了解完了形参和实参,我们再来说值传递和引用传递:

值传递:

在方法的执行过程中,实参把它的实际值传递给形参,此传递过程就是将实参的值复制一份传递到函数中,这样如果在函数中对该值(形参的值)进行了操作,将不会影响实参的值。

java的八种基本数据类型或者String、Integer、Double作为参数时,可以理解为值传递,修改形参不会影响实参。

引用传递:

在方法的执行过程中,形参和实参的内容相同,指向堆内存中的同一块内存地址,也就是说操作的其实都是源数据,修改形参会影响实参。

java的类类型、接口类型和数组作为参数时,可以理解为引用传递,修改形参会影响实参。

5.String类型

String类型十分特殊,它不属于基本数据类型,但又可以像基本数据类型一样用 = 赋值,还可以通过 new 进行创建,一起来看看两种创建方式在内存中有什么区别。

String str = “a”;


An interview frequently asked question Java threads, causing a chain of tragedy


String str = “a”;

步骤分析:

  • 1.在栈内存中创建一个变量名为str的引用。

  • 2.在常量池中查找是否有字符串a,没有找到,创建一个字符串a。

  • 3.栈内存中的变量str指向常量池中的字符串a。

String str = new String("a");


An interview frequently asked question Java threads, causing a chain of tragedy


String str = new String("a");

步骤分析:

  • 1.在栈内存中创建一个变量名为str的引用。

  • 2.在堆内存中创建一个String对象,堆内存会自动计算String对象的首地址值,假设为0x0001。

  • 3.栈内存中变量str指向堆内存中String对象的首地址0x0001。

  • 4.String对象首先到常量池中查找有没有字符串a,如果有则指向字符串a,如果没有则创建。

6.解题分析

在学习了上面的知识之后,我们再回过头来分析一下这道面试题:

An interview frequently asked question Java threads, causing a chain of tragedy


以A、B、C标识三段逻辑,分别来看下:

  • A:


An interview frequently asked question Java threads, causing a chain of tragedy


步骤分析:

  • 1.在栈内存中创建一个变量名为str(实参)的引用。

  • 2. Find a string constant pool hello, is not found, create a string hello.

  • Variable str (argument) in the stack memory 3. hello to a string constant pool.

  • 4. Create a reference variable named str (parameter) in the stack memory.

  • 5. Find a string constant pool changed, not found, create a string changed.

  • Variable str (parameter) in the stack memory 6. changed to a string constant pool.

A print str real-valued parameter, the output hello

  • B:


An interview frequently asked question Java threads, causing a chain of tragedy


  • 1. Create a variable named a (argument) in the stack memory references.

  • 2. Create a String object heap memory address is 0x0001, reference variables a (argument) point to this address.

  • 3.String objects look first to the constant pool has no string hello, is not found, create a string hello and point it in the constant pool.

  • 4. Create a variable named a (parameter) in the stack memory references.

  • In the heap memory 5. Create a String object, the address of 0x0011, reference variable a (parameter) to point to this address.

  • 6.String objects first to the constant pool Finding no strings changed, not found, create a string changed in the constant pool and point to it.

At this time, a value of the argument in the print output hello

  • C:


An interview frequently asked question Java threads, causing a chain of tragedy


  • 1. Create a variable named A1 in the stack memory (argument) references.

  • 2. Create a String object heap memory address is 0x0001, reference variables a1 (argument) point to this address.

  • 3.String objects look first to the constant pool has no string hello, is not found, create a string hello and point it in the constant pool.

  • 4. Create a reference variable named a1 (parameter) that points to the address 0x0001 in the stack memory.

  • 5.String objects first to the constant pool Finding no strings changed, not found, create a string changed in the constant pool and point it no longer points to the string hello.

At this time, a value of the argument in the print output changed

7. Written in the last

Here, the summary of the Road Java interview questions is complete, still many things associated, if you encounter a problem or have the wrong place, you can give me a message, thank you!


Guess you like

Origin blog.51cto.com/14528283/2445582