Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Length of byte[] array

Tags:

java

arrays

byte

String str = "123456789";
byte[] bytes = str.getBytes();

I want to make the following loop

for (int j = 0; j < bytes.length(); j++) {
  b = bytes[j];
}

b will store each byte of my array, but I can't seem to get the length of the array correctly.

Error:cannot find symbol
Problem solved:  bytes.length instead of  bytes.length()

2 Answers

Use bytes.length without the ()

like image 186
Brian Avatar answered Sep 03 '25 14:09

Brian


See the JLS - 10.7. Array Members:

The members of an array type are all of the following:

The public final field length, which contains the number of components of the array. length may be positive or zero.

length is a property, not a method. You should write:

bytes.length
like image 35
Maroun Avatar answered Sep 03 '25 13:09

Maroun