概述

本类文章会不段更新分析学习到的经典面试题目,在此记录下来便于自己理解。如果有不对的地方还请各位观众拍砖。 
今天主要分享一下常用的字符串的几个题目,相信学习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.
* <p>
* A pool of strings, initially empty, is maintained privately by the
* class <code>String</code>.
* <p>
* When the intern method is invoked, if the pool already contains a
* string equal to this <code>String</code> object as determined by
* the {@link #equals(Object)} method, then the string from the pool is
* returned. Otherwise, this <code>String</code> object is added to the
* pool and a reference to this <code>String</code> object is returned.
* <p>
* It follows that for any two strings <code>s</code> and <code>t</code>,
* <code>s.intern()&nbsp;==&nbsp;t.intern()</code> is <code>true</code>
* if and only if <code>s.equals(t)</code> is <code>true</code>.
* <p>
* All literal strings and string-valued constant expressions are
* interned. String literals are defined in section 3.10.5 of the
* <cite>The Java&trade; Language Specification</cite>.
*
* @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);

好了,今天就到这里。之后会继续分析。如果喜欢我的文章欢迎关注我。各位看官大爷的支持是我最大的动力!!

Java你可能不知道的事系列1的更多相关文章

  1. Java你可能不知道的事系列(1)

    概述 本类文章会不段更新分析学习到的经典面试题目,在此记录下来便于自己理解.如果有不对的地方还请各位观众拍砖. 今天主要分享一下常用的字符串的几个题目,相信学习java的小伙伴们对String类是再熟 ...

  2. java你可能不知道的事(2)--堆和栈

    在java语言的学习和使用当中你可能已经了解或者知道堆和栈,但是你可能没有完全的理解它们.今天我们就一起来学习堆.栈的特点以及它们的区别.认识了这个之后,你可能对java有更深的理解. Java堆内存 ...

  3. java你可能不知道的事(2)--堆和栈<转>

    在java语言的学习和使用当中你可能已经了解或者知道堆和栈,但是你可能没有完全的理解它们.今天我们就一起来学习堆.栈的特点以及它们的区别.认识了这个之后,你可能对java有更深的理解. Java堆内存 ...

  4. Java你可能不知道的事(3)HashMap

    概述 HashMap对于做Java的小伙伴来说太熟悉了.估计你们每天都在使用它.它为什么叫做HashMap?它的内部是怎么实现的呢?为什么我们使用的时候很多情况都是用String作为它的key呢?带着 ...

  5. Spring中你可能不知道的事(一)

    Spring作为Java的王牌开源项目,相信大家都用过,但是可能大家仅仅用到了Spring最常用的功能,Spring实在是庞大了,很多功能可能一辈子都不会用到,今天我就罗列下Spring中你可能不知道 ...

  6. ES6 你可能不知道的事 – 基础篇

    序 ES6,或许应该叫 ES2015(2015 年 6 月正式发布),对于大多数前端同学都不陌生. 首先这篇文章不是工具书,不会去过多谈概念,而是想聊聊关于每个特性 你可能不知道的事,希望能为各位同学 ...

  7. overflow:hidden 你所不知道的事

    overflow:hidden 你所不知道的事 overflow:hidden这个CSS样式是大家常用到的CSS样式,但是大多数人对这个样式的理解仅仅局限于隐藏溢出,而对于清除浮动这个含义不是很了解. ...

  8. js值----你所不知道的JavaScript系列(6)

    1.数组 在 JavaScript 中,数组可以容纳任何类型的值,可以是字符串.数字.对象(object),甚至是其他数组(多维数组就是通过这种方式来实现的) .----<你所不知道的JavaS ...

  9. js类型----你所不知道的JavaScript系列(5)

    ECMAScirpt 变量有两种不同的数据类型:基本类型,引用类型.也有其他的叫法,比如原始类型和对象类型等. 1.内置类型 JavaScript 有七种内置类型: • 空值(null) • 未定义( ...

随机推荐

  1. Java魔法堂:类加载机制入了个门

    一.前言 当在CMD/SHELL中输入 $ java Main<CR><LF> 后,Main程序就开始运行了,但在运行之前总得先把Main.class及其所依赖的类加载到JVM ...

  2. Maven提高篇系列之(六)——编写自己的Plugin(本系列完)

    这是一个Maven提高篇的系列,包含有以下文章: Maven提高篇系列之(一)——多模块 vs 继承 Maven提高篇系列之(二)——配置Plugin到某个Phase(以Selenium集成测试为例) ...

  3. 解决360、猎豹浏览器等极速模式下css3兼容问题

    有时候你会发现你写的animation动画的css3效果,在IE.谷歌.火狐等主流的新版本的浏览器的是没有什么兼容问题的,即便你不写前缀,也是可以显示动画效果的.然后,你本地在360浏览器或猎豹浏览器 ...

  4. Redis使用总结(2):Python接口

    安装redis-py sudo pip2 install redis 牛刀小试 redis连接实例是线程安全的,可以直接将redis连接实例设置为一个全局变量直接使用.如果需要另一个Redis实例(o ...

  5. 介绍开源的.net通信框架NetworkComms框架 源码分析(二十 )ConnectionCreate

    原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架  作者是英国人  以前是收费的 目前作者已经开源  许可是 ...

  6. PHP程序的常见漏洞攻击分析

    综述:PHP程序也不是固若金汤,随着PHP的广泛运用,一些黑客们也在无时不想找PHP的麻烦,通过PHP程序漏洞进行攻击就是其中一种.在节,我们将从全局变量,远程文件,文件上载,库文件,Session文 ...

  7. 不可或缺 Windows Native (17) - C++: 类与对象

    [源码下载] 不可或缺 Windows Native (17) - C++: 类与对象 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 类与对象 示例1.类的设计CppE ...

  8. Android使用SQLite数据库(2)

    打开SQLite数据库,首先要建立一个DatabaseHelper类的实例,然后,再获得数据库: DatabaseHelper mDBH; SQLiteDatabase db; mDBH = new ...

  9. PyMySQL Evaluation

    PyMySQL Evaluation This page will capture issues related to Openstack moving to the PyMySQL driver f ...

  10. 互联网背景时代下的大机遇,为什么用nosql

    1.单机MySQL的美好年代 在90年代,一个网站的访问量一般都不大,用单个数据库完全可以轻松应付.在那个时候,更多的都是静态网页,动态交互类型的网站不多. 上述架构下,我们来看看数据存储的瓶颈是什么 ...