当前位置:网站首页>Differences in usage between tostring() and new string()
Differences in usage between tostring() and new string()
2022-07-25 11:50:00 【The great demon king of meat mountain in Sajia】
We do it in our daily work Base6 When encoding and decoding data to string, we often encounter toString() And new String(), Briefly summarize the usage differences between the two .
Take a chestnut
The test case :
@Test
public void myTest1() {
String value = "01234567012345670123456701234567";
String base64String = Base64.toBase64String(value.getBytes());
System.out.println("Base64 code :" + base64String );
byte[] decode = Base64.decode(base64String);
System.out.println("Base64 decode :" + decode );
}Output results :
Base64 code :MDEyMzQ1NjcwMTIzNDU2NzAxMjM0NTY3MDEyMzQ1Njc=
Base64 decode :[[email protected]
Process the decoded data :
The test case above is slightly changed :
@Test
public void myTest1() {
String value = "01234567012345670123456701234567";
String base64String = Base64.toBase64String(value.getBytes());
System.out.println("Base64 code :" + base64String );
byte[] decode = Base64.decode(base64String);
System.out.println("Base64 decode :" + decode );
String newString = new String(decode);
System.out.println("newString result :" + newString );
String toString = decode.toString();
System.out.println("toString result :" + toString );
}Output results :
Base64 code :MDEyMzQ1NjcwMTIzNDU2NzAxMjM0NTY3MDEyMzQ1Njc=
Base64 decode :[[email protected]
newString result :01234567012345670123456701234567
toString result :[[email protected]
Compare the findings , Decoded data in the same byte array format , When doing string conversion , The output results are very different , Which usage is correct ? Why? ?
It should be used here new String() Methods , because Base64 It is an algorithm for converting coding format .
toString() And new String () Usage difference
- toString(): It's called Object Class toString() Method . Generally, it returns such a String:[class name]@[hashCode], In fact, it is the hash value of the object .
This can be done from toString() From the source code of the method

Calling this method will get a String:[class name]@[hashCode] Such a string object .
- new String(byte[] parameter): Enter the reference parameter It's an array of bytes , Use java The default encoding format for virtual machines , Decode and convert the input parameter byte array into the corresponding characters . If the default encoding format of the virtual machine is ISO-8859-1, according to ascii The character corresponding to the byte can be obtained from the encoding table .
If used properly ?
- new String(): When using character transcoding , That is, when the byte array is converted to a string . For example, when encoding and decoding data / When encryption and decryption are converted to binary strings , You can directly use this method for conversion .
- toString(): When printing out objects , Or when you need to get the address of the object
边栏推荐
- flinksql client 连接kafka select * from table没有数据报错,如何解决?
- 已解决The JSP specification requires that an attribute name is preceded by whitespace
- SQL injection LESS18 (header injection + error injection)
- DICOM medical image viewing and browsing function based on cornerstone.js
- SQL injection less23 (filter comment)
- Miidock Brief
- 11. Reading rumors spread with deep learning
- Various controls ==pyqt5
- SQL language (I)
- JS data types and mutual conversion
猜你喜欢
随机推荐
Information management system for typical works of urban sculpture (picture sharing system SSM)
Web APIs(获取元素 事件基础 操作元素)
贪心问题01_活动安排代码分析
Common web attacks and defense
【USB设备设计】--复合设备,双HID高速(64Byte 和 1024Byte)
Small and micro enterprise smart business card management applet
[MySQL learning 08]
Javescript loop
JS常用内置对象 数据类型的分类 传参 堆栈
使用Three.js实现炫酷的赛博朋克风格3D数字地球大屏
The most efficient note taking method in the world (change your old version of note taking method)
各种控件==PYQT5
【MySQL 17】安装异常:Could not open file ‘/var/log/mysql/mysqld.log‘ for error logging: Permission denied
JDBC summary
JS数据类型以及相互转换
JS运算符
W5500上传温湿度到oneNET平台
varest蓝图设置json
信号与槽机制==PYQT5
SQL language (4)







