Cast « byte « Java Data Type Q&A

1. cast byte to enum

I have the following code:

public enum PortableTypes { Boolean, etc...};

public void testConversion() {
  byte b = 5;
  PortableTypes type = (PortableTypes)b;
When I compile, I am told that these ...

2. Java instanceof and byte[]

What I would expect is that 'potentialByteArray instanceof byte[] would return true when potentialByteArray is an instance of a byte[], but this doesn't seem to happen -- it's always false for ...

3. Why do I have to cast 0 to byte when the method argument is byte?

Why do I have to cast 0 to byte when the method argument is byte? Example:

void foo() {
   bar((byte) 0);

void bar(byte val) {}
I know that if the argument is of ...

4. casting problem...from long to byte..

Hi, I m preparing for scjp and refering book by bertbates/kathy seera... I m not able to clear one funda that while explicit casting from long to byte... when the value is greater then +127. in that book ... in one page there is written about removing of higher order bits left to 8 lower order bits...and if last bit left ...

5. Cast a long into a byte - please explain

When I cast a long into an byte, and the long is larger than the byte, can someone show me what is happening in the background? long l = 130; byte b = (byte)l; I know that the byte is widened to an integer type, but not sure what else is happening to get the answer -126. Can someone tell me ...

6. casting from String to byte[]

You can use the getBytes() method of String class also ...and then cast each byte of the array to char for further is the code below... class StringByte { public static void main(String[] args) { String str = "00000000"; byte[] b = str.getBytes(); System.out.println("Hello World!-----"+b.length); for(int i = 0; i < b.length; i++) System.out.println("element ----"+(char)b[i]); } }

7. cast to bytes

class Test { public static void main(String[] args) { byte a = 127; byte b = (byte) 128; byte c = (byte) 256; System.out.println("Value of a:" + a); System.out.println("\nValue of b:" + b); System.out.println("\nValue of c:" + c); } } The output is Value of a:127 Value of b:-128 Value of c:0 I understand that casting is required after 8 bits ...

8. How to casting from byte[] to String??

9. Cast Byte[] to byte[]

You can't. Create a byte[] array of the same length as the Byte[] array and then copy the values (you can use System.arraycopy or something from Arrays). You won't have to worry about casting the individual values, or using byteValue on the Byte instances as autoboxing will take care of the conversions. It simply can't (or at least won't, with good ...