---
title: "JVM 笔记"
author: "vortexchoo"
date: "2014-12-03T09:41:00Z"
tags: []
categories: ["编程语言"]
slug: "jvm"
draft: false
lastmod: "2014-12-03T09:47:19Z"
id: 2162785
url: "https://vortexchoo.iteye.com/blog/2162785"
---

<div class="iteye-blog-content-contain" style="font-size: 14px"></div>
例如：

  public void doSth(){

   String a = "abc";

   String b = "a"+"b"+"c";

   system.out.println(a==b);

  }

（“==”是比较内存单元格的）

结果是true jvm编译的时候：

     a="abc";

     b="abc";

a与b所指向的内存空间是一致的。

又例如：

public void doSth(){

   String a = "a";

   String b = "b";

   String c = "c";

   String d = "abc";

   system.out.println(a+b+c==d);

}

结果是false，当引用变量的时候jvm不会认为内存空间是指定的。因此是false

例3：

public void doSth(){

  Integer a = 2;
           Integer b = 2;
           Integer c = 127;
           Integer d = 127;
           System.out.println(a==b);
           System.out.println(c==d);

}

结果为 true,true。

public void doSth(){

           Integer a = 2;
           Integer b = 2;
           Integer c = 128;
           Integer d = 128;
           System.out.println(a==b);
           System.out.println(c==d);

}
Integer 缓存-128 ~ 127 之间的数字，因此在该范围内的相同数字都指向的是一个内存空间，该范围外的就去new 
结果为true, false，超出128的都会是false 其实此时的c,d都是重新开辟的内存空间。