当前位置:网站首页>toString()与new String()用法区别
toString()与new String()用法区别
2022-07-25 10:51:00 【洒家肉山大魔王】
我们在平时工作中做Base6编解码数据转字符串时经常会遇到toString()与new String(),对于这两者的用法区别简单总结一下。
举个栗子
测试用例:
@Test
public void myTest1() {
String value = "01234567012345670123456701234567";
String base64String = Base64.toBase64String(value.getBytes());
System.out.println("Base64编码:" + base64String );
byte[] decode = Base64.decode(base64String);
System.out.println("Base64解码:" + decode );
}输出结果:
Base64编码:MDEyMzQ1NjcwMTIzNDU2NzAxMjM0NTY3MDEyMzQ1Njc=
Base64解码:[[email protected]
对解码后的数据处理:
在上边的测试用例稍加改动:
@Test
public void myTest1() {
String value = "01234567012345670123456701234567";
String base64String = Base64.toBase64String(value.getBytes());
System.out.println("Base64编码:" + base64String );
byte[] decode = Base64.decode(base64String);
System.out.println("Base64解码:" + decode );
String newString = new String(decode);
System.out.println("newString结果:" + newString );
String toString = decode.toString();
System.out.println("toString结果:" + toString );
}输出结果:
Base64编码:MDEyMzQ1NjcwMTIzNDU2NzAxMjM0NTY3MDEyMzQ1Njc=
Base64解码:[[email protected]
newString结果:01234567012345670123456701234567
toString结果:[[email protected]
对比一下发现,相同的字节数组格式的解码数据,在做字符串转换时,输出的结果区别很大,哪一个用法是正确的?为什么?
这里应该用new String()的方法,因为Base64是一种转换编码格式的算法。
toString()与new String ()用法区别
- toString():是调用了Object类中的toString()方法。一般是返回这么一个String:[class name]@[hashCode],实际上就是对象的哈希值。
这点可以从toString()方法的源码看出

调用该方法将获取一个 String:[class name]@[hashCode] 这么一个字符串对象。
- new String(byte[] parameter):入参parameter是一个字节数组,使用java虚拟机默认的编码格式,将这个入参字节数组解码转换为对应的字符。若虚拟机默认的编码格式是ISO-8859-1,按照ascii编码表即可得到字节对应的字符。
如果恰当地使用?
- new String():一般使用字符转码的时候,即字节数组转换为字符串的时候。比如在对数据做编解码/加解密转为二进制字符串时,就可以直接使用此方法进行转换处理。
- toString():将对象打印输出的时候,或者需要获取对象的地址的时候使用
边栏推荐
猜你喜欢

Common linear modulation methods based on MATLAB

论文解读(MaskGAE)《MaskGAE: Masked Graph Modeling Meets Graph Autoencoders》
![[MySQL learning 08]](/img/9e/6e5f0c4c956ca8dc31d82560262013.png)
[MySQL learning 08]

Breadth first traversal (problems related to sequence traversal of graphs and binary trees)

相似矩阵,可对角化条件

Emmet syntax quick query syntax basic syntax part

SQL injection less17 (error injection + subquery)

Introduction to shortcut keys in debug chapter

"Mqtt protocol explanation and Practice (access to onenet)" of wiznet w5500 series training activities

Want to record your supernatural moments when playing games? Let's take a look at how to use unity screenshots
随机推荐
Esp8266 uses drv8833 drive board to drive N20 motor
动态规划问题03_最大子段和
基于W5500实现的考勤系统
Database integrity -- six constraints learning
Leetcode sword finger offer 27. image of binary tree
【电子器件笔记5】二极管参数和选型
ESP8266 使用 DRV8833驱动板驱动N20电机
贪心问题01_活动安排问题
Leetcode sword finger offer 28. symmetric binary tree
LVS load balancing lvs-dr builds Web Clusters and LVS combines with kept to build highly available Web Clusters
各种控件==PYQT5
DNS分离解析的实现方法详解
Several common PCB surface treatment technologies!
Game backpack system, "inventory Pro plug-in", research and learning ----- mom doesn't have to worry that I won't make a backpack anymore (unity3d)
What kind of product power does Hongguang miniev, the top seller of new energy, have?
Database design - Simplified dictionary table [easy to understand]
Shell - Chapter 6 exercise
SQL language (II)
Want to record your supernatural moments when playing games? Let's take a look at how to use unity screenshots
Common linear modulation methods based on MATLAB