java Byte 和byte 差别及byte[ ]和string转换

  先看Byte,是一个类:

public final class Byte

The Byte class wraps a value of primitive type byte in an object. An object of type Byte contains a single field whose type is byte.

In addition, this class provides several methods for converting a byte to a String and a String to a byte, as well as other constants and methods useful when dealing with a byte.

byte是基本数据类型 
Byte是byte的包装类 

我们可以看出Byte是一个类,byte只是一个原始数据类型。Byte是引用类型,byte是值类型(原型), Byte是一个类,有很多方法,方便我们转换为其他类型.

转换Byte【】到string
public class Main {
    
    /*
     * This method converts an byte array to a String object.
     */
    
    public void convertByteArrayToString() {
        
        byte[] byteArray = new byte[] {87, 79, 87, 46, 46, 46};
        
        String value = new String(byteArray);
        
        System.out.println(value);
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Main().convertByteArrayToString();
    }
}

string 转byte【】 

String str;

byte[] a=str.getBytes[];

总结:

String s = "fs123fdsa";//String变量 

byte b[] = s.getBytes();//String转换为byte[] 

String t = new String(b);//bytep[]转换为String

 

原文地址:https://www.cnblogs.com/youxin/p/2601674.html