Java基础知识强化31:String类之String的面试题
1.先看一个图:

2.String面试题:
(1)题1:
package cn.itcast_02; /*
* 看程序写结果
*/
public class StringDemo3 {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);// false
System.out.println(s1.equals(s2));// true String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3 == s4);// false
System.out.println(s3.equals(s4));// true String s5 = "hello";
String s6 = "hello";
System.out.println(s5 == s6);// true
System.out.println(s5.equals(s6));// true
}
}
(2)题2:
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);// false ,另外单独开了一个空间(地址不详),然后在这个空间中进行字符串拼接。
System.out.println(s3.equals((s1 + s2)));// true System.out.println(s3 == "hello" + "world");//这个我们错了,应该是true,因为之前s3 = "helloworld",已经存在"helloworld"直接返回这个
System.out.println(s3.equals("hello" + "world"));// true // 通过反编译看源码,我们知道这里已经做好了处理。
// System.out.println(s3 == "helloworld");
// System.out.println(s3.equals("helloworld"));
}
}
程序理解示意图:

Java基础知识强化31:String类之String的面试题的更多相关文章
- Java基础知识强化之集合框架笔记76:ConcurrentHashMap之 ConcurrentHashMap简介
1. ConcurrentHashMap简介: ConcurrentHashMap是一个线程安全的Hash Table,它的主要功能是提供了一组和Hashtable功能相同但是线程安全的方法.Conc ...
- Java基础知识强化43:StringBuffer类之StringBuffer和String的相互转化
1. String和StringBuffer的相互转换 思想是:A-----B的转换,我们把A转换为B,其实是为了使用B的功能:B-----A的转换,我们可能的结果是A类型,所以还要转换回来 2. 案 ...
- Java基础知识强化35:String类之String的其他功能
1. String类的其他功能: (1)替换功能: String replace(char old, char new) String replace(String old,String new) ( ...
- Java基础知识强化34:String类之String类的转换功能
1. String类的转换功能 String[] split(String regex)//将字符串变成字符串数组(字符串切割) byte[] getBytes()//将字符串变成字节数组 char[ ...
- Java基础知识强化33:String类之String类的获取功能
1. String类的获取功能 int length() // 获取字符串中字符的个数(长度) char charAt(int index)//根据位置获取字符 int indexOf(int ch) ...
- Java基础知识强化32:String类之String类的判断功能
1. String类的判断功能: boolean equals (Object obj ) boolean equalsIgnoreCase (String str ) boolean contain ...
- Java基础知识强化29:String类之String类构造方法
1. 常用String构造方法使用: package cn.itcast_01; /* * 字符串:就是由多个字符组成的一串数据.也可以看成是一个字符数组. * 通过查看API,我们可以知道 * A: ...
- Java基础知识强化101:Java 中的 String对象真的不可变吗 ?
1. 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变的对象呢? 可以这样认为:如果一个对象,在它创建完成之后,不能再改变它的状态,那么这个对 ...
- Java基础知识强化之集合框架笔记59:Map集合之TreeMap(TreeMap<String,String>)的案例
1. TreeMap类的概述: 键是红黑树结构,可以保证键的排序和唯一性. 2. TreeMap案例: TreeMap<String, String> 代码示例: package cn.i ...
随机推荐
- ubuntu远程连接
apt-cache search openssh-server //直接用apt-get install openssh-server安装.记不清包名字时可用apt-cache search o ...
- [技术翻译]Guava-libraries(一): 用户指导
用户指导 本文翻译自http://code.google.com/p/guava-libraries/wiki/GuavaExplained,由十八子将翻译,发表于博客园 http://www.cnb ...
- POJ3658Matrix( 双重二分+负数+死循环)
POJ 3658 Matrix 双重二分,wa了一下午,实在不太明白为啥一写二分就会进入死循环. INF要设的大一些,本题设0x3f3f3f3f会wa. 本题有负数, 二分时(l+r)/2与(l+r) ...
- git config --global core.excludesfile配置gitignore全局文件
Linux中,这时把全局要忽略的文件列表.gitignore放当前用户根目录下: git config --global core.excludesfile '~/.gitignogtire' Win ...
- CAFFE安装 CentOS无GPU
前记 由于是在一台用了很久的机器上安装caffe,过程比较复杂,网上说再干净的机器上装比较简单.如果能有干净的机器,就不用再过这么多坑了,希望大家好运!介绍这里就不说了,直接进入正题: Caffe 主 ...
- 【学习笔记】【oc】指针
1.取一个变量的值,可通过直接与间接的方式 直接:访问变量 间接:通过变量的内存地址来获取变量的值. 存放内存地址的变量就是指针变量. 2.定义指针变量的语法:类型* 变量名 &:取地址运算符 ...
- iOS 获取手机 唯一标识-b
存贮在keychainQuery 可以统计用户使用情况 -(void)gatherMessage{ //采集用户设备信息 NSUserDefaults *userDefaults=[NSUserDef ...
- poj 2492A Bug's Life
http://poj.org/problem?id=2492 #include<cstdio> #include<cstring> #include<algorithm& ...
- 【HDOJ】2395 Alarm Clock
水题. /* 2395 */ #include <cstdio> #include <cstring> #include <cstdlib> #define MAX ...
- HDU3549 Flow Problem(网络流增广路算法)
题目链接. 分析: 网络流增广路算法模板题.http://www.cnblogs.com/tanhehe/p/3234248.html AC代码: #include <iostream> ...