当前位置:网站首页>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():将对象打印输出的时候,或者需要获取对象的地址的时候使用
边栏推荐
- [MySQL learning 08]
- Several common PCB surface treatment technologies!
- Breadth first traversal (problems related to sequence traversal of graphs and binary trees)
- Shell - Chapter 8 exercise
- Reptile foundation I
- Talking about Devops monitoring, how does the team choose monitoring tools?
- 相似矩阵,可对角化条件
- Make a reliable delay queue with redis
- SQL injection less17 (error injection + subquery)
- Brief description of model deployment
猜你喜欢

Only know that the preform is used to generate objects? See how I use unity to generate UI prefabs

基于MATLAB的常见线性调制方法

SQL language (4)

【mysql学习09】
Definition of information entropy

Teach you how to configure S2E to UDP working mode through MCU

JDBC summary

SQL注入 Less23(过滤注释符)

SQL injection less23 (filter comment)

ArcMap cannot start the solution
随机推荐
全网显示 IP 归属地,是怎么实现的?
Miidock Brief
Varest blueprint settings JSON
圆角大杀器,使用滤镜构建圆角及波浪效果!
LVS负载均衡之LVS-DR搭建Web群集与LVS结合Keepalived搭建高可用Web群集
PostgreSQL stepping on the pit | error: operator does not exist: UUID = character varying
苹果美国宣布符合销售免税假期的各州产品清单细节
W5500在处于TCP_Server模式下,在交换机/路由器网络中无法ping通也无法通讯。
用 Redis 做一个可靠的延迟队列
基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
leetcode 剑指 Offer 28. 对称的二叉树
[tree] 100. Same tree
Web mobile terminal: touchmove realizes local scrolling
如何解决“W5500芯片在TCP_Client模式下,断电重启之后无法立即连接到服务器”的问题
活动报名 | 玩转 Kubernetes 容器服务提高班正式开营!
Reinforcement learning (IV)
贪心问题01_活动安排代码分析
Eigenvalues and eigenvectors of matrices
【leetcode刷题】
Greedy problem 01_ Activity arrangement code analysis