Reputation: 23
I am creating a new Byte
object in JAVA and using Byte
to construct a String
and it gives an error...
Byte B1 = new Byte((byte)41);
String S1 = new String(B1);
But, no issue when I use byte
instead of Byte
byte d[]= {65,48};
String s1 = new String(d);
what is the difference?
Upvotes: 0
Views: 512
Reputation: 31699
Well, the biggest difference is that in one case, you're using an array, and in the other case, you're not.
However, it's worth pointing out that there would be a difference even if you used an array in both cases. That is:
Byte[] d1 = {65,48}; // legal
byte[] d2 = {65,48}; // legal
String s1 = new String(d1); // illegal
String s2 = new String(d2); // legal
(Note: It's preferable to say Byte[] d1
instead of Byte d1[]
; the two are equivalent, but the first one makes it clear that Byte[]
is the type of the variable, while the second one was included in Java only as a concession to C programmers who were using to writing things that way.)
The point here is that Java will autobox and autounbox Byte
and byte
. That is, if you have a Byte
variable and assign a byte
to it, it will convert automatically, and vice versa. However, this autoboxing and autounboxing doesn't apply to arrays--Java won't automatically convert between Byte[]
and byte[]
.
Unfortunately, I don't see a simple way to convert between the two array types, except by using Apache Commons which has toObject
and toPrimitive
array conversions (see here).
Upvotes: 0
Reputation: 719561
The difference is that in the new String(byte[])
case there is a suitable constructor overload, and in the new String(Byte)
case there isn't.
Why is that?
How should you find out more? For example, what constructors does a type have? What do they do / mean?
By the way, auto-unboxing / widening are not relevant to this example. There is no String(byte)
or String(Byte)
... or String(Object)
constructor. No amount of unboxing or widening is going to make this example work.
And to illustrate that new String(...)
does not work with a byte
...
public class Test {
String s = new String((byte) 42);
}
$ javac Test.java
Test.java:2: error: no suitable constructor found for String(byte)
String s = new String((byte) 42);
^
constructor String.String(String) is not applicable
(argument mismatch; byte cannot be converted to String)
constructor String.String(char[]) is not applicable
(argument mismatch; byte cannot be converted to char[])
constructor String.String(byte[]) is not applicable
(argument mismatch; byte cannot be converted to byte[])
constructor String.String(StringBuffer) is not applicable
(argument mismatch; byte cannot be converted to StringBuffer)
constructor String.String(StringBuilder) is not applicable
(argument mismatch; byte cannot be converted to StringBuilder)
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
$
Upvotes: 3
Reputation: 447
This is because the way autoboxing and unboxing works. Here you can get more information: http://beginnersbook.com/2014/09/java-autoboxing-and-unboxing-with-examples/
Upvotes: -2