zoukankan      html  css  js  c++  java
  • 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

     

  • 相关阅读:
    leetcode8.字符串转换整数(atoi)
    leetcode7、整数反转
    leetcode6.Z字形变换
    leetcode5.最长回文子串
    leetcode4.寻找两个正序数组的中位数
    leetcode3. 无重复字符的最长子串
    leetcode 2.两数相加
    leetcode 1. 两数之和
    post&get请求总结
    oracle知识总结
  • 原文地址:https://www.cnblogs.com/youxin/p/2601674.html
Copyright © 2011-2022 走看看