How to initialize a byte array in Java

2 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        byte[] bytes = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x65};
          
        for (byte b : bytes) {
            System.out.print(b + " ");
        }
    }
}
   
   
   
   
/*
run:
  
0 1 2 3 4 5 6 7 8 101 
  
*/

 



answered Nov 14, 2023 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        byte[] bytes = new byte[10];
        
        for (int i = 0; i < 10; i++) {
            bytes[i] = (byte)i;
        }
         
        for (byte b : bytes) {
            System.out.print(b + " ");
        }
    }
}
  
  
  
  
/*
run:
 
0 1 2 3 4 5 6 7 8 9 
 
*/

 

 



answered Nov 14, 2023 by avibootz

Related questions

1 answer 179 views
1 answer 200 views
3 answers 122 views
3 answers 133 views
4 answers 139 views
3 answers 123 views
2 answers 88 views
...