从byte []创建字符串

时间:2013-10-25 12:27:46

标签: java

如果像这样创建,则工作正常:

byte [] d = {1,2};
String ss = new String( d );

但如果像这样创作就失败了:

String ss = new String( {1,2} );

甚至是这样:

String ss = new String( {(byte)1,(byte)2});

有什么问题?

2 个答案:

答案 0 :(得分:3)

String ss = new String( new byte[]{1,2} );

String ss = new String( {1,2} );不起作用,因为只需执行{}块就无法初始化数组。它需要前面的new someThing[]

答案 1 :(得分:0)

String ss = new String (array of bytes);
 we can pass here array of bytes but in
String ss = new String( {1,2} ); {1 , 2} this is not an array and 
String ss = new String( {(byte)1,(byte)2}); {(byte)1,(byte)2} this is also not an array 

So we can simply use 
String ss = new String( new byte[]{1,2} );
相关问题