Amar C
Amar C

Reputation: 374

Using byte in java

How does the code in java

     for (int a=0; a < 256; a++) {
        sta[a] = (byte)a;
    System.out.println(a);
    }

prints 0 to 255 when the maximum value if a byte is 127.

How to convert int to unsigned byte using "& oxff" ?

Upvotes: 0

Views: 1021

Answers (2)

Plaix
Plaix

Reputation: 881

the width of byte is 8 bits while integer is 32 bits. An you can convert for example

int a = 23;
byte b = (byte)a;

Upvotes: 0

Rocreex
Rocreex

Reputation: 160

You are casting the integer variable a into a byte variable being stored in an array. However the variable you are printing is a which still is an integer.

Upvotes: 4

Related Questions