当前位置:网站首页>字符串 & 堆 & 方法区
字符串 & 堆 & 方法区
2022-06-28 10:51:00 【Hello_xzy_Word】
字符串 & 堆 & 方法区
1. 问题引入
package com.learn.java;
/** * @author xzy * @date 2020-09-17 22:33 * 说明:字符串 & 堆 & 方法区 */
public class Main {
public static String s = "HELLO WORLD!";
public static void main(String[] args) {
String s1 = "HELLO WORLD!";
String s2 = "HELLO WORLD!";
String s3 = new String("HELLO WORLD!");
System.out.println(s1 == s); // true
System.out.println(s2 == s); // true
System.out.println(s1 == s2);// true
System.out.println(s3 == s); // false
System.out.println(s3 == s1);// false
System.out.println(s3 == s2);// false
}
}
2.问题分析

2.1 字符串 & 方法区
class 文件中有一块被称作“常量池”的区域,常量池可以被视为 class 文件的资源仓库,常量池中主要存放两大类常量:
- 字面量:字符串、final常量……
- 符号引用:类/接口的全限定名、字段/方法的名称、字段/方法的描述符
在方法区有一块被称作“运行时常量池”的区域,当类被加载进 java 虚拟机的内存空间,class 文件常量池中的内容会被加载到运行时常量池。

使用 javap -v Main.class 命令查看程序编译后产生的字节码:

2.2 字符串 & 堆
堆是 java 虚拟机所管理的内存中最大的一块,此内存区域的唯一作用是存放对象实例和数组。
2.3 总结

s、s1、s2指向方法区中值为 “Hello WORLD!” 的 String 实例(类加载时创建);s3指向堆中值为 “Hello WORLD!” 的 String 实例(程序运行时创建)。
边栏推荐
- 一款自动生成单元测试的 IDEA 插件,开发效率提升 70% 以上!
- 一种跳板机的实现思路
- fastposter v2.8.4 发布 电商海报生成器
- Debug debugging in katalon
- [unity][ecs] learning notes (I)
- [unity][ecs] learning notes (III)
- Please consult me. I run the MYSQL to MySQL full synchronization of flykcdc in my local ide. This is in my local ide
- MySQL cannot be opened. Flash back
- 树莓派无需显示屏的VNC Viewer方式的远程连接
- 【力扣——动态规划】整理题目1:基础题目:509、70、746、62、63、343、96(附链接、题目描述、解题方法及代码)
猜你喜欢

Training and recognition of handwritten digits through the lenet-5 network built by pytorch
![[practice] appium settings app is not running after 5000ms](/img/3c/5a7544561231a15a4bb5cfa9e102b3.png)
[practice] appium settings app is not running after 5000ms

Markdown -- basic usage syntax

JS基础8

Realization of a springboard machine

JS基础1-JS引入与运算符

linux中源码安装mysql数据库(centos)

Dataease installation upgrade
![[Unity]EBUSY: resource busy or locked](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[Unity]EBUSY: resource busy or locked
![[Li Kou - dynamic planning] sort out topic 1: basic topics: 509, 70, 746, 62, 63, 343, 96 (with links, topic descriptions, problem solving methods and codes)](/img/02/4dbd97c8b8df1c96b7c8e1460aeda2.png)
[Li Kou - dynamic planning] sort out topic 1: basic topics: 509, 70, 746, 62, 63, 343, 96 (with links, topic descriptions, problem solving methods and codes)
随机推荐
fastposter v2.8.4 发布 电商海报生成器
Katalon当中的使用循环for、while和if...else、break、continue
Spatial-Temporal时间序列预测建模方法汇总
Remote connection of raspberry pie in VNC viewer mode without display
Hystrix 部署
JS基础5
移动命令
Metersphere implements UI automation elements that are not clickable (partially occluded)
Compression and decompression
Realize an air conditioner with compose to bring you cool in summer
树莓派无需显示屏的VNC Viewer方式的远程连接
【力扣——动态规划】整理题目1:基础题目:509、70、746、62、63、343、96(附链接、题目描述、解题方法及代码)
Redis database
Fastposter v2.8.4 release e-commerce poster generator
ICMP协议的作用,Ping of Death攻击的原理是什么?
Décompression par compression
File的io流与base64
Who knows if it is safe to open an account with CSC securities
Yann Lecun's new paper: the road to building automatic agents
Ffmpeg audio and video recording