堆栈,== equals 知识点

public class StaticHeapTest {
    public static void main(String[] args) {
        //栈 stack    堆 heap
        int a=3;
        int b=3;
        System.out.println(a==b);//true
        Integer A=3;
        Integer B=3;
        System.out.println(A==B);//true
        System.out.println(A.equals(B));//true
        System.out.println(a==A);//true
        System.out.println(A.equals(a));//true


        String as="aaaa";
        String bs="aaaa";
        String As=new String("aaaa");
        System.out.println(as==As);//false
        System.out.println(as.equals(As));//true
        System.out.println(as==bs);//true
        System.out.println(bs.equals(as));//true
    }
}

结果:
true
true
true
true
true
false
true
true
true

主要知识点:
堆栈的区别:
栈(stack)与堆(heap)
栈的优势是,存取速度比堆要快
栈:栈中存放基本类型的变量数据和对象的引用,但对象本身不存放在栈中,而是存放在堆(new 出来的对象)或者常量池中(对象可能在常量池里)(字符串常量对象存放在常量池中。);

堆:存放所有new出来的对象;

静态域:存放静态成员(static定义的);
常量池:存放字符串常量和基本类型常量(public static final)。
equal和==的区别:
==号,根据JDK的说明,只有在两个引用都指向了同一个对象时才返回真值,但string的equal方法进行了重写。

栈有一个很重要的特殊性,就是存在栈中的数据可以共享。

String a=”a” 是放在常量池中的,而String a =new String(“a”); 是存放在堆中的

import java.util.*;
public class EqureTest {
    public static void main(String[] args){
        String a="abc";
        String b="abc";
        System.out.println(a==b);//true
        a.toUpperCase();//将常量池中的abc转为ABC
        System.out.println(a);//abc
        System.out.println(a==b);//true
        a=a.toUpperCase();
        System.out.println(a);//ABC
        System.out.println(b);//abc
        System.out.println(a==b);//false

        String c="a"+"bc";
        String x="a";
        String y="bc";
        final String z="bc";
        System.out.println(y==z);//true
        String d=x+y;
        String e=x+z;
        String f="a"+z;
        System.out.println(c==d);//false
        System.out.println(c==b);//true
        System.out.println(c==e);//false
        System.out.println(c==f);//true
    }

}

上一篇讲到String a=”a” 是放在常量池中的,常量池是有共享性的,所以a==b
a.toUpperCase();//将常量池中的abc转为ABC,但a还是abc,所以a==b
但a=a.toUpperCase();就将a指向了“ABC”,所以a!=b

由于在字符串的”+”连接中,有字符串引用存在,而引用的值在程序编译期是无法确定的,即x+y无法被编译器优化,只有在程序运行期来动态分配并将连接后的新地址赋给d。所以上面程序的结果也就为false。

z字符串加了final修饰,对于final修饰的变量,它在编译时被解析为常量值的一个本地拷贝存储到自己的常量池中或嵌入到它的字节码流中。所以此时的f=”a”+z和”a” + “bc”效果是一样的。故上面程序的结果为true。

Integer -128–127是放在常量池,其他的在堆中

        //关于Integer的坑
        Integer a=18;//常量池
        Integer b=18;//常量池
        Integer c=new Integer(18);//堆
        System.out.println(a==b);//true
        System.out.println(a==c);//false

        Integer d=2000;//堆
        Integer e=2000;//堆
        Integer f=new Integer(2000);//堆
        //虽然都在堆中但不共用
        System.out.println(d==e);//false
        System.out.println(d==f);//false

基本的类java都已经覆写了.equals()方法。
类里面的int也是放在栈中
==只能比较相同类型,否则编译会出错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值