博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java你可能不知道的事系列1
阅读量:7127 次
发布时间:2019-06-28

本文共 4019 字,大约阅读时间需要 13 分钟。

概述

本类文章会不段更新分析学习到的经典面试题目,在此记录下来便于自己理解。假设有不正确的地方还请各位观众拍砖。

今天主要分享一下经常使用的字符串的几个题目。相信学习java的小伙伴们对String类是再熟悉只是了,今天我们就来和她再次邂逅。好了以下開始。

先来说说String特点

String是不可变的常量,每当我们创建一个字符串对象的时候。假设堆区的常量池里不存在这个字符串。就会创建一个存储在常量池里(String存的地方叫String pool),假设存在了,就直接把变量的地址指向常量池里。比方:String b = “abc” 这句话 内存表演示样例如以下。

以下開始上题

这里写图片描写叙述

1.1

String s1 = new String("abc");String s2 = new String("abc"); System.out.println(s1 == s2);

输出结果是什么呢?

从上面的图也大概说了jvm里面有栈区、堆区。栈区里面主要存放的是局部变量。堆区里存放的是new出来的对象。==对于对象类型比較的是地址。所以在s1和s1是分别引用了堆里面new出来的不同对象的地址,图形理解例如以下

这里写图片描写叙述

答案非常明显了。地址不同 输出false.

1.2

String s1 = "abc";StringBuffer s2 = new StringBuffer(s1); System.out.println(s1.equals(s2));

这是true 还是false呢?答案是false。

首先s1变量引用了字符串”abc”,然后StringBuffer s2 = new StringBuffer(s1)。新建了一个StringBuffer对象调用append()方法返回自身。

调用String的equals方法。重点就是这个equals方法里有个instance of,必需是同一类型的才进行比較否则直接返回false。

来看一下源代码:

/**     * Compares this string to the specified object.  The result is {
@code * true} if and only if the argument is not {
@code null} and is a {
@code * String} object that represents the same sequence of characters as this * object. * * @param anObject * The object to compare this {
@code String} against * * @return {
@code true} if the given object represents a {
@code String} * equivalent to this string, {
@code false} otherwise * * @see #compareTo(String) * @see #equalsIgnoreCase(String) */ public boolean equals(Object anObject) { if (this == anObject) { return true; } //关键点就在这里了 if (anObject instanceof String) { String anotherString = (String) anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }

1.3

以下的代码在内存会产生几个对象呢?

String s1 = new String(“abc”);
String s2 = new String(“abc”);

答案:3个

有了上面的分析。相信大家都明确了,new了两个对象。加上string pool里的一个”abc”。

1.4

以下的代码输出结果是啥?

String s1 = "abc";String s2 = new String("abc");s2.intern();System.out.println(s1 ==s2);

我们可能对intern()这种方法不太熟悉。先来看看凝视:

/**     * Returns a canonical representation for the string object.     * 

* A pool of strings, initially empty, is maintained privately by the * class String. *

* When the intern method is invoked, if the pool already contains a * string equal to this String object as determined by * the {@link #equals(Object)} method, then the string from the pool is * returned. Otherwise, this String object is added to the * pool and a reference to this String object is returned. *

* It follows that for any two strings s and t, * s.intern() == t.intern() is true * if and only if s.equals(t) is true. *

* All literal strings and string-valued constant expressions are * interned. String literals are defined in section 3.10.5 of the * The Java™ Language Specification. * * @return a string that has the same contents as this string, but is * guaranteed to be from a pool of unique strings. */ public native String intern();

凝视好多我草,关键的是这句:When the intern method is invoked, if the pool already contains a string equal to this String object as determined by

the {@link #equals(Object)} method, then the string from the pool is
returned. Otherwise, this String object is added to the
pool and a reference to this String object is returned.
大致就是说,假设常量池里不存在这个字符串,就创建一个而且返回地址。否则的话直接返回地址。

上面的代码第二行String s2 = new String(“abc”); s2事实上是引用到了new的对象,尽管在第三行调用了intern方法,可是没有赋值给s2,所以s2的引用还是没有变。所以返回false。

假设第三行代码改成s2 = s2.intern()就会返回true了。

String s1 = "abc"; String s2 = new String("abc"); s2 = s2.intern(); System.out.println(s1==s2);

好了,今天就到这里。之后会继续分析。

假设喜欢我的文章欢迎关注我。各位看官大爷的支持是我最大的动力!!

你可能感兴趣的文章
构建自己的词典库
查看>>
Graph Data Structures for Beginners
查看>>
@NotNull, @NotEmpty和@NotBlank
查看>>
使用jQuery的ajax实现同步获取返回值
查看>>
HBase 优化,columnFamily和qualifierColumn的设计原则
查看>>
java中Arraylist复制方法
查看>>
关于Kafka 的 consumer 消费者手动提交详解
查看>>
生成excle表格
查看>>
Maven基础篇之安装与目录结构
查看>>
hashMap 与hashtable
查看>>
JVM(四)垃圾回收的实现算法和执行细节
查看>>
input去空格
查看>>
spring boot项目打包成war并在tomcat上运行的步骤
查看>>
Node.js---01、初识NodeJS和Node.js的HTTP服务器搭建
查看>>
分享聚能聊"向代码致敬,寻找你的第83行"话题评论截图,得礼品咯!
查看>>
liunx 命令总结
查看>>
redis,Linux搭建
查看>>
门面模式
查看>>
预览文章: android使用webview时按后退退出的问题
查看>>
5月31日云栖精选夜读丨视频编辑,4k播放,3D游戏, 阿里云图形工作站,了解一下?...
查看>>