2

I'm completely stumped.. Seems like there'd be a simple solution

private Byte[] arrayOfBytes = null;    

public Data(String input) {
    arrayOfBytes = new Byte[input.getBytes().length];
    arrayOfBytes = input.getBytes();
}

Throws the follwing error:

incompatible types
    required: java.lang.Byte[]
    found: byte[]
1
  • 1
    Auto-boxing just handles the conversion of values not type casting Commented Feb 16, 2012 at 19:24

5 Answers 5

4

getBytes() from String returns a byte[] and you are trying to affect it to a Byte[].

byte is a primitive whereas Byte is a wrapper class (kind of like Integer and int).

What you can do is change :

private Byte[] arrayOfBytes = null;

to :

private byte[] arrayOfBytes = null;
Sign up to request clarification or add additional context in comments.

Comments

2

So try:

private byte[] arrayOfBytes = null;    

public Data(String input) {
    arrayOfBytes = new byte[input.getBytes().length];
    arrayOfBytes = input.getBytes();
}

2 Comments

Thanks for the quick response! But isn't this exactly what I wrote, line for line?
@marshmallow: nope change Byte to byte.
2

Byte is an Object, while byte is a primative. Like the difference between Integer and int.

Comments

1

getBytes() return a byte[] array. You are assigning to Byte[] array.

So, this should work

private byte[] arrayOfBytes = null;    

public Data(String input) {
    arrayOfBytes = new Byte[input.getBytes().length];
    arrayOfBytes = input.getBytes();
}

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.

Comments

1
public Data(String input) {
    arrayOfBytes = new byte[input.getBytes().length];// this line is useless
    arrayOfBytes = input.getBytes();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.