Java中String, byte[], char[],StringBuffer, StringBuilder的区别联系与常见操作

byte和char的区别

byte是字节数据类型、有符号型的、占1个字节、大小范围为-128——127
char是字符数据类型、无符号型的、占2个字节(unicode码)、大小范围为0-65535
byte和char在指定编码类型的情况下,可以相互转换

String、byte[]和char[]的联系

String的构造函数

byte[]转String

String(byte[] bytes)
Constructs a new String by decoding the specified array of bytes using the platform’s default charset.
String(byte[] bytes, Charset charset)
Constructs a new String by decoding the specified array of bytes using the specified charset.
String(byte[] bytes, int offset, int length)
Constructs a new String by decoding the specified subarray of bytes using the platform’s default charset.
String(byte[] bytes, int offset, int length, Charset charset)
Constructs a new String by decoding the specified subarray of bytes using the specified charset.
String(byte[] bytes, int offset, int length, String charsetName)
Constructs a new String by decoding the specified subarray of bytes using the specified charset.
String(byte[] bytes, String charsetName)
Constructs a new String by decoding the specified array of bytes using the specified charset.

char[]转String

与byte[]不同,char[]转String不用指定编码类型
String(char[] value)
Allocates a new String so that it represents the sequence of characters currently contained in the character array argument.
String(char[] value, int offset, int count)
Allocates a new String that contains characters from a subarray of the character array argument.

String转byte[]

需指定编码类型,默认使用平台的默认编码
byte[] getBytes()
Encodes this String into a sequence of bytes using the platform’s default charset, storing the result into a new byte array.
byte[] getBytes(Charset charset)
Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array.
byte[] getBytes

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值