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 ...
随机推荐
- arcgis python 更新顺序号
i = 0def myFun(): global i i=i +1 return i myFun() ========================== accumulate( ) total = ...
- 软件快速开发平台 JEPF
JEPF新一代软件快速开发平台(Java Elephant Platform)是一款优秀的平台产品,它本着灵活.快捷开发.高性能.高协作性.高稳定性.高可用性.人性化的操作体验为设计宗旨历经2年研发成 ...
- oracle10g 统计信息查看、收集
1. 统计信息查看 1.1 单个表的全局统计信息.统计效果查看 2. 统计信息分析(收集) 2.1 分析工具选择 2.2 分析前做index重建 2.3 分析某数据表,可以在PL/SQL的comm ...
- Thread.CurrentPrincipal & HttpContext.Current.User
据说要这样写才稳妥 // This principal will flow throughout the request.VoyagerPrincipal principal = new Voyage ...
- 安卓开发_深入学习ViewPager控件
一.概述 ViewPager是android扩展包v4包(android.support.v4.view.ViewPager)中的类,这个类可以让用户左右切换当前的view. ViewPager特点: ...
- NDK、SDK以及JNI的关系
最近由于项目的需要,使用到了Android的NDK技术,对项目核心算法跨平台的移植.简答而言,就是使用C对原来的算法进行了改进,并集成到原 来的app项目里. 从前的项目一直没有使用NDK进行开发的机 ...
- Android 文件访问权限的四种模式
Linux文件的访问权限* 在Android中,每一个应用是一个独立的用户* drwxrwxrwx* 第1位:d表示文件夹,-表示文件* 第2-4位:rwx,表示这个文件的拥有者(创建这个文件的应用) ...
- iOS 抽象工厂模式
iOS 抽象工厂模式 什么是抽象工厂模式 简单了解一下 按照惯例,我们先了解一下什么是抽象工厂模式.抽象工厂模式和工厂方法模式很相似,但是抽象工厂模式将抽象发挥的更加极致,是三种工厂模式中最抽象的一种 ...
- 【原】visual studio添加现有文件夹的方法
由于使用版本管理器协调工作,有时同事就直接上传文件夹了,但右键添加现有项时不能添加文件夹的. 在工具栏"项目"下面有个"显示所有文件",选择这个,整个工程的文件 ...
- iOS开发--隐藏(去除)导航栏底部横线
iOS开发大部分情况下会使用到导航栏,由于我司的app导航栏需要与下面紧挨着的窗口颜色一致,导航栏底部的横线就会影响这个美观,LZ使用了以下方法.觉得不错,分享来给小伙伴们. 1)声明UIImageV ...