java.lang中String=和equals()函数解析
java.lang是java语言的基础包。String类为lang包中的一个基础类。本文主要讨论 "="和"equals()"方法对String的不同判断结果。
"=" .VS. "equals()" :
"="判断的是两个String类型的引用是否指向同一个对象,如果是,那么表达式结果为true,如果不是,那么表达式结果为false;
"equals"判断的是两个String类型的引用所指向对象的值,如果相等,那么返回结果为true,如果不相等,那么返回结果为false;
下面是我的一段简单的实验程序。
1 package BasicKnowledge.Testlang; 2 3 public class StringTest { 4 public static void main(String args[]) 5 { 6 String str1; 7 str1 = new String("Str"); 8 String str2 = new String("Str"); 9 10 11 12 String str3; 13 str3 = str1; 14 String equal = "The two strings are equal with"; 15 String notEqual = "The two strings are not equal"; 16 17 String stat2 = "str1 and str2 are two different objects but they have the same value"; 18 if(equalSign(str1,str2)) 19 { 20 System.out.println(stat2+";"+equal+" '=' sign"); 21 }else System.out.println(stat2+";"+notEqual+" '=' sign"); 22 23 if(equalFunction(str1,str2)) 24 { 25 System.out.println(stat2+";"+equal+ " 'equal()' function"); 26 }else System.out.println(stat2+";"+notEqual+" 'equal()' fuction"); 27 28 str3 = str1; 29 String stat = "str3 shares the same reference with str1"; 30 31 if(equalSign(str1,str3)) 32 { 33 System.out.println(stat+";"+equal+" '=' sign"); 34 }else System.out.println(stat+";"+notEqual+" '=' sign"); 35 36 if(equalFunction(str1,str3)) 37 { 38 System.out.println(stat+";"+equal+ " 'equal()' function"); 39 }else System.out.println(stat+";"+notEqual+" 'equal()' fuction"); 40 } 41 42 static boolean equalSign(String a, String b)//在静态方法中,是不能访问非静态成员,所以这里用static来形容 43 { 44 boolean backValue = true; 45 if(a == b) 46 { 47 backValue = true; 48 }else backValue = false; 49 return backValue; 50 } 51 52 static boolean equalFunction(String a, String b) 53 { 54 boolean backValue = true; 55 if(a.equals(b)) 56 { 57 backValue = true; 58 }else backValue = false; 59 return backValue; 60 } 61 62 }
编译和运行结果如下:
str1 and str2 are two different objects but they have the same value;The two strings are not equal '=' sign str1 and str2 are two different objects but they have the same value;The two strings are equal with 'equal()' function str3 shares the same reference with str1;The two strings are equal with '=' sign str3 shares the same reference with str1;The two strings are equal with 'equal()' function
转载于:https://www.cnblogs.com/SevenwindMa/p/3670603.html
java.lang中String=和equals()函数解析相关推荐
- java.lang中String类源码分析
一.类 public final class String:final关键字说明String类不能被修改(不能被其他类继承和重写) public final class Stringimplement ...
- Excel 导入数据库中java.lang.StringIndexOutOfBoundsException: String index out of range: -50提示解决原因...
dbf文件另存为.xls然后导入到sql数据库里会报错. java.lang.StringIndexOutOfBoundsException: String index out of range: - ...
- java代码中fastjson生成字符串和解析字符串的方法和javascript文件中字符串和json数组之间的转换方法...
1.java代码中fastjson生成字符串和解析字符串的方法 List<TemplateFull> templateFulls = new ArrayList<TemplateFu ...
- java.lang.StringIndexOutOfBoundsException: String index out of range: 0错误的原因
2019独角兽企业重金招聘Python工程师标准>>> java.lang.StringIndexOutOfBoundsException: String index out of ...
- java.lang.StringIndexOutOfBoundsException: String index out of range: -1
在这里插入代码片 package com.itheima.d3_char_buffer; import java.io.*; import java.util.ArrayList; import ja ...
- Java开发中Netty线程模型原理解析!
Java开发中Netty线程模型原理解析,Netty是Java领域有名的开源网络库具有高性能和高扩展性的特点,很多流行的框架都是基于它来构建.Netty 线程模型不是一成不变的,取决于用户的启动参数配 ...
- springBoot-tk.mybatis.mapper.MapperException: java.lang.StringIndexOutOfBoundsException: String inde
springBoot-tk.mybatis.mapper.MapperException: java.lang.StringIndexOutOfBoundsException: String inde ...
- java 0xf0_java 中类似js encodeURIComponent 函数的实现案例
我就废话不多说了,大家还是直接看代码吧~ import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import ...
- 【JAVA源码分析——Java.lang】String源码分析
String 基本实现 初学java经常会误认为String是java基本类型,实际上String并非Java基本类型,String本质上是对char数组的封装. 以下是String实现相关源码 /* ...
最新文章
- seo需要处理页面html,SEO人员,正确处理页面标题的三大思考?
- jquery的基本api
- mixin network_基于Mixin Network的Ruby比特币开发教程
- FPGA 实验六 计数器、 ROM和DDS
- 王道考研 计算机网络17 IP数据报 最大传送单元MTU IP地址 IPv4 子网划分 ARP协议 ICMP协议 移动IP
- Linux——VMware虚拟机安装CentOS步骤
- ‘ascii‘ codec can‘t encode characters in position
- 使用CLion构建Qt项目
- html调用谷歌内核,360浏览器指定Webkit(chrome)内核
- pytorch调用不了多个gpu_pytorch利用多个GPU并行计算
- Java程序模拟QQ空间登录 - 并模拟刷说说的赞(图文) 注意:腾讯修改了加密算法,已失效(2015-01-31)
- EO CAT软件下载数据
- 杭电oj部分新手入门题目全解(1089-1096)
- Mybatis缓存详解
- Scala+HuffmanCoding实现无损压缩
- 安卓各版本大变化(Android 6.0到10.0),兼容适配
- VAM语料库--学习笔记
- 基于STM32的万能红外遥控器
- GlobalSign即将停止签发SHA1代码签名证书
- c语言动态分配内存keil,keil5中结构体分配内存问题
热门文章
- 模糊pid控制的温度系统matlab源代码_模糊PID控制系统(二)对比分析
- echarts时间轴传什么格式_职场必看,使用Excel实现大事件时间轴的做法,不看后悔...
- [SQL Server 2014] SQL Server 2014新特性探秘
- Excel 求差集和并集
- Java杂记3—流程控制之条件 1
- 从HTTP响应头看苹果国内用了哪家CDN及CDN高防DDOS技术
- LVS+Keepalive+Nginx实现负载均衡
- Linux系统NFS故障现象
- 联发科MT8377 MT8389 MT6589 MT6577等芯片详细解析
- 使用Netapp与windows server 2012搭建 iSNS服务器