当前位置:网站首页>Several methods of converting object to string
Several methods of converting object to string
2022-07-29 03:08:00 【zhougl996】
1.object.toString() Method
This method should pay attention to object Not for null, Otherwise it will be reported NullPointException, Don't use this method in general .
2.String.valueOf(object) Method
There is no need to worry about this method object by null The problem of , if nul
l, It will be converted to "null" character string , instead of null. Special attention should be paid to this point ."null" and null It's not a concept .
3.(String)(object) Method
There is no need to worry about this method object by null The problem of . however ,object If it can be converted to String The object of . if Object object = 1, Again (String)1, Class conversion exception will be reported .
4.“”+object Method
There is no need to worry about this method object by null The problem of . But if object by null, Returns the "null" character string , and String.valueOf(object) equally .
The following is a code example :
public class Test {
public static void main(String[] args) {
Object object = null;
System.out.println("(String)null and \"null\" The result of the comparison is :" + ("null".equals((String)object)));
System.out.println("String.valueOf(null) and \"null\" The result of the comparison is :" + "null".equals(String.valueOf(object)));
System.out.println("(\"\" + null) and \"null\" The result of the comparison is :" + "null".equals("" + object));
}
}Running results :
(String)null and "null" The result of the comparison is :false
String.valueOf(null) and "null" The result of the comparison is :true
("" + null) and "null" The result of the comparison is :true
| The way | Example | matters needing attention |
|---|---|---|
| Object.toString() | Object object = getObject(); System.out.println(object.toString()); | Pay attention when using , Must ensure object No null value , Otherwise, it will throw NullPointerException abnormal . |
| (String)obj | Object obj = new Object (); String strVal = (String)obj; | The converted data type must be converted to String type . So it's best to use it instanceof Do a type check , To see if it can be converted . Otherwise, it's easy to throw CalssCastException abnormal . Besides , The reason to be particularly careful is defined as Object Objects of type are being converted to String Grammar checking does not report errors , This can lead to potential errors , in addition , because null Value can be cast to any java Class types ,(String)null It's also legal . |
| String.valueOf(Object) | String.valueOf(object) | Pay attention to when object by null when ,String.valueOf(object) The value of is a string "null", instead of null, It is equivalent to displaying the empty flag as data . |
边栏推荐
- Shell programming specifications and variables
- kubernetes-1.24.x 特性
- cuda-gdb提示:/tmp/tmpxft_***.cudafe1.stub.c: No such file or directory.
- vasp计算任务报错:M_divide:can not subdivide 8 nodes by 6
- 【FreeSwitch开发实践】media bug获取通话语音流
- Data truncation and estimation
- Summary of common hooks
- MySQL - the difference between count (field), count (primary key), count (1), count (*)
- Jinshan cloud returns to Hong Kong for listing: Hong Kong stock rush of Chinese to B cloud manufacturers
- TP5.0 小程序用户无需登录,直接获取用户手机号。
猜你喜欢

sqlilabs less-32~less-33

01-SDRAM:初始化模块的代码

【FreeSwitch开发实践】UniMRCP编译与安装

瀚高数据库最佳实践配置工具HG_BP日志采集内容

C语言程序设计 | 交换二进制数奇偶位(宏实现)

Interpreting AI robots' pet raising and leading fashion trends

万字详解 Google Play 上架应用标准包格式 AAB

西瓜书学习第六章---SVM

2.nodejs--路径(_dirname,_filname)、url网址、querystring模块、mime模块、各种路径(相对路径)、网页的加载(面试题*)

Verilog's time system tasks - $time, $stime, $realtime
随机推荐
算法---粉刷房子(Kotlin)
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
2.nodejs--路径(_dirname,_filname)、url网址、querystring模块、mime模块、各种路径(相对路径)、网页的加载(面试题*)
SAP 中国本地化内容汇总
Apache文件管理自学笔记——映射文件夹和基于单ip多域名配置apache虚拟机
Wechat's crazy use of glide - life cycle learning
Digital image processing Chapter 10 - image segmentation
Inventory of domestic and foreign project collaborative management software: SAAS and customization become a trend
国产ERP有没有机会击败SAP ?
C traps and defects Chapter 3 semantic "traps" 3.3 array declaration as parameters
The basic concept of the origin and connotation of maker Education
【打开新世界大门】看测试老鸟如何把API 测试玩弄在鼓掌之间
Production deployment zabbix5.0 notes
.net serialize enumeration as string
美联储再加息,75基点 鲍威尔“放鸽”,美股狂欢
C陷阱与缺陷 第3章 语义“陷阱” 3.3 作为参数的数组声明
会议OA之反馈功能
13_ UE4 advanced_ Montage animation realizes attack while walking
13_ue4进阶_蒙太奇动画实现一边走一边攻击
C陷阱与缺陷 第2章 语法“陷阱” 2.6 “悬挂”else引发的问题