

Java基础之String中equals,声明方式,等大总结
source link: https://blog.csdn.net/dmk877/article/details/49420141
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Java基础之String中equals,声明方式,等大总结
转载请注明出处:http://blog.csdn.net/dmk877/article/details/49420141
无论你是一个编程新手还是老手,提到String你肯定感觉特别熟悉,因为String类我们在学习java基础的时候就已经学过,但是String类型有我们想象的那么简单吗?其实不然,String类型的知识点还是比较多的。今天就和大家来一起讨论一下,关于String的一些容易让人疑惑的地方,废话不多说进入正题。。。如有谬误请批评指正,如果有疑问请留言。我会在第一时间修改或回答
通过本篇博客你将学到以下知识
①==和equals的区别,String a="abc"和String a=new String("abc")的堆内存和栈内存的变化
②String a="abc"和String a=new String("abc")两种声明方式的区别
④String对象的值是不能修改的,为啥可以重新赋值,并且改变
⑤String a;和String a = null,String a =""的区别
因为equals方法的作用是将内容进行比较,所以此处的返回结果都为true。
咦,这是啥子情况啊?上面我们刚得出结论"=="在String中比较的是地址值,为什么打印结果都是true呢?其实这是java中一种共享设计,这种设计思路是,在java中形成一个对象池,在这个池中保存多个对象,新实例化的对象如果已经在池中定义了则不再重新定义,而从池中取出继续使用。String采用了这种设计,在Java运行环境中有一个字符串池,由String类维护。执行语句String str1="hello"时,首先查看字符串池中是否存在字符串"hello",如果存在则直接将"hello"赋给str1,如果不存在则先在字符串池中新建一个字符串"hello",然后再将其赋给str1。执行语句String str=new String("hello")时,不管字符串池中是否存在字符串"hello",直接新建一个字符串"hello"(注意:新建的字符串"hello"不是在字符串池中),然后将其付给str。前一语句的效率高,后一语句的效率低,因为新建字符串占用内存空间。String str = new String()创建了一个空字符串,与String str=new String("")相同。
String a = null ;申明一个string类型的 a,同时在内存里申请了一个地址,但是该地址不指向任何引用地址;
String a = "" ;申明一个string类型的 a,既在内存里申请了地址,该地址又指向一个引用该字符串的引用地址;
String s2 = new String("s1"); //创建一个对象,并且以后每执行一次创建一个对象,一个引用
String s4 = "xyz"; //不创建对象,只是创建一个新的引用
Recommend
-
49
Java 中 hashCode() 和 equals() 的关系是面试中的常考点,如果没有深入思考过两者设计的初衷,这个问题将很难回答。除了应付面试,理解二者的关系更有助于我们写出高质量且准确的代码。 一.基础:hashCode() 和 equals() 简介
-
28
Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web. A resource can be something as simple as a file or a directory, or it can be a reference to a more complicated o...
-
4
C#中Equals和= =比较 这两种方式也是大家在日常编码工作当中用的比较多的判断方式、之前在使用的时候也没太关注两者在比较不同类型的时候存在哪些区别、
-
11
Skip to content Internationalization and Localization Performanc...
-
9
不实现equals方法的情况下比较java list 发表于 2017-04-10...
-
10
Debugging Java Equals and Hashcode Performance in ProductionHashcode and equals are at the crux of many hard to nail down Java performance issues. Here's how to track it in production.
-
4
本文为博主原创,未经允许不得转载: 1. equals 和 hashCode 方法之间的关系 这两个方法都是 Object 的方法,意味着 若一个对象在没有重写 这两个方法时,都会默认采用 Object 类中的方法实现,它们的关系为: 如果两个对...
-
8
A Deep Dive into Java 8 equals() and hashcode()
-
4
【面经】被虐了之后,我翻烂了equals源码,总结如下 原创 博学谷狂野架构师 ...
-
4
Add equals() method to the String class?I'm fairly new to web dev and my expertise is in C/C++, Java, and Python. I am perplexed by the complexity of comparing two strings in JavaScript.
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK