0

I am trying to assign a byte value to a byte array value. Now the problem I am getting is it says Illegal start of expression .

This is my running code:

Byte send[];
Byte data1;
Byte data2;
Byte data3;
send = {(byte)0xE1, data1, data2, data3};

Can I know where did I make my mistake.I am getting the error at line 5. Thank you.

raaj5671
  • 95
  • 2
  • 12

2 Answers2

1

You cannot create an array of Byte (or any other type like this). It is a syntax error.
You should use following to assign an array :

send = new Byte[]{(byte)0xE1, data1, data2, data3};

Or you can assign the same way you are doing during declaration :

Byte send[] = {(byte)0xE1, data1, data2, data3};

Note: You will get another error because you have not initialized the variables you are using. They must be initialized before being used

afzalex
  • 8,254
  • 2
  • 29
  • 53
0

Your illegal start of expression is due to the way you are trying to add to the array. To add to an array in java you must append it: send.add((byte)0xE1)

See this question for more information on adding to arrays How to add new elements to an array?

Side Note: Your code won't be miraculously fixed by this. You need to intialize your variables (data1, data2, data3). You also need to declare the length of the array before you can start adding stuff to it, or you could just declare the array later in one go to avoid having to lock down a size straight away. Read this question to see the different ways to declare an array How do I declare and initialize an array in Java?

Community
  • 1
  • 1
Aaron Troeger
  • 165
  • 1
  • 3
  • 17