Skip to content

Commit

Permalink
Site updated: 2024-01-12 04:03:55
Browse files Browse the repository at this point in the history
  • Loading branch information
hubertwongcn committed Jan 11, 2024
1 parent dd9f8d8 commit 3d2adc6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions posts/37225.html
Original file line number Diff line number Diff line change
Expand Up @@ -599,13 +599,13 @@ <h6 id="基本数据类型的局部变量"><a href="#基本数据类型的局部
</ol>
<p><img src="https://cdn.jsdelivr.net/gh/HubertWongCN/image_host/img/202401111235495.png" alt="JVM Stack"></p>
<p>如上图,在方法内定义的变量直接存储在栈中</p>
<pre class="language-java" data-language="java"><code class="language-java"><span class="token number">1</span>int age<span class="token operator">=</span><span class="token number">50</span><span class="token punctuation">;</span>
<span class="token number">2</span>int weight<span class="token operator">=</span><span class="token number">50</span><span class="token punctuation">;</span>
<span class="token number">3</span>int grade<span class="token operator">=</span><span class="token number">6</span><span class="token punctuation">;</span></code></pre>
<pre class="language-java" data-language="java"><code class="language-java"><span class="token keyword">int</span> age<span class="token operator">=</span><span class="token number">50</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> weight<span class="token operator">=</span><span class="token number">50</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> grade<span class="token operator">=</span><span class="token number">6</span><span class="token punctuation">;</span></code></pre>

<p>当我们写“int age=50;”,其实是分为两步的:</p>
<pre class="language-java" data-language="java"><code class="language-java"><span class="token number">1</span>int age<span class="token punctuation">;</span><span class="token comment">//定义变量</span>
<span class="token number">2</span>age<span class="token operator">=</span><span class="token number">50</span><span class="token punctuation">;</span><span class="token comment">//赋值</span></code></pre>
<pre class="language-java" data-language="java"><code class="language-java"><span class="token keyword">int</span> age<span class="token punctuation">;</span><span class="token comment">//定义变量</span>
age<span class="token operator">=</span><span class="token number">50</span><span class="token punctuation">;</span><span class="token comment">//赋值</span></code></pre>

<p>首先JVM创建一个名为<code>age</code>的变量,存于局部变量表中,然后去栈中查找是否存在有字面量值为<code>50</code>的内容,如果有就直接把<code>age</code>指向这个地址,如果没有,JVM会在栈中开辟一块空间来存储<code>50</code>这个内容,并且把<code>age</code>指向这个地址。因此我们可以知道: <strong>我们声明并初始化基本数据类型的局部变量时,变量名以及字面量值都是存储在栈中,而且是真实的内容。</strong></p>
<p>我们再来看<code>int weight=50;</code>,按照刚才的思路:字面量为<code>50</code>的内容在栈中已经存在,因此<code>weight</code>是直接指向这个地址的。由此可见:<strong>栈中的数据在当前线程下是共享的</strong></p>
Expand Down
Loading

0 comments on commit 3d2adc6

Please sign in to comment.