当前位置:网站首页>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():将对象打印输出的时候,或者需要获取对象的地址的时候使用
边栏推荐
- Web mobile terminal: touchmove realizes local scrolling
- Common linear modulation methods based on MATLAB
- "Mqtt protocol explanation and Practice (access to onenet)" of wiznet w5500 series training activities
- Make a reliable delay queue with redis
- 世界上最高效的笔记方法(改变你那老版的记笔记方法吧)
- 黑客入门教程(非常详细)从零基础入门到精通,看完这一篇就够了。
- Flinksql client connection Kafka select * from table has no data error, how to solve it?
- LVS load balancing lvs-nat building Web Cluster
- Leetcode sword finger offer 28. symmetric binary tree
- Smart cloud IOT platform STM32 esp8266-01s simple wireless light control
猜你喜欢

There is a newline problem when passing shell script parameters \r

全网显示 IP 归属地,是怎么实现的?

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)

用 Redis 做一个可靠的延迟队列

Fillet big killer, use filter to build fillet and wave effect!

基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现

Stm32cubemx learning record -- installation, configuration and use

W5500 upload temperature and humidity to onenet platform

What kind of product power does Hongguang miniev, the top seller of new energy, have?

【mysql学习09】
随机推荐
Wiznet embedded Ethernet technology training open class (free!!!)
leetcode 剑指 Offer 28. 对称的二叉树
LVS load balancing lvs-nat building Web Cluster
LVS负载均衡之LVS-DR搭建Web群集与LVS结合Keepalived搭建高可用Web群集
SQL注入 Less23(过滤注释符)
Small and micro enterprise smart business card management applet
Let sports happen naturally, and fire creates a new lifestyle
ESP8266 使用 DRV8833驱动板驱动N20电机
Shell fourth day homework
相似矩阵,可对角化条件
Learn NLP with Transformer (Chapter 1)
Signal and slot mechanism ==pyqt5
教你如何通过MCU将S2E配置为UDP的工作模式
2022 年中回顾|一文看懂预训练模型最新进展
黑客入门教程(非常详细)从零基础入门到精通,看完这一篇就够了。
How does the whole network display IP ownership?
Similarity matrix, diagonalization condition
web移动端:touchmove实现局部滚动
Menu bar + status bar + toolbar ==pyqt5
Teach you how to configure S2E to UDP working mode through MCU