java11-2 String面试题
package cn.itcast_02;
/*
* String s = new String(“hello”)和String s = “hello”;的区别?
* 有。前者会创建2个对象,后者创建1个对象。
*
* ==:比较引用类型比较的是地址值是否相同
* equals:比较引用类型默认也是比较地址值是否相同,而String类重写了equals()方法,比较的是内容是否相同。
*/
- public class StringDemo2 {
- public static void main(String[] args) {
- String s1 = new String("hello");
- String s2 = "hello";
- System.out.println(s1 == s2);
- System.out.println(s1.equals(s2));
- }
- }
答案: false true
package cn.itcast_02;
/*
* 看程序写结果
*/
- 1 public class StringDemo3 {
- 2 public static void main(String[] args) {
- 3 String s1 = new String("hello");
- 4 String s2 = new String("hello");
- 5 System.out.println(s1 == s2);
- 6 System.out.println(s1.equals(s2));
- 7
- 8 String s3 = new String("hello");
- 9 String s4 = "hello";
- 10 System.out.println(s3 == s4);
- 11 System.out.println(s3.equals(s4));
- 12
- 13 String s5 = "hello";
- 14 String s6 = "hello";
- 15 System.out.println(s5 == s6);
- 16 System.out.println(s5.equals(s6));
- 17 }
- 18 }
- 答案:false true false true true true
解析:
- String s5 = "hello";
- String s6 = "hello";
- System.out.println(s5 == s6); true
因为之前的字符串赋值时,已经在方法区中创建了"hello",设它地址值为0x001,而s5、s6的时候,它会先在方法区中搜索是否存在"hello",存在的话,它直接调用到s6中,地址值也是0x001.
所以 s5 == s6 的结果为 true
- package cn.itcast_02;
- /*
- * 看程序写结果
- * 字符串如果是变量相加,先开空间,在拼接。
- * 字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。
- */
- public class StringDemo4 {
- public static void main(String[] args) {
- String s1 = "hello";
- String s2 = "world";
- String s3 = "helloworld";
- System.out.println(s3 == s1 + s2);
- System.out.println(s3.equals((s1 + s2)));
- System.out.println(s3 == "hello" + "world");
- System.out.println(s3.equals("hello" + "world"));
- }
- }
答案: false true true true
System.out.println(s3 == "hello" + "world");
因为这里的hello和world是字符串,先进行合并再和s3来判断的
通过反编译看源码,得知这里已经做好了处理。
System.out.println(s3 == "helloworld");
System.out.println(s3.equals("helloworld"));
java11-2 String面试题的更多相关文章
- 金九银十,收下这份 Java String 面试题
请点赞关注,你的支持对我意义重大. Hi,我是小彭.本文已收录到 GitHub · Android-NoteBook 中.这里有 Android 进阶成长知识体系,有志同道合的朋友,关注公众号 [彭旭 ...
- String面试题
//a b c 分别是怎么存储的, a和b a和c分别有什么区别// c和d的区别是什么 String a= "hello";String b= "hello" ...
- Java String 面试题以及答案
String是最常使用的Java类之一,整理的了一些重要的String知识分享给大家. 作为一个Java新手程序员,对String进行更深入的了解很有必要.如果你是有几年Java开发经验,可以根据目录 ...
- Java 几道常见String面试题
String s1="abc"; String s2="abc"; System.out.println(s1==s2); System.out.println ...
- Java基础知识强化31:String类之String的面试题
1.先看一个图: 2.String面试题: (1)题1: package cn.itcast_02; /* * 看程序写结果 */ public class StringDemo3 { public ...
- 5道面试题,拿捏String底层原理!
原创:微信公众号 码农参上,欢迎分享,转载请保留出处. String字符串是我们日常工作中常用的一个类,在面试中也是高频考点,这里Hydra精心总结了一波常见但也有点烧脑的String面试题,一共5道 ...
- 李洪强iOS经典面试题128
1.写一个NSString类的实现 + (id)initWithCString:(c*****t char *)nullTerminatedCString encoding:(NSStringEnco ...
- 【转】C/C++程序员应聘常见面试题深入剖析
1.引言 本文的写作目的并不在于提供C/C++程序员求职面试指导,而旨在从技术上分析面试题的内涵.文中的大多数面试题来自各大论坛,部分试题解答也参考了网友的意见. 许多面试题看似简单,却需要深厚的基 ...
- ios 面试题 经典(比较全) 根据重点总结
史上最全的iOS面试题及答案 1.写一个NSString类的实现 + (id)initWithCString:(c*****t char *)nullTerminatedCString encodin ...
随机推荐
- ubuntu vps 安装java
Introduction Java is a programming technology originally developed by Sun Microsystems and later acq ...
- WP多语言
WP多语言.本地化 建立一个Resources文件夹,再建立两个文件夹en-US和zh-CN,(分别表示美国英语.中文中国),每个文件夹下再新建资源文件(.resx),在资源文件中添加示例信息 新建一 ...
- ArcGIS的许可文件问题
ArcGIS我是从9.3版本开始使用的,当时破解非常麻烦,容易出现各种问题,当时生成的许可文件是一个单独的exe:后来10版本甚至10.1都出来,我还是使用的那个exe来生成许可文件,正常使用没有问题 ...
- SharePoint通过stsadm备份和还原子网站(不是网站集)
大家都知道SharePoint的stsadm命令提供了很多便捷甚至是唯一的操作方法! 这里列出的所有命令:http://www.cnblogs.com/dadongzuo/archive/2013/0 ...
- IOS程序开发中-跳转到 发送短信界面 实现发短信
前言:我发现我标题取的不好,谁帮我取个承接上下文的标题?评论一下,我改 项目需求:在程序开发中,我们需要在某个程序里面发送一些短信验证(不是接收短信验证,关于短信验证,传送门:http://www.c ...
- 干货-iOS、mac开源项目及库,以后我也会持续更新。
昨晚在网上看的干货,直接分享给大家了,觉得有用的,直接fork吧. https://github.com/Brances/TimLiu-iOS
- iOS之UI--微博个人详情页
前言:微博个人详情页,和我常用的的QQ空间的详情页是同样的.要求能够融会贯通,做这一类的界面能够快速上手实现. 动态图效果展示: 直接使用UINavigationBar->UITableView ...
- android studio annotation 配置过程
参考了好些配置,发现总有这样,那样的问题. 环境:androidstudio 1.5 preview 2 sdk 6.0 1.首先新建一个android项目. 过程略 2.配置project的buil ...
- 最小安装centos 7 无GUI静默安装 oracle 12c,打造轻量linux化服务器
CentOS 7 下载地址:http://mirrors.opencas.cn/centos/7/isos/x86_64/CentOS-7-x86_64-Everything-1511.iso 一.安 ...
- c# 进程间通信 IPC
最近在调试一个算法,想通过改变算法的参数看看结果有什么变化. 碰到一个麻烦的事情是,从磁盘加载.构建数据需要15分钟.这就比较讨厌了,也就是说我每次调一个参数前都要等15分钟启动时间? 于是我就想,能 ...