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面试题的更多相关文章

  1. 金九银十,收下这份 Java String 面试题

    请点赞关注,你的支持对我意义重大. Hi,我是小彭.本文已收录到 GitHub · Android-NoteBook 中.这里有 Android 进阶成长知识体系,有志同道合的朋友,关注公众号 [彭旭 ...

  2. String面试题

    //a b c 分别是怎么存储的, a和b a和c分别有什么区别// c和d的区别是什么 String a= "hello";String b= "hello" ...

  3. Java String 面试题以及答案

    String是最常使用的Java类之一,整理的了一些重要的String知识分享给大家. 作为一个Java新手程序员,对String进行更深入的了解很有必要.如果你是有几年Java开发经验,可以根据目录 ...

  4. Java 几道常见String面试题

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

  5. Java基础知识强化31:String类之String的面试题

    1.先看一个图: 2.String面试题: (1)题1: package cn.itcast_02; /* * 看程序写结果 */ public class StringDemo3 { public ...

  6. 5道面试题,拿捏String底层原理!

    原创:微信公众号 码农参上,欢迎分享,转载请保留出处. String字符串是我们日常工作中常用的一个类,在面试中也是高频考点,这里Hydra精心总结了一波常见但也有点烧脑的String面试题,一共5道 ...

  7. 李洪强iOS经典面试题128

    1.写一个NSString类的实现 + (id)initWithCString:(c*****t char *)nullTerminatedCString encoding:(NSStringEnco ...

  8. 【转】C/C++程序员应聘常见面试题深入剖析

    1.引言 本文的写作目的并不在于提供C/C++程序员求职面试指导,而旨在从技术上分析面试题的内涵.文中的大多数面试题来自各大论坛,部分试题解答也参考了网友的意见­. 许多面试题看似简单,却需要深厚的基 ...

  9. ios 面试题 经典(比较全) 根据重点总结

    史上最全的iOS面试题及答案 1.写一个NSString类的实现 + (id)initWithCString:(c*****t char *)nullTerminatedCString encodin ...

随机推荐

  1. 转载 BCS 的好文章 1 - 怎么创建和使用BCS

    http://www.sharepointfabian.com/2010/04/16/how-to-create-configure-consume-sharepoint-2010-secure-st ...

  2. Python数据结构与算法--List和Dictionaries

    Lists 当实现 list 的数据结构的时候Python 的设计者有很多的选择. 每一个选择都有可能影响着 list 操作执行的快慢. 当然他们也试图优化一些不常见的操作. 但是当权衡的时候,它们还 ...

  3. 关于 xib 的使用

    前两天写百度地图的时候要添加 一个标注的泡泡view  但有些复杂所以想用xib 拖拽出一个View ,拖拽出来之后发现添加不到Controller中 ,郁闷!! 终于找到了方法: //先获取NIb ...

  4. OC中的复合

    #import <Foundation/Foundation.h> #import "Car.h" int main(int argc, const char * ar ...

  5. iOS实现三屏复用循环广告[从服务器请求的广告]

    循环广告我们在开发中已经是熟得不能再熟了,今天整理这篇scrollview三屏复用广告 原理使用scrollview里的三个imageview分别去加载不同的图片,用少量的资源来显示大量或不确定的广告 ...

  6. Effective Java 77 For instance control, prefer enum types to readResolve

    The readResolve feature allows you to substitute another instance for the one created by readObject ...

  7. Memcache修改端口

    修改端口 网上很多的说法都无法起作用(像下面这样) D:\.......memcached -p 10000 -d start 现在有两种解决方法 ①直接修改注册表 HKEY_LOCAL_MACHIN ...

  8. 2015年p2p网络借贷平台的发展现状

    2015年春暖花开,莺飞草长,股市大涨大跌起起落落,P2P网络借贷收到越来越多的人关注,P2P网络借贷平台是p2p借贷与网络借贷相结合的金 融服务网站,这么多P2P网络借贷平台排我们应该如何选择呢?小 ...

  9. javascript 特效实现(3)—— 鼠标滑过显示二级菜单效果

    1. 关键代码:使用 switch 或 if 判断语句,改变对应的二级菜单显示方式为 block 或 none function selectTabMenu(i){ switch(i){ case 7 ...

  10. sphinx增量索引

    首先建立一个计数表,保存数据表的最新记录ID CREATE TABLE `sph_counter` (  `id` int(11) unsigned NOT NULL,  `max_id` int(1 ...